../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
391 / 603
64.8%
Branches:
300 / 511
58.7%
Functions:
27 / 30
90.0%

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