FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/encode.c
Date: 2026-10-30 17:37:53
Exec Total Coverage
Lines: 387 594 65.2%
Functions: 27 30 90.0%
Branches: 295 501 58.9%

Line Branch Exec Source
1 /*
2 * generic encoding-related code
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "libavutil/avassert.h"
22 #include "libavutil/channel_layout.h"
23 #include "libavutil/emms.h"
24 #include "libavutil/frame.h"
25 #include "libavutil/internal.h"
26 #include "libavutil/intreadwrite.h"
27 #include "libavutil/mem.h"
28 #include "libavutil/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 1624819 static EncodeContext *encode_ctx(AVCodecInternal *avci)
58 {
59 1624819 return (EncodeContext*)avci;
60 }
61
62 31266 int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
63 {
64
2/4
✓ Branch 0 taken 31266 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 31266 times.
31266 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 31266 times.
31266 av_assert0(!avpkt->data);
71
72 31266 av_fast_padded_malloc(&avctx->internal->byte_buffer,
73 31266 &avctx->internal->byte_buffer_size, size);
74 31266 avpkt->data = avctx->internal->byte_buffer;
75
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31266 times.
31266 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 31266 avpkt->size = size;
80
81 31266 return 0;
82 }
83
84 538459 int avcodec_default_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int flags)
85 {
86 int ret;
87
88
2/4
✓ Branch 0 taken 538459 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 538459 times.
538459 if (avpkt->size < 0 || avpkt->size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
89 ✗ return AVERROR(EINVAL);
90
91
2/4
✓ Branch 0 taken 538459 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 538459 times.
538459 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 538459 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
97
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 538459 times.
538459 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 538459 avpkt->data = avpkt->buf->data;
102
103 538459 return 0;
104 }
105
106 538459 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 538459 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 538459 times.
538459 if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
111 ✗ return AVERROR(EINVAL);
112
113
2/4
✓ Branch 0 taken 538459 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 538459 times.
538459 av_assert0(!avpkt->data && !avpkt->buf);
114
115 538459 avpkt->size = size;
116 538459 ret = avctx->get_encode_buffer(avctx, avpkt, flags);
117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 538459 times.
538459 if (ret < 0)
118 ✗ goto fail;
119
120
2/4
✓ Branch 0 taken 538459 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 538459 times.
538459 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 538459 memset(avpkt->data + avpkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
126
127 538459 ret = 0;
128 538459 fail:
129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 538459 times.
538459 if (ret < 0) {
130 ✗ av_log(avctx, AV_LOG_ERROR, "get_encode_buffer() failed\n");
131 ✗ av_packet_unref(avpkt);
132 }
133
134 538459 return ret;
135 }
136
137 539135 static int encode_make_refcounted(AVCodecContext *avctx, AVPacket *avpkt)
138 {
139 539135 uint8_t *data = avpkt->data;
140 int ret;
141
142
2/2
✓ Branch 0 taken 507869 times.
✓ Branch 1 taken 31266 times.
539135 if (avpkt->buf)
143 507869 return 0;
144
145 31266 avpkt->data = NULL;
146 31266 ret = ff_get_encode_buffer(avctx, avpkt, avpkt->size, 0);
147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31266 times.
31266 if (ret < 0)
148 ✗ return ret;
149 31266 memcpy(avpkt->data, data, avpkt->size);
150
151 31266 return 0;
152 }
153
154 /**
155 * Pad last frame with silence.
156 */
157 216 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 216 frame->format = src->format;
164 216 frame->nb_samples = out_samples;
165 216 ret = av_channel_layout_copy(&frame->ch_layout, &s->ch_layout);
166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 216 times.
216 if (ret < 0)
167 ✗ goto fail;
168 216 ret = av_frame_get_buffer(frame, 0);
169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 216 times.
216 if (ret < 0)
170 ✗ goto fail;
171
172 216 ret = av_frame_copy_props(frame, src);
173
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 216 times.
216 if (ret < 0)
174 ✗ goto fail;
175
176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 216 times.
216 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
177 216 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 216 times.
216 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
181 216 frame->nb_samples - src->nb_samples,
182 s->ch_layout.nb_channels, s->sample_fmt)) < 0)
183 ✗ goto fail;
184
185 216 discard_padding = frame->nb_samples - src->nb_samples;
186 av_assert1(discard_padding > 0);
187 216 sd = av_frame_new_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES, 10);
188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 216 times.
216 if (!sd) {
189 ✗ ret = AVERROR(ENOMEM);
190 ✗ goto fail;
191 }
192 216 AV_WL32A(sd->data, 0);
193 216 AV_WL32A(sd->data + 4, discard_padding);
194 216 AV_WL16A(sd->data + 8, 0);
195
196 216 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 989 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 989 times.
989 if (sub->start_display_time) {
209 ✗ av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
210 ✗ return -1;
211 }
212
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 989 times.
989 av_assert0(buf_size >= 0);
213
214 989 ret = ffcodec(avctx->codec)->cb.encode_sub(avctx, buf, buf_size, sub);
215 989 avctx->frame_num++;
216 989 return ret;
217 }
218
219 1081548 int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame)
220 {
221 1081548 AVCodecInternal *avci = avctx->internal;
222
223
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1081548 times.
1081548 if (avci->draining)
224 ✗ return AVERROR_EOF;
225
226
2/2
✓ Branch 0 taken 542438 times.
✓ Branch 1 taken 539110 times.
1081548 if (!avci->buffer_frame->buf[0])
227 542438 return AVERROR(EAGAIN);
228
229 539110 av_frame_move_ref(frame, avci->buffer_frame);
230
231 539110 return 0;
232 }
233
234 523748 static int encode_set_packet_props(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame)
235 {
236 523748 AVCodecInternal *avci = avctx->internal;
237 523748 EncodeContext *ec = encode_ctx(avci);
238
239
2/2
✓ Branch 0 taken 506095 times.
✓ Branch 1 taken 17653 times.
523748 if (avpkt->pts == AV_NOPTS_VALUE) {
240 506095 avpkt->pts = frame->pts;
241
4/4
✓ Branch 0 taken 373325 times.
✓ Branch 1 taken 132770 times.
✓ Branch 2 taken 370125 times.
✓ Branch 3 taken 3200 times.
506095 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO && avpkt->pts != AV_NOPTS_VALUE)
242 370125 avpkt->pts -= ff_samples_to_time_base(avctx, avctx->initial_padding);
243 }
244
245
2/2
✓ Branch 0 taken 519161 times.
✓ Branch 1 taken 4587 times.
523748 if (!avpkt->duration) {
246
2/2
✓ Branch 0 taken 498422 times.
✓ Branch 1 taken 20739 times.
519161 if (frame->duration)
247 498422 avpkt->duration = frame->duration;
248
2/2
✓ Branch 0 taken 3200 times.
✓ Branch 1 taken 17539 times.
20739 else if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
249 3200 avpkt->duration = ff_samples_to_time_base(avctx,
250 3200 frame->nb_samples);
251 }
252
2/2
✓ Branch 0 taken 386387 times.
✓ Branch 1 taken 132774 times.
519161 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
253 386387 AVFrameSideData *frame_sd = av_frame_get_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES);
254
255
3/4
✓ Branch 0 taken 254 times.
✓ Branch 1 taken 386133 times.
✓ Branch 2 taken 254 times.
✗ Branch 3 not taken.
386387 if (frame_sd && frame_sd->size >= 10) {
256 254 int skip_samples = AV_RL32(frame_sd->data + 0);
257 254 int discard_padding = AV_RL32(frame_sd->data + 4);
258
259
4/6
✓ Branch 0 taken 212 times.
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 212 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 212 times.
✗ Branch 5 not taken.
254 if (discard_padding > 0 && avctx->frame_size && ec->last_audio_frame) {
260 212 avpkt->duration = av_sat_add64(avpkt->duration, ff_samples_to_time_base(avctx, avctx->initial_padding));
261
2/2
✓ Branch 1 taken 119 times.
✓ Branch 2 taken 93 times.
212 avpkt->duration = FFMIN(avpkt->duration, ff_samples_to_time_base(avctx, avctx->frame_size));
262 212 discard_padding = avctx->frame_size - ff_samples_from_time_base(avctx, avpkt->duration);
263 }
264
265
4/4
✓ Branch 0 taken 212 times.
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 93 times.
✓ Branch 3 taken 119 times.
254 if (skip_samples > 0 || discard_padding > 0) {
266 135 uint8_t *packet_sd = av_packet_new_side_data(avpkt, AV_PKT_DATA_SKIP_SAMPLES, 10);
267
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 135 times.
135 if (!packet_sd)
268 ✗ return AVERROR(ENOMEM);
269 135 AV_WL32A(packet_sd + 0, skip_samples);
270 135 AV_WL32A(packet_sd + 4, discard_padding);
271 135 AV_WL8 (packet_sd + 8, AV_RB8(frame_sd->data + 8));
272 135 AV_WL8 (packet_sd + 9, AV_RB8(frame_sd->data + 9));
273 }
274 }
275 }
276 }
277
278 523748 return 0;
279 }
280
281 531727 int ff_encode_reordered_opaque(AVCodecContext *avctx,
282 AVPacket *pkt, const AVFrame *frame)
283 {
284
2/2
✓ Branch 0 taken 528050 times.
✓ Branch 1 taken 3677 times.
531727 if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) {
285 528050 int ret = av_buffer_replace(&pkt->opaque_ref, frame->opaque_ref);
286
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 528050 times.
528050 if (ret < 0)
287 ✗ return ret;
288 528050 pkt->opaque = frame->opaque;
289 }
290
291 531727 return 0;
292 }
293
294 539740 int ff_encode_encode_cb(AVCodecContext *avctx, AVPacket *avpkt,
295 AVFrame *frame, int *got_packet)
296 {
297 539740 const FFCodec *const codec = ffcodec(avctx->codec);
298 int ret;
299
300 539740 ret = codec->cb.encode(avctx, avpkt, frame, got_packet);
301 ff_assert1_fpu();
302
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 539740 times.
539740 av_assert0(ret <= 0);
303
304
3/4
✓ Branch 0 taken 539740 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 526 times.
✓ Branch 3 taken 539214 times.
539740 if (!ret && *got_packet) {
305
2/2
✓ Branch 0 taken 539135 times.
✓ Branch 1 taken 79 times.
539214 if (avpkt->data) {
306 539135 ret = encode_make_refcounted(avctx, avpkt);
307
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 539135 times.
539135 if (ret < 0)
308 ✗ goto unref;
309 // Date returned by encoders must always be ref-counted
310
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 539135 times.
539135 av_assert0(avpkt->buf);
311 }
312
313 // set the timestamps for the simple no-delay case
314 // encoders with delay have to set the timestamps themselves
315
4/4
✓ Branch 0 taken 31116 times.
✓ Branch 1 taken 508098 times.
✓ Branch 2 taken 30820 times.
✓ Branch 3 taken 296 times.
539214 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) ||
316
2/2
✓ Branch 0 taken 15650 times.
✓ Branch 1 taken 15170 times.
30820 (frame && (codec->caps_internal & FF_CODEC_CAP_EOF_FLUSH))) {
317 523748 ret = encode_set_packet_props(avctx, avpkt, frame);
318
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 523748 times.
523748 if (ret < 0)
319 ✗ goto unref;
320
321 523748 ret = ff_encode_reordered_opaque(avctx, avpkt, frame);
322
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 523748 times.
523748 if (ret < 0)
323 ✗ goto unref;
324 }
325
326 // dts equals pts unless there is reordering
327 // there can be no reordering if there is no encoder delay
328
2/2
✓ Branch 0 taken 8718 times.
✓ Branch 1 taken 530496 times.
539214 if (!(avctx->codec_descriptor->props & AV_CODEC_PROP_REORDER) ||
329
2/2
✓ Branch 0 taken 7953 times.
✓ Branch 1 taken 765 times.
8718 !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) ||
330
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7953 times.
7953 (codec->caps_internal & FF_CODEC_CAP_EOF_FLUSH))
331 531261 avpkt->dts = avpkt->pts;
332 } else {
333 526 unref:
334 526 av_packet_unref(avpkt);
335 }
336
337
2/2
✓ Branch 0 taken 539110 times.
✓ Branch 1 taken 630 times.
539740 if (frame)
338 539110 av_frame_unref(frame);
339
340 539740 return ret;
341 }
342
343 1095763 static int encode_simple_internal(AVCodecContext *avctx, AVPacket *avpkt)
344 {
345 1095763 AVCodecInternal *avci = avctx->internal;
346 1095763 AVFrame *frame = avci->in_frame;
347 1095763 const FFCodec *const codec = ffcodec(avctx->codec);
348 int got_packet;
349 int ret;
350
351
2/2
✓ Branch 0 taken 2093 times.
✓ Branch 1 taken 1093670 times.
1095763 if (avci->draining_done)
352 2093 return AVERROR_EOF;
353
354
3/4
✓ Branch 0 taken 1093670 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1081548 times.
✓ Branch 3 taken 12122 times.
1093670 if (!frame->buf[0] && !avci->draining) {
355 1081548 av_frame_unref(frame);
356 1081548 ret = ff_encode_get_frame(avctx, frame);
357
3/4
✓ Branch 0 taken 542438 times.
✓ Branch 1 taken 539110 times.
✓ Branch 2 taken 542438 times.
✗ Branch 3 not taken.
1081548 if (ret < 0 && ret != AVERROR_EOF)
358 542438 return ret;
359 }
360
361
2/2
✓ Branch 0 taken 12122 times.
✓ Branch 1 taken 539110 times.
551232 if (!frame->buf[0]) {
362
2/2
✓ Branch 0 taken 11492 times.
✓ Branch 1 taken 630 times.
12122 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
363
2/2
✓ Branch 0 taken 6526 times.
✓ Branch 1 taken 4966 times.
11492 avci->frame_thread_encoder))
364 6526 return AVERROR_EOF;
365
366 // Flushing is signaled with a NULL frame
367 5596 frame = NULL;
368 }
369
370 544706 got_packet = 0;
371
372
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 544706 times.
544706 av_assert0(codec->cb_type == FF_CODEC_CB_TYPE_ENCODE);
373
374 #if CONFIG_FRAME_THREAD_ENCODER
375
2/2
✓ Branch 0 taken 64273 times.
✓ Branch 1 taken 480433 times.
544706 if (avci->frame_thread_encoder)
376 /* This will unref frame. */
377 64273 ret = ff_thread_video_encode_frame(avctx, avpkt, frame, &got_packet);
378 else
379 #endif
380 480433 ret = ff_encode_encode_cb(avctx, avpkt, frame, &got_packet);
381
382
4/4
✓ Branch 0 taken 5596 times.
✓ Branch 1 taken 539110 times.
✓ Branch 2 taken 2093 times.
✓ Branch 3 taken 3503 times.
544706 if (avci->draining && !got_packet)
383 2093 avci->draining_done = 1;
384
385 544706 return ret;
386 }
387
388 1090271 static int encode_simple_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
389 {
390 int ret;
391
392
4/4
✓ Branch 0 taken 1095842 times.
✓ Branch 1 taken 539135 times.
✓ Branch 2 taken 1095763 times.
✓ Branch 3 taken 79 times.
1634977 while (!avpkt->data && !avpkt->side_data) {
393 1095763 ret = encode_simple_internal(avctx, avpkt);
394
2/2
✓ Branch 0 taken 551057 times.
✓ Branch 1 taken 544706 times.
1095763 if (ret < 0)
395 551057 return ret;
396 }
397
398 539214 return 0;
399 }
400
401 1096932 static int encode_receive_packet_internal(AVCodecContext *avctx, AVPacket *avpkt)
402 {
403 1096932 AVCodecInternal *avci = avctx->internal;
404 int ret;
405
406
2/2
✓ Branch 0 taken 6661 times.
✓ Branch 1 taken 1090271 times.
1096932 if (avci->draining_done)
407 6661 return AVERROR_EOF;
408
409
2/4
✓ Branch 0 taken 1090271 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1090271 times.
1090271 av_assert0(!avpkt->data && !avpkt->side_data);
410
411
2/2
✓ Branch 0 taken 300557 times.
✓ Branch 1 taken 789714 times.
1090271 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
412
3/4
✓ Branch 0 taken 808 times.
✓ Branch 1 taken 299749 times.
✓ Branch 2 taken 808 times.
✗ Branch 3 not taken.
300557 if ((avctx->flags & AV_CODEC_FLAG_PASS1) && avctx->stats_out)
413 808 avctx->stats_out[0] = '\0';
414 }
415
416
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1090271 times.
1090271 if (ffcodec(avctx->codec)->cb_type == FF_CODEC_CB_TYPE_RECEIVE_PACKET) {
417 ✗ ret = ffcodec(avctx->codec)->cb.receive_packet(avctx, avpkt);
418 ✗ if (ret < 0)
419 ✗ av_packet_unref(avpkt);
420 else
421 // Encoders must always return ref-counted buffers.
422 // Side-data only packets have no data and can be not ref-counted.
423 ✗ av_assert0(!avpkt->data || avpkt->buf);
424 } else
425 1090271 ret = encode_simple_receive_packet(avctx, avpkt);
426
2/2
✓ Branch 0 taken 539214 times.
✓ Branch 1 taken 551057 times.
1090271 if (ret >= 0)
427 539214 avpkt->flags |= encode_ctx(avci)->intra_only_flag;
428
429
2/2
✓ Branch 0 taken 8619 times.
✓ Branch 1 taken 1081652 times.
1090271 if (ret == AVERROR_EOF)
430 8619 avci->draining_done = 1;
431
432 1090271 return ret;
433 }
434
435 #if CONFIG_LCMS2
436 static int encode_generate_icc_profile(AVCodecContext *avctx, AVFrame *frame)
437 {
438 enum AVColorTransferCharacteristic trc = frame->color_trc;
439 enum AVColorPrimaries prim = frame->color_primaries;
440 const FFCodec *const codec = ffcodec(avctx->codec);
441 AVCodecInternal *avci = avctx->internal;
442 cmsHPROFILE profile;
443 int ret;
444
445 /* don't generate ICC profiles if disabled or unsupported */
446 if (!(avctx->flags2 & AV_CODEC_FLAG2_ICC_PROFILES))
447 return 0;
448 if (!(codec->caps_internal & FF_CODEC_CAP_ICC_PROFILES))
449 return 0;
450
451 if (trc == AVCOL_TRC_UNSPECIFIED)
452 trc = avctx->color_trc;
453 if (prim == AVCOL_PRI_UNSPECIFIED)
454 prim = avctx->color_primaries;
455 if (trc == AVCOL_TRC_UNSPECIFIED || prim == AVCOL_PRI_UNSPECIFIED)
456 return 0; /* can't generate ICC profile with missing csp tags */
457
458 if (av_frame_get_side_data(frame, AV_FRAME_DATA_ICC_PROFILE))
459 return 0; /* don't overwrite existing ICC profile */
460
461 if (!avci->icc.avctx) {
462 ret = ff_icc_context_init(&avci->icc, avctx);
463 if (ret < 0)
464 return ret;
465 }
466
467 ret = ff_icc_profile_generate(&avci->icc, prim, trc, &profile);
468 if (ret < 0)
469 return ret;
470
471 ret = ff_icc_profile_attach(&avci->icc, profile, frame);
472 cmsCloseProfile(profile);
473 return ret;
474 }
475 #else /* !CONFIG_LCMS2 */
476 145134 static int encode_generate_icc_profile(av_unused AVCodecContext *c, av_unused AVFrame *f)
477 {
478 145134 return 0;
479 }
480 #endif
481
482 539110 static int encode_send_frame_internal(AVCodecContext *avctx, const AVFrame *src)
483 {
484 539110 AVCodecInternal *avci = avctx->internal;
485 539110 EncodeContext *ec = encode_ctx(avci);
486 539110 AVFrame *dst = avci->buffer_frame;
487 int ret;
488
489
2/2
✓ Branch 0 taken 393976 times.
✓ Branch 1 taken 145134 times.
539110 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
490 /* extract audio service type metadata */
491 393976 AVFrameSideData *sd = av_frame_get_side_data(src, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
492
3/4
✓ Branch 0 taken 642 times.
✓ Branch 1 taken 393334 times.
✓ Branch 2 taken 642 times.
✗ Branch 3 not taken.
393976 if (sd && sd->size >= sizeof(enum AVAudioServiceType))
493 642 avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
494
495 /* check for valid frame size */
496
2/2
✓ Branch 0 taken 107481 times.
✓ Branch 1 taken 286495 times.
393976 if (avctx->frame_size) {
497 /* if we already got an undersized frame, that must have been the last */
498
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 107481 times.
107481 if (ec->last_audio_frame) {
499 ✗ av_log(avctx, AV_LOG_ERROR, "frame_size (%d) was not respected for a non-last frame\n", avctx->frame_size);
500 ✗ return AVERROR(EINVAL);
501 }
502
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 107481 times.
107481 if (src->nb_samples > avctx->frame_size) {
503 ✗ av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) > frame_size (%d)\n", src->nb_samples, avctx->frame_size);
504 ✗ return AVERROR(EINVAL);
505 }
506
2/2
✓ Branch 0 taken 301 times.
✓ Branch 1 taken 107180 times.
107481 if (src->nb_samples < avctx->frame_size) {
507 301 ec->last_audio_frame = 1;
508
2/2
✓ Branch 0 taken 145 times.
✓ Branch 1 taken 156 times.
301 if (!(avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) ||
509
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 85 times.
145 (avctx->flags2 & AV_CODEC_FLAG2_FIXED_FRAME_SIZE)) {
510
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 213 times.
216 int pad_samples = avci->pad_samples ? avci->pad_samples : avctx->frame_size;
511 216 int out_samples = (src->nb_samples + pad_samples - 1) / pad_samples * pad_samples;
512
513
1/2
✓ Branch 0 taken 216 times.
✗ Branch 1 not taken.
216 if (out_samples != src->nb_samples) {
514 216 ret = pad_last_frame(avctx, dst, src, out_samples);
515
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 216 times.
216 if (ret < 0)
516 ✗ return ret;
517 216 goto finish;
518 }
519 }
520 }
521 }
522
2/2
✓ Branch 0 taken 27898 times.
✓ Branch 1 taken 117236 times.
145134 } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE)) {
523 27898 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(src->format);
524
525
2/4
✓ Branch 0 taken 27898 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27898 times.
✗ Branch 3 not taken.
27898 if (!(desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL) &&
526
1/2
✓ Branch 0 taken 27898 times.
✗ Branch 1 not taken.
27898 (src->format != avctx->pix_fmt ||
527
2/4
✓ Branch 0 taken 27898 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 27898 times.
27898 src->width < avctx->width || src->height < avctx->height)) {
528 ✗ av_log(avctx, AV_LOG_ERROR, "Frame size or pixel format does not match the encoder\n");
529 ✗ return AVERROR(EINVAL);
530 }
531 }
532
533 538894 ret = av_frame_ref(dst, src);
534
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 538894 times.
538894 if (ret < 0)
535 ✗ return ret;
536
537 538894 finish:
538
539
2/2
✓ Branch 0 taken 145134 times.
✓ Branch 1 taken 393976 times.
539110 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
540 145134 ret = encode_generate_icc_profile(avctx, dst);
541
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 145134 times.
145134 if (ret < 0)
542 ✗ return ret;
543 }
544
545 // unset frame duration unless AV_CODEC_FLAG_FRAME_DURATION is set,
546 // since otherwise we cannot be sure that whatever value it has is in the
547 // right timebase, so we would produce an incorrect value, which is worse
548 // than none at all
549
2/2
✓ Branch 0 taken 3309 times.
✓ Branch 1 taken 535801 times.
539110 if (!(avctx->flags & AV_CODEC_FLAG_FRAME_DURATION))
550 3309 dst->duration = 0;
551
552 539110 return 0;
553 }
554
555 547731 int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
556 {
557 547731 AVCodecInternal *avci = avctx->internal;
558 int ret;
559
560
2/4
✓ Branch 1 taken 547731 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 547731 times.
547731 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
561 ✗ return AVERROR(EINVAL);
562
563
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 547729 times.
547731 if (avci->draining)
564 2 return AVERROR_EOF;
565
566
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 547729 times.
547729 if (avci->buffer_frame->buf[0])
567 ✗ return AVERROR(EAGAIN);
568
569
2/2
✓ Branch 0 taken 8619 times.
✓ Branch 1 taken 539110 times.
547729 if (!frame) {
570 8619 avci->draining = 1;
571 } else {
572 539110 ret = encode_send_frame_internal(avctx, frame);
573
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 539110 times.
539110 if (ret < 0)
574 ✗ return ret;
575 }
576
577
2/4
✓ Branch 0 taken 547729 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 547729 times.
✗ Branch 3 not taken.
547729 if (!avci->buffer_pkt->data && !avci->buffer_pkt->side_data) {
578 547729 ret = encode_receive_packet_internal(avctx, avci->buffer_pkt);
579
5/6
✓ Branch 0 taken 10060 times.
✓ Branch 1 taken 537669 times.
✓ Branch 2 taken 6661 times.
✓ Branch 3 taken 3399 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 6661 times.
547729 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
580 ✗ return ret;
581 }
582
583 547729 avctx->frame_num++;
584
585 547729 return 0;
586 }
587
588 1086872 int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
589 {
590 1086872 AVCodecInternal *avci = avctx->internal;
591 int ret;
592
593 1086872 av_packet_unref(avpkt);
594
595
2/4
✓ Branch 1 taken 1086872 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1086872 times.
1086872 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
596 ✗ return AVERROR(EINVAL);
597
598
4/4
✓ Branch 0 taken 549282 times.
✓ Branch 1 taken 537590 times.
✓ Branch 2 taken 79 times.
✓ Branch 3 taken 549203 times.
1086872 if (avci->buffer_pkt->data || avci->buffer_pkt->side_data) {
599 537669 av_packet_move_ref(avpkt, avci->buffer_pkt);
600 } else {
601 549203 ret = encode_receive_packet_internal(avctx, avpkt);
602
2/2
✓ Branch 0 taken 547658 times.
✓ Branch 1 taken 1545 times.
549203 if (ret < 0)
603 547658 return ret;
604 }
605
606 539214 return 0;
607 }
608
609 ✗ av_cold int ff_encode_reconf_parse_dict(AVCodecContext *avctx, AVDictionary **dict)
610 {
611 ✗ const AVCodec *codec = avctx->codec;
612 ✗ const FFCodec *codec2 = ffcodec(codec);
613 ✗ const AVDictionaryEntry *t = NULL;
614 ✗ AVDictionary *copy = NULL;
615 int ret;
616
617 ✗ av_assert0(av_codec_is_encoder(codec) && (codec->capabilities & AV_CODEC_CAP_ENCODER_RECONF));
618
619 ✗ ret = av_dict_copy(&copy, *dict, 0);
620 ✗ if (ret < 0)
621 ✗ goto end;
622
623 // Remove the dictionary entries that would be applied to the private codec context
624 ✗ if (codec->priv_class) {
625 ✗ while ((t = av_dict_iterate(*dict, t))) {
626 ✗ if (av_opt_find(avctx->priv_data, t->key, NULL,
627 AV_OPT_FLAG_RUNTIME_PARAM, 0))
628 ✗ av_dict_set(dict, t->key, NULL, 0);
629 }
630 }
631
632 // Ditto for global options
633 ✗ if (codec2->defaults) {
634 ✗ const FFCodecDefault *d = codec2->defaults;
635 ✗ while (d->key) {
636 ✗ if (d->flags & AV_OPT_FLAG_RUNTIME_PARAM)
637 ✗ av_dict_set(dict, d->key, NULL, 0);
638 ✗ d++;
639 }
640 }
641
642 // If any entry remains, then the requested option/s don't exist or are not settable.
643 ✗ if (av_dict_count(*dict)) {
644 ✗ ret = AVERROR_OPTION_NOT_FOUND;
645 ✗ goto end;
646 }
647
648 ✗ ret = av_dict_copy(dict, copy, 0);
649 ✗ if (ret < 0)
650 ✗ goto end;
651
652 // Do a dry run of applying the options, to ensure the encoder is unchanged in case
653 // one of them has an invalid value.
654 // This is done twice, once for avctx and once for the AVCodec, because using the
655 // search children flag in combination with the fake obj flag will iterate through
656 // the options from all compiled in codecs if you pass the avctx class.
657 ✗ if (codec->priv_class) {
658 ✗ ret = av_opt_set_dict2((void *)&codec->priv_class, &copy, AV_OPT_SEARCH_FAKE_OBJ);
659 ✗ if (ret < 0)
660 ✗ goto end;
661 }
662 ✗ ret = av_opt_set_dict2((void *)&avctx->av_class, &copy, AV_OPT_SEARCH_FAKE_OBJ);
663 ✗ if (ret < 0)
664 ✗ goto end;
665
666 // The dictionary should be empty.
667 ✗ av_assert0(!av_dict_count(copy));
668
669 ✗ ret = av_opt_set_dict2(avctx, dict, AV_OPT_SEARCH_CHILDREN);
670 ✗ if (ret < 0)
671 ✗ goto end;
672
673 // The dictionary should be empty.
674 ✗ av_assert0(!av_dict_count(*dict));
675
676 ✗ ret = 0;
677 ✗ end:
678 ✗ av_dict_free(&copy);
679
680 ✗ return ret;
681 }
682
683 ✗ av_cold int avcodec_encode_reconfigure(AVCodecContext *avctx, AVDictionary **dict)
684 {
685 ✗ const FFCodec *codec = ffcodec(avctx->codec);
686 ✗ int ret = AVERROR_BUG;
687
688 ✗ if (!dict || !*dict || !avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
689 ✗ return AVERROR(EINVAL);
690
691 ✗ if (!(avctx->codec->capabilities & AV_CODEC_CAP_ENCODER_RECONF)) {
692 ✗ av_log(avctx, AV_LOG_ERROR, "This encoder does not support reconfiguration\n");
693 ✗ return AVERROR(ENOSYS);
694 }
695
696 ✗ if (codec->reconf)
697 ✗ ret = codec->reconf(avctx, dict);
698 else
699 ✗ ret = ff_encode_reconf_parse_dict(avctx, dict);
700 ✗ if (ret < 0)
701 ✗ return ret;
702
703 ✗ return 0;
704 }
705
706 21065 static int encode_preinit_video(AVCodecContext *avctx)
707 {
708 21065 const AVCodec *c = avctx->codec;
709 21065 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt);
710 const enum AVPixelFormat *pix_fmts;
711 int ret, i, num_pix_fmts;
712
713
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21065 times.
21065 if (!pixdesc) {
714 ✗ av_log(avctx, AV_LOG_ERROR, "Invalid video pixel format: %d\n",
715 ✗ avctx->pix_fmt);
716 ✗ return AVERROR(EINVAL);
717 }
718
719 21065 ret = avcodec_get_supported_config(avctx, NULL, AV_CODEC_CONFIG_PIX_FORMAT,
720 0, (const void **) &pix_fmts, &num_pix_fmts);
721
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21065 times.
21065 if (ret < 0)
722 ✗ return ret;
723
724
2/2
✓ Branch 0 taken 866 times.
✓ Branch 1 taken 20199 times.
21065 if (pix_fmts) {
725
1/2
✓ Branch 0 taken 2760 times.
✗ Branch 1 not taken.
2760 for (i = 0; i < num_pix_fmts; i++)
726
2/2
✓ Branch 0 taken 866 times.
✓ Branch 1 taken 1894 times.
2760 if (avctx->pix_fmt == pix_fmts[i])
727 866 break;
728
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 866 times.
866 if (i == num_pix_fmts) {
729 ✗ av_log(avctx, AV_LOG_ERROR,
730 "Specified pixel format %s is not supported by the %s encoder.\n",
731 ✗ av_get_pix_fmt_name(avctx->pix_fmt), c->name);
732
733 ✗ av_log(avctx, AV_LOG_ERROR, "Supported pixel formats:\n");
734 ✗ for (int p = 0; pix_fmts[p] != AV_PIX_FMT_NONE; p++) {
735 ✗ av_log(avctx, AV_LOG_ERROR, " %s\n",
736 ✗ av_get_pix_fmt_name(pix_fmts[p]));
737 }
738
739 ✗ return AVERROR(EINVAL);
740 }
741
2/2
✓ Branch 0 taken 841 times.
✓ Branch 1 taken 25 times.
866 if (pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
742
1/2
✓ Branch 0 taken 841 times.
✗ Branch 1 not taken.
841 pix_fmts[i] == AV_PIX_FMT_YUVJ411P ||
743
2/2
✓ Branch 0 taken 837 times.
✓ Branch 1 taken 4 times.
841 pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
744
1/2
✓ Branch 0 taken 837 times.
✗ Branch 1 not taken.
837 pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
745
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 827 times.
837 pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
746 39 avctx->color_range = AVCOL_RANGE_JPEG;
747 }
748
749
2/2
✓ Branch 0 taken 2591 times.
✓ Branch 1 taken 18474 times.
21065 if (pixdesc->flags & AV_PIX_FMT_FLAG_ALPHA) {
750 const enum AVAlphaMode *alpha_modes;
751 int num_alpha_modes;
752 2591 ret = avcodec_get_supported_config(avctx, NULL, AV_CODEC_CONFIG_ALPHA_MODE,
753 0, (const void **) &alpha_modes, &num_alpha_modes);
754
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2591 times.
2591 if (ret < 0)
755 ✗ return ret;
756
757
4/4
✓ Branch 0 taken 533 times.
✓ Branch 1 taken 2058 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 529 times.
2591 if (avctx->alpha_mode != AVALPHA_MODE_UNSPECIFIED && alpha_modes) {
758
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 for (i = 0; i < num_alpha_modes; i++) {
759
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (avctx->alpha_mode == alpha_modes[i])
760 4 break;
761 }
762
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (i == num_alpha_modes) {
763 ✗ av_log(avctx, AV_LOG_ERROR,
764 "Specified alpha mode '%s' is not supported by the %s encoder.\n",
765 ✗ av_alpha_mode_name(avctx->alpha_mode), c->name);
766 ✗ av_log(avctx, AV_LOG_ERROR, "Supported alpha modes:\n");
767 ✗ for (int p = 0; alpha_modes[p] != AVALPHA_MODE_UNSPECIFIED; p++) {
768 ✗ av_log(avctx, AV_LOG_ERROR, " %s\n",
769 ✗ av_alpha_mode_name(alpha_modes[p]));
770 }
771 ✗ return AVERROR(EINVAL);
772 }
773 }
774 }
775
776
1/2
✓ Branch 0 taken 21065 times.
✗ Branch 1 not taken.
21065 if ( avctx->bits_per_raw_sample < 0
777
3/4
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 20982 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 83 times.
21065 || (avctx->bits_per_raw_sample > 8 && pixdesc->comp[0].depth <= 8)) {
778 ✗ av_log(avctx, AV_LOG_WARNING, "Specified bit depth %d not possible with the specified pixel formats depth %d\n",
779 ✗ avctx->bits_per_raw_sample, pixdesc->comp[0].depth);
780 ✗ avctx->bits_per_raw_sample = pixdesc->comp[0].depth;
781 }
782
2/4
✓ Branch 0 taken 21065 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 21065 times.
21065 if (avctx->width <= 0 || avctx->height <= 0) {
783 ✗ av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
784 ✗ return AVERROR(EINVAL);
785 }
786
787
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21065 times.
21065 if (avctx->hw_frames_ctx) {
788 ✗ AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
789 ✗ if (frames_ctx->format != avctx->pix_fmt) {
790 ✗ av_log(avctx, AV_LOG_ERROR,
791 "Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format\n");
792 ✗ return AVERROR(EINVAL);
793 }
794 ✗ if (avctx->sw_pix_fmt != AV_PIX_FMT_NONE &&
795 ✗ avctx->sw_pix_fmt != frames_ctx->sw_format) {
796 ✗ av_log(avctx, AV_LOG_ERROR,
797 "Mismatching AVCodecContext.sw_pix_fmt (%s) "
798 "and AVHWFramesContext.sw_format (%s)\n",
799 av_get_pix_fmt_name(avctx->sw_pix_fmt),
800 av_get_pix_fmt_name(frames_ctx->sw_format));
801 ✗ return AVERROR(EINVAL);
802 }
803 ✗ avctx->sw_pix_fmt = frames_ctx->sw_format;
804 }
805
806 21065 return 0;
807 }
808
809 1636 static int encode_preinit_audio(AVCodecContext *avctx)
810 {
811 1636 const AVCodec *c = avctx->codec;
812 const enum AVSampleFormat *sample_fmts;
813 const int *supported_samplerates;
814 const AVChannelLayout *ch_layouts;
815 int ret, i, num_sample_fmts, num_samplerates, num_ch_layouts;
816
817
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1636 times.
1636 if (!av_get_sample_fmt_name(avctx->sample_fmt)) {
818 ✗ av_log(avctx, AV_LOG_ERROR, "Invalid audio sample format: %d\n",
819 ✗ avctx->sample_fmt);
820 ✗ return AVERROR(EINVAL);
821 }
822
823 1636 ret = avcodec_get_supported_config(avctx, NULL, AV_CODEC_CONFIG_SAMPLE_FORMAT,
824 0, (const void **) &sample_fmts,
825 &num_sample_fmts);
826
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1636 times.
1636 if (ret < 0)
827 ✗ return ret;
828
1/2
✓ Branch 0 taken 1636 times.
✗ Branch 1 not taken.
1636 if (sample_fmts) {
829
1/2
✓ Branch 0 taken 1655 times.
✗ Branch 1 not taken.
1655 for (i = 0; i < num_sample_fmts; i++) {
830
2/2
✓ Branch 0 taken 1636 times.
✓ Branch 1 taken 19 times.
1655 if (avctx->sample_fmt == sample_fmts[i])
831 1636 break;
832
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 &&
833 5 av_get_planar_sample_fmt(avctx->sample_fmt) ==
834 5 av_get_planar_sample_fmt(sample_fmts[i])) {
835 ✗ avctx->sample_fmt = sample_fmts[i];
836 ✗ break;
837 }
838 }
839
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1636 times.
1636 if (i == num_sample_fmts) {
840 ✗ av_log(avctx, AV_LOG_ERROR,
841 "Specified sample format %s is not supported by the %s encoder\n",
842 ✗ av_get_sample_fmt_name(avctx->sample_fmt), c->name);
843
844 ✗ av_log(avctx, AV_LOG_ERROR, "Supported sample formats:\n");
845 ✗ for (int p = 0; sample_fmts[p] != AV_SAMPLE_FMT_NONE; p++) {
846 ✗ av_log(avctx, AV_LOG_ERROR, " %s\n",
847 ✗ av_get_sample_fmt_name(sample_fmts[p]));
848 }
849
850 ✗ return AVERROR(EINVAL);
851 }
852 }
853
854 1636 ret = avcodec_get_supported_config(avctx, NULL, AV_CODEC_CONFIG_SAMPLE_RATE,
855 0, (const void **) &supported_samplerates,
856 &num_samplerates);
857
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1636 times.
1636 if (ret < 0)
858 ✗ return ret;
859
2/2
✓ Branch 0 taken 208 times.
✓ Branch 1 taken 1428 times.
1636 if (supported_samplerates) {
860
1/2
✓ Branch 0 taken 889 times.
✗ Branch 1 not taken.
889 for (i = 0; i < num_samplerates; i++)
861
2/2
✓ Branch 0 taken 208 times.
✓ Branch 1 taken 681 times.
889 if (avctx->sample_rate == supported_samplerates[i])
862 208 break;
863
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 208 times.
208 if (i == num_samplerates) {
864 ✗ av_log(avctx, AV_LOG_ERROR,
865 "Specified sample rate %d is not supported by the %s encoder\n",
866 ✗ avctx->sample_rate, c->name);
867
868 ✗ av_log(avctx, AV_LOG_ERROR, "Supported sample rates:\n");
869 ✗ for (int p = 0; supported_samplerates[p]; p++)
870 ✗ av_log(avctx, AV_LOG_ERROR, " %d\n", supported_samplerates[p]);
871
872 ✗ return AVERROR(EINVAL);
873 }
874 }
875 1636 ret = avcodec_get_supported_config(avctx, NULL, AV_CODEC_CONFIG_CHANNEL_LAYOUT,
876 0, (const void **) &ch_layouts, &num_ch_layouts);
877
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1636 times.
1636 if (ret < 0)
878 ✗ return ret;
879
2/2
✓ Branch 0 taken 213 times.
✓ Branch 1 taken 1423 times.
1636 if (ch_layouts) {
880
1/2
✓ Branch 0 taken 334 times.
✗ Branch 1 not taken.
334 for (i = 0; i < num_ch_layouts; i++) {
881
2/2
✓ Branch 1 taken 213 times.
✓ Branch 2 taken 121 times.
334 if (!av_channel_layout_compare(&avctx->ch_layout, &ch_layouts[i]))
882 213 break;
883 }
884
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 213 times.
213 if (i == num_ch_layouts) {
885 char buf[512];
886 ✗ int ret = av_channel_layout_describe(&avctx->ch_layout, buf, sizeof(buf));
887 ✗ av_log(avctx, AV_LOG_ERROR,
888 "Specified channel layout '%s' is not supported by the %s encoder\n",
889 ✗ ret > 0 ? buf : "?", c->name);
890
891 ✗ av_log(avctx, AV_LOG_ERROR, "Supported channel layouts:\n");
892 ✗ for (int p = 0; ch_layouts[p].nb_channels; p++) {
893 ✗ ret = av_channel_layout_describe(&ch_layouts[p], buf, sizeof(buf));
894 ✗ av_log(avctx, AV_LOG_ERROR, " %s\n", ret > 0 ? buf : "?");
895 }
896 ✗ return AVERROR(EINVAL);
897 }
898 }
899
900
2/2
✓ Branch 0 taken 1579 times.
✓ Branch 1 taken 57 times.
1636 if (!avctx->bits_per_raw_sample)
901 1579 avctx->bits_per_raw_sample = av_get_exact_bits_per_sample(avctx->codec_id);
902
2/2
✓ Branch 0 taken 332 times.
✓ Branch 1 taken 1304 times.
1636 if (!avctx->bits_per_raw_sample)
903 332 avctx->bits_per_raw_sample = 8 * av_get_bytes_per_sample(avctx->sample_fmt);
904
905 1636 return 0;
906 }
907
908 22747 int ff_encode_preinit(AVCodecContext *avctx)
909 {
910 22747 AVCodecInternal *avci = avctx->internal;
911 22747 EncodeContext *ec = encode_ctx(avci);
912 22747 int ret = 0;
913
914
2/4
✓ Branch 0 taken 22747 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 22747 times.
22747 if (avctx->time_base.num <= 0 || avctx->time_base.den <= 0) {
915 ✗ av_log(avctx, AV_LOG_ERROR, "The encoder timebase is not set.\n");
916 ✗ return AVERROR(EINVAL);
917 }
918
919
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22747 times.
22747 if (avctx->bit_rate < 0) {
920 ✗ av_log(avctx, AV_LOG_ERROR, "The encoder bitrate is negative.\n");
921 ✗ return AVERROR(EINVAL);
922 }
923
924
2/2
✓ Branch 0 taken 22620 times.
✓ Branch 1 taken 127 times.
22747 if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE &&
925
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22620 times.
22620 !(avctx->codec->capabilities & AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE)) {
926 ✗ av_log(avctx, AV_LOG_ERROR, "The copy_opaque flag is set, but the "
927 "encoder does not support it.\n");
928 ✗ return AVERROR(EINVAL);
929 }
930
931
3/3
✓ Branch 0 taken 21065 times.
✓ Branch 1 taken 1636 times.
✓ Branch 2 taken 46 times.
22747 switch (avctx->codec_type) {
932 21065 case AVMEDIA_TYPE_VIDEO: ret = encode_preinit_video(avctx); break;
933 1636 case AVMEDIA_TYPE_AUDIO: ret = encode_preinit_audio(avctx); break;
934 }
935
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22747 times.
22747 if (ret < 0)
936 ✗ return ret;
937
938
4/4
✓ Branch 0 taken 1682 times.
✓ Branch 1 taken 21065 times.
✓ Branch 2 taken 1636 times.
✓ Branch 3 taken 46 times.
22747 if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
939
3/4
✓ Branch 0 taken 22658 times.
✓ Branch 1 taken 43 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 22658 times.
22701 && avctx->bit_rate>0 && avctx->bit_rate<1000) {
940 ✗ av_log(avctx, AV_LOG_WARNING, "Bitrate %"PRId64" is extremely low, maybe you mean %"PRId64"k\n", avctx->bit_rate, avctx->bit_rate);
941 }
942
943
2/2
✓ Branch 0 taken 22745 times.
✓ Branch 1 taken 2 times.
22747 if (!avctx->rc_initial_buffer_occupancy)
944 22745 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3LL / 4;
945
946
2/2
✓ Branch 0 taken 22277 times.
✓ Branch 1 taken 470 times.
22747 if (avctx->codec_descriptor->props & AV_CODEC_PROP_INTRA_ONLY)
947 22277 ec->intra_only_flag = AV_PKT_FLAG_KEY;
948
949
2/2
✓ Branch 1 taken 22701 times.
✓ Branch 2 taken 46 times.
22747 if (ffcodec(avctx->codec)->cb_type == FF_CODEC_CB_TYPE_ENCODE) {
950 22701 avci->in_frame = av_frame_alloc();
951
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22701 times.
22701 if (!avci->in_frame)
952 ✗ return AVERROR(ENOMEM);
953 }
954
955
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 22741 times.
22747 if ((avctx->flags & AV_CODEC_FLAG_RECON_FRAME)) {
956
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!(avctx->codec->capabilities & AV_CODEC_CAP_ENCODER_RECON_FRAME)) {
957 ✗ av_log(avctx, AV_LOG_ERROR, "Reconstructed frame output requested "
958 "from an encoder not supporting it\n");
959 ✗ return AVERROR(ENOSYS);
960 }
961
962 6 avci->recon_frame = av_frame_alloc();
963
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!avci->recon_frame)
964 ✗ return AVERROR(ENOMEM);
965 }
966
967
2/2
✓ Branch 0 taken 272964 times.
✓ Branch 1 taken 22747 times.
295711 for (int i = 0; ff_sd_global_map[i].packet < AV_PKT_DATA_NB; i++) {
968 272964 const enum AVPacketSideDataType type_packet = ff_sd_global_map[i].packet;
969 272964 const enum AVFrameSideDataType type_frame = ff_sd_global_map[i].frame;
970 const AVFrameSideData *sd_frame;
971 AVPacketSideData *sd_packet;
972
973 272964 sd_frame = av_frame_side_data_get(avctx->decoded_side_data,
974 avctx->nb_decoded_side_data,
975 type_frame);
976
3/4
✓ Branch 0 taken 281 times.
✓ Branch 1 taken 272683 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 281 times.
273245 if (!sd_frame ||
977 281 av_packet_side_data_get(avctx->coded_side_data, avctx->nb_coded_side_data,
978 type_packet))
979
980 272683 continue;
981
982 281 sd_packet = av_packet_side_data_new(&avctx->coded_side_data, &avctx->nb_coded_side_data,
983 281 type_packet, sd_frame->size, 0);
984
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 281 times.
281 if (!sd_packet)
985 ✗ return AVERROR(ENOMEM);
986
987 281 memcpy(sd_packet->data, sd_frame->data, sd_frame->size);
988 }
989
990 #if CONFIG_FRAME_THREAD_ENCODER
991 22747 ret = ff_frame_thread_encoder_init(avctx);
992
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22747 times.
22747 if (ret < 0)
993 ✗ return ret;
994 #endif
995
996 22747 return 0;
997 }
998
999 12973 int ff_encode_alloc_frame(AVCodecContext *avctx, AVFrame *frame)
1000 {
1001 int ret;
1002
1003 av_assert1(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
1004
1005 12973 frame->format = avctx->pix_fmt;
1006
3/4
✓ Branch 0 taken 12957 times.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12957 times.
12973 if (frame->width <= 0 || frame->height <= 0) {
1007 16 frame->width = avctx->width;
1008 16 frame->height = avctx->height;
1009 }
1010
1011 12973 ret = avcodec_default_get_buffer2(avctx, frame, 0);
1012
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12973 times.
12973 if (ret < 0) {
1013 ✗ av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1014 ✗ av_frame_unref(frame);
1015 ✗ return ret;
1016 }
1017
1018 12973 return 0;
1019 }
1020
1021 104 int ff_encode_receive_frame(AVCodecContext *avctx, AVFrame *frame)
1022 {
1023 104 AVCodecInternal *avci = avctx->internal;
1024
1025
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
104 if (!avci->recon_frame)
1026 ✗ return AVERROR(EINVAL);
1027
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
104 if (!avci->recon_frame->buf[0])
1028 ✗ return avci->draining_done ? AVERROR_EOF : AVERROR(EAGAIN);
1029
1030 104 av_frame_move_ref(frame, avci->recon_frame);
1031 104 return 0;
1032 }
1033
1034 ✗ void ff_encode_flush_buffers(AVCodecContext *avctx)
1035 {
1036 ✗ AVCodecInternal *avci = avctx->internal;
1037
1038 ✗ if (avci->in_frame)
1039 ✗ av_frame_unref(avci->in_frame);
1040 ✗ if (avci->recon_frame)
1041 ✗ av_frame_unref(avci->recon_frame);
1042 ✗ }
1043
1044 22747 AVCodecInternal *ff_encode_internal_alloc(void)
1045 {
1046 22747 return av_mallocz(sizeof(EncodeContext));
1047 }
1048
1049 245 AVCPBProperties *ff_encode_add_cpb_side_data(AVCodecContext *avctx)
1050 {
1051 AVPacketSideData *tmp;
1052 AVCPBProperties *props;
1053 size_t size;
1054 int i;
1055
1056
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 245 times.
266 for (i = 0; i < avctx->nb_coded_side_data; i++)
1057
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (avctx->coded_side_data[i].type == AV_PKT_DATA_CPB_PROPERTIES)
1058 ✗ return (AVCPBProperties *)avctx->coded_side_data[i].data;
1059
1060 245 props = av_cpb_properties_alloc(&size);
1061
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 245 times.
245 if (!props)
1062 ✗ return NULL;
1063
1064 245 tmp = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*tmp));
1065
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 245 times.
245 if (!tmp) {
1066 ✗ av_freep(&props);
1067 ✗ return NULL;
1068 }
1069
1070 245 avctx->coded_side_data = tmp;
1071 245 avctx->nb_coded_side_data++;
1072
1073 245 avctx->coded_side_data[avctx->nb_coded_side_data - 1].type = AV_PKT_DATA_CPB_PROPERTIES;
1074 245 avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props;
1075 245 avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size;
1076
1077 245 return props;
1078 }
1079
1080 13417 int ff_encode_add_stats_side_data(AVPacket *pkt, int quality, const int64_t error[],
1081 int error_count, enum AVPictureType pict_type)
1082 {
1083 uint8_t *side_data;
1084 size_t side_data_size;
1085
1086 13417 side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS, &side_data_size);
1087
1/2
✓ Branch 0 taken 13417 times.
✗ Branch 1 not taken.
13417 if (!side_data) {
1088 13417 side_data_size = 4+4+8*error_count;
1089 13417 side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
1090 side_data_size);
1091 }
1092
1093
2/4
✓ Branch 0 taken 13417 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 13417 times.
13417 if (!side_data || side_data_size < 4+4+8*error_count)
1094 ✗ return AVERROR(ENOMEM);
1095
1096 13417 AV_WL32(side_data, quality);
1097 13417 side_data[4] = pict_type;
1098 13417 side_data[5] = error_count;
1099
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13417 times.
13417 for (int i = 0; i < error_count; ++i)
1100 ✗ AV_WL64(side_data+8 + 8*i , error[i]);
1101
1102 13417 return 0;
1103 }
1104
1105 1849 int ff_check_codec_matrices(AVCodecContext *avctx, unsigned types, uint16_t min, uint16_t max)
1106 {
1107 1849 uint16_t *matrices[] = {avctx->intra_matrix, avctx->inter_matrix, avctx->chroma_intra_matrix};
1108 1849 const char *names[] = {"Intra", "Inter", "Chroma Intra"};
1109 static_assert(FF_ARRAY_ELEMS(matrices) == FF_ARRAY_ELEMS(names), "matrix count mismatch");
1110
2/2
✓ Branch 0 taken 5547 times.
✓ Branch 1 taken 1849 times.
7396 for (int m = 0; m < FF_ARRAY_ELEMS(matrices); m++) {
1111 5547 uint16_t *matrix = matrices[m];
1112
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5547 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5547 if (matrix && (types & (1U << m))) {
1113 ✗ for (int i = 0; i < 64; i++) {
1114 ✗ if (matrix[i] < min || matrix[i] > max) {
1115 ✗ 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);
1116 ✗ return AVERROR(EINVAL);
1117 }
1118 }
1119 }
1120 }
1121 1849 return 0;
1122 }
1123