FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/encode.c
Date: 2024-07-14 13:34:57
Exec Total Coverage
Lines: 327 468 69.9%
Functions: 24 25 96.0%
Branches: 250 386 64.8%

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/attributes.h"
22 #include "libavutil/avassert.h"
23 #include "libavutil/channel_layout.h"
24 #include "libavutil/emms.h"
25 #include "libavutil/frame.h"
26 #include "libavutil/imgutils.h"
27 #include "libavutil/internal.h"
28 #include "libavutil/mem.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 844111 static EncodeContext *encode_ctx(AVCodecInternal *avci)
58 {
59 844111 return (EncodeContext*)avci;
60 }
61
62 36272 int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
63 {
64
2/4
✓ Branch 0 taken 36272 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 36272 times.
36272 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 36272 times.
36272 av_assert0(!avpkt->data);
71
72 36272 av_fast_padded_malloc(&avctx->internal->byte_buffer,
73 36272 &avctx->internal->byte_buffer_size, size);
74 36272 avpkt->data = avctx->internal->byte_buffer;
75
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36272 times.
36272 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 36272 avpkt->size = size;
80
81 36272 return 0;
82 }
83
84 409247 int avcodec_default_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int flags)
85 {
86 int ret;
87
88
2/4
✓ Branch 0 taken 409247 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 409247 times.
409247 if (avpkt->size < 0 || avpkt->size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
89 return AVERROR(EINVAL);
90
91
2/4
✓ Branch 0 taken 409247 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 409247 times.
409247 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 409247 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
97
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 409247 times.
409247 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 409247 avpkt->data = avpkt->buf->data;
102
103 409247 return 0;
104 }
105
106 409247 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 409247 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 409247 times.
409247 if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
111 return AVERROR(EINVAL);
112
113
2/4
✓ Branch 0 taken 409247 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 409247 times.
409247 av_assert0(!avpkt->data && !avpkt->buf);
114
115 409247 avpkt->size = size;
116 409247 ret = avctx->get_encode_buffer(avctx, avpkt, flags);
117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 409247 times.
409247 if (ret < 0)
118 goto fail;
119
120
2/4
✓ Branch 0 taken 409247 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 409247 times.
409247 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 409247 memset(avpkt->data + avpkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
126
127 409247 ret = 0;
128 409247 fail:
129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 409247 times.
409247 if (ret < 0) {
130 av_log(avctx, AV_LOG_ERROR, "get_encode_buffer() failed\n");
131 av_packet_unref(avpkt);
132 }
133
134 409247 return ret;
135 }
136
137 409311 static int encode_make_refcounted(AVCodecContext *avctx, AVPacket *avpkt)
138 {
139 409311 uint8_t *data = avpkt->data;
140 int ret;
141
142
2/2
✓ Branch 0 taken 373039 times.
✓ Branch 1 taken 36272 times.
409311 if (avpkt->buf)
143 373039 return 0;
144
145 36272 avpkt->data = NULL;
146 36272 ret = ff_get_encode_buffer(avctx, avpkt, avpkt->size, 0);
147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36272 times.
36272 if (ret < 0)
148 return ret;
149 36272 memcpy(avpkt->data, data, avpkt->size);
150
151 36272 return 0;
152 }
153
154 /**
155 * Pad last frame with silence.
156 */
157 54 static int pad_last_frame(AVCodecContext *s, AVFrame *frame, const AVFrame *src, int out_samples)
158 {
159 int ret;
160
161 54 frame->format = src->format;
162 54 frame->nb_samples = out_samples;
163 54 ret = av_channel_layout_copy(&frame->ch_layout, &s->ch_layout);
164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
54 if (ret < 0)
165 goto fail;
166 54 ret = av_frame_get_buffer(frame, 0);
167
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
54 if (ret < 0)
168 goto fail;
169
170 54 ret = av_frame_copy_props(frame, src);
171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
54 if (ret < 0)
172 goto fail;
173
174
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
54 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
175 54 src->nb_samples, s->ch_layout.nb_channels,
176 s->sample_fmt)) < 0)
177 goto fail;
178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
54 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
179 54 frame->nb_samples - src->nb_samples,
180 s->ch_layout.nb_channels, s->sample_fmt)) < 0)
181 goto fail;
182
183 54 return 0;
184
185 fail:
186 av_frame_unref(frame);
187 encode_ctx(s->internal)->last_audio_frame = 0;
188 return ret;
189 }
190
191 822 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
192 const AVSubtitle *sub)
193 {
194 int ret;
195
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 822 times.
822 if (sub->start_display_time) {
196 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
197 return -1;
198 }
199
200 822 ret = ffcodec(avctx->codec)->cb.encode_sub(avctx, buf, buf_size, sub);
201 822 avctx->frame_num++;
202 822 return ret;
203 }
204
205 842776 int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame)
206 {
207 842776 AVCodecInternal *avci = avctx->internal;
208
209
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 842776 times.
842776 if (avci->draining)
210 return AVERROR_EOF;
211
212
2/2
✓ Branch 0 taken 427336 times.
✓ Branch 1 taken 415440 times.
842776 if (!avci->buffer_frame->buf[0])
213 427336 return AVERROR(EAGAIN);
214
215 415440 av_frame_move_ref(frame, avci->buffer_frame);
216
217 #if FF_API_FRAME_KEY
218 FF_DISABLE_DEPRECATION_WARNINGS
219
2/2
✓ Branch 0 taken 277606 times.
✓ Branch 1 taken 137834 times.
415440 if (frame->key_frame)
220 277606 frame->flags |= AV_FRAME_FLAG_KEY;
221 FF_ENABLE_DEPRECATION_WARNINGS
222 #endif
223 #if FF_API_INTERLACED_FRAME
224 FF_DISABLE_DEPRECATION_WARNINGS
225
2/2
✓ Branch 0 taken 7933 times.
✓ Branch 1 taken 407507 times.
415440 if (frame->interlaced_frame)
226 7933 frame->flags |= AV_FRAME_FLAG_INTERLACED;
227
2/2
✓ Branch 0 taken 6124 times.
✓ Branch 1 taken 409316 times.
415440 if (frame->top_field_first)
228 6124 frame->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST;
229 FF_ENABLE_DEPRECATION_WARNINGS
230 #endif
231
232 415440 return 0;
233 }
234
235 404244 int ff_encode_reordered_opaque(AVCodecContext *avctx,
236 AVPacket *pkt, const AVFrame *frame)
237 {
238
2/2
✓ Branch 0 taken 400705 times.
✓ Branch 1 taken 3539 times.
404244 if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) {
239 400705 int ret = av_buffer_replace(&pkt->opaque_ref, frame->opaque_ref);
240
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 400705 times.
400705 if (ret < 0)
241 return ret;
242 400705 pkt->opaque = frame->opaque;
243 }
244
245 404244 return 0;
246 }
247
248 415869 int ff_encode_encode_cb(AVCodecContext *avctx, AVPacket *avpkt,
249 AVFrame *frame, int *got_packet)
250 {
251 415869 const FFCodec *const codec = ffcodec(avctx->codec);
252 int ret;
253
254 415869 ret = codec->cb.encode(avctx, avpkt, frame, got_packet);
255 415869 emms_c();
256
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 415869 times.
415869 av_assert0(ret <= 0);
257
258
3/4
✓ Branch 0 taken 415869 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6505 times.
✓ Branch 3 taken 409364 times.
415869 if (!ret && *got_packet) {
259
2/2
✓ Branch 0 taken 409311 times.
✓ Branch 1 taken 53 times.
409364 if (avpkt->data) {
260 409311 ret = encode_make_refcounted(avctx, avpkt);
261
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 409311 times.
409311 if (ret < 0)
262 goto unref;
263 // Date returned by encoders must always be ref-counted
264
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 409311 times.
409311 av_assert0(avpkt->buf);
265 }
266
267 // set the timestamps for the simple no-delay case
268 // encoders with delay have to set the timestamps themselves
269
4/4
✓ Branch 0 taken 26205 times.
✓ Branch 1 taken 383159 times.
✓ Branch 2 taken 26002 times.
✓ Branch 3 taken 203 times.
409364 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) ||
270
2/2
✓ Branch 0 taken 14993 times.
✓ Branch 1 taken 11009 times.
26002 (frame && (codec->caps_internal & FF_CODEC_CAP_EOF_FLUSH))) {
271
2/2
✓ Branch 0 taken 382835 times.
✓ Branch 1 taken 15317 times.
398152 if (avpkt->pts == AV_NOPTS_VALUE)
272 382835 avpkt->pts = frame->pts;
273
274
2/2
✓ Branch 0 taken 393153 times.
✓ Branch 1 taken 4999 times.
398152 if (!avpkt->duration) {
275
2/2
✓ Branch 0 taken 372907 times.
✓ Branch 1 taken 20246 times.
393153 if (frame->duration)
276 372907 avpkt->duration = frame->duration;
277
2/2
✓ Branch 0 taken 3200 times.
✓ Branch 1 taken 17046 times.
20246 else if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
278 3200 avpkt->duration = ff_samples_to_time_base(avctx,
279 3200 frame->nb_samples);
280 }
281 }
282
283 398152 ret = ff_encode_reordered_opaque(avctx, avpkt, frame);
284
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 398152 times.
398152 if (ret < 0)
285 goto unref;
286 }
287
288 // dts equals pts unless there is reordering
289 // there can be no reordering if there is no encoder delay
290
2/2
✓ Branch 0 taken 6831 times.
✓ Branch 1 taken 402533 times.
409364 if (!(avctx->codec_descriptor->props & AV_CODEC_PROP_REORDER) ||
291
2/2
✓ Branch 0 taken 6066 times.
✓ Branch 1 taken 765 times.
6831 !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) ||
292
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6066 times.
6066 (codec->caps_internal & FF_CODEC_CAP_EOF_FLUSH))
293 403298 avpkt->dts = avpkt->pts;
294 } else {
295 6505 unref:
296 6505 av_packet_unref(avpkt);
297 }
298
299
2/2
✓ Branch 0 taken 415440 times.
✓ Branch 1 taken 429 times.
415869 if (frame)
300 415440 av_frame_unref(frame);
301
302 415869 return ret;
303 }
304
305 856931 static int encode_simple_internal(AVCodecContext *avctx, AVPacket *avpkt)
306 {
307 856931 AVCodecInternal *avci = avctx->internal;
308 856931 AVFrame *frame = avci->in_frame;
309 856931 const FFCodec *const codec = ffcodec(avctx->codec);
310 int got_packet;
311 int ret;
312
313
2/2
✓ Branch 0 taken 1828 times.
✓ Branch 1 taken 855103 times.
856931 if (avci->draining_done)
314 1828 return AVERROR_EOF;
315
316
3/4
✓ Branch 0 taken 855103 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 842776 times.
✓ Branch 3 taken 12327 times.
855103 if (!frame->buf[0] && !avci->draining) {
317 842776 av_frame_unref(frame);
318 842776 ret = ff_encode_get_frame(avctx, frame);
319
3/4
✓ Branch 0 taken 427336 times.
✓ Branch 1 taken 415440 times.
✓ Branch 2 taken 427336 times.
✗ Branch 3 not taken.
842776 if (ret < 0 && ret != AVERROR_EOF)
320 427336 return ret;
321 }
322
323
2/2
✓ Branch 0 taken 12327 times.
✓ Branch 1 taken 415440 times.
427767 if (!frame->buf[0]) {
324
2/2
✓ Branch 0 taken 11898 times.
✓ Branch 1 taken 429 times.
12327 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
325
2/2
✓ Branch 0 taken 4619 times.
✓ Branch 1 taken 7279 times.
11898 avci->frame_thread_encoder))
326 4619 return AVERROR_EOF;
327
328 // Flushing is signaled with a NULL frame
329 7708 frame = NULL;
330 }
331
332 423148 got_packet = 0;
333
334
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 423148 times.
423148 av_assert0(codec->cb_type == FF_CODEC_CB_TYPE_ENCODE);
335
336
2/2
✓ Branch 0 taken 63722 times.
✓ Branch 1 taken 359426 times.
423148 if (CONFIG_FRAME_THREAD_ENCODER && avci->frame_thread_encoder)
337 /* This will unref frame. */
338 63722 ret = ff_thread_video_encode_frame(avctx, avpkt, frame, &got_packet);
339 else {
340 359426 ret = ff_encode_encode_cb(avctx, avpkt, frame, &got_packet);
341 }
342
343
4/4
✓ Branch 0 taken 7708 times.
✓ Branch 1 taken 415440 times.
✓ Branch 2 taken 1828 times.
✓ Branch 3 taken 5880 times.
423148 if (avci->draining && !got_packet)
344 1828 avci->draining_done = 1;
345
346 423148 return ret;
347 }
348
349 843147 static int encode_simple_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
350 {
351 int ret;
352
353
4/4
✓ Branch 0 taken 856984 times.
✓ Branch 1 taken 409311 times.
✓ Branch 2 taken 856931 times.
✓ Branch 3 taken 53 times.
1266295 while (!avpkt->data && !avpkt->side_data) {
354 856931 ret = encode_simple_internal(avctx, avpkt);
355
2/2
✓ Branch 0 taken 433783 times.
✓ Branch 1 taken 423148 times.
856931 if (ret < 0)
356 433783 return ret;
357 }
358
359 409364 return 0;
360 }
361
362 847849 static int encode_receive_packet_internal(AVCodecContext *avctx, AVPacket *avpkt)
363 {
364 847849 AVCodecInternal *avci = avctx->internal;
365 int ret;
366
367
2/2
✓ Branch 0 taken 4702 times.
✓ Branch 1 taken 843147 times.
847849 if (avci->draining_done)
368 4702 return AVERROR_EOF;
369
370
2/4
✓ Branch 0 taken 843147 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 843147 times.
843147 av_assert0(!avpkt->data && !avpkt->side_data);
371
372
2/2
✓ Branch 0 taken 228116 times.
✓ Branch 1 taken 615031 times.
843147 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
373
3/4
✓ Branch 0 taken 404 times.
✓ Branch 1 taken 227712 times.
✓ Branch 2 taken 404 times.
✗ Branch 3 not taken.
228116 if ((avctx->flags & AV_CODEC_FLAG_PASS1) && avctx->stats_out)
374 404 avctx->stats_out[0] = '\0';
375
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 228116 times.
228116 if (av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx))
376 return AVERROR(EINVAL);
377 }
378
379
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 843147 times.
843147 if (ffcodec(avctx->codec)->cb_type == FF_CODEC_CB_TYPE_RECEIVE_PACKET) {
380 ret = ffcodec(avctx->codec)->cb.receive_packet(avctx, avpkt);
381 if (ret < 0)
382 av_packet_unref(avpkt);
383 else
384 // Encoders must always return ref-counted buffers.
385 // Side-data only packets have no data and can be not ref-counted.
386 av_assert0(!avpkt->data || avpkt->buf);
387 } else
388 843147 ret = encode_simple_receive_packet(avctx, avpkt);
389
2/2
✓ Branch 0 taken 409364 times.
✓ Branch 1 taken 433783 times.
843147 if (ret >= 0)
390 409364 avpkt->flags |= encode_ctx(avci)->intra_only_flag;
391
392
2/2
✓ Branch 0 taken 6447 times.
✓ Branch 1 taken 836700 times.
843147 if (ret == AVERROR_EOF)
393 6447 avci->draining_done = 1;
394
395 843147 return ret;
396 }
397
398 #if CONFIG_LCMS2
399 static int encode_generate_icc_profile(AVCodecContext *avctx, AVFrame *frame)
400 {
401 enum AVColorTransferCharacteristic trc = frame->color_trc;
402 enum AVColorPrimaries prim = frame->color_primaries;
403 const FFCodec *const codec = ffcodec(avctx->codec);
404 AVCodecInternal *avci = avctx->internal;
405 cmsHPROFILE profile;
406 int ret;
407
408 /* don't generate ICC profiles if disabled or unsupported */
409 if (!(avctx->flags2 & AV_CODEC_FLAG2_ICC_PROFILES))
410 return 0;
411 if (!(codec->caps_internal & FF_CODEC_CAP_ICC_PROFILES))
412 return 0;
413
414 if (trc == AVCOL_TRC_UNSPECIFIED)
415 trc = avctx->color_trc;
416 if (prim == AVCOL_PRI_UNSPECIFIED)
417 prim = avctx->color_primaries;
418 if (trc == AVCOL_TRC_UNSPECIFIED || prim == AVCOL_PRI_UNSPECIFIED)
419 return 0; /* can't generate ICC profile with missing csp tags */
420
421 if (av_frame_get_side_data(frame, AV_FRAME_DATA_ICC_PROFILE))
422 return 0; /* don't overwrite existing ICC profile */
423
424 if (!avci->icc.avctx) {
425 ret = ff_icc_context_init(&avci->icc, avctx);
426 if (ret < 0)
427 return ret;
428 }
429
430 ret = ff_icc_profile_generate(&avci->icc, prim, trc, &profile);
431 if (ret < 0)
432 return ret;
433
434 ret = ff_icc_profile_attach(&avci->icc, profile, frame);
435 cmsCloseProfile(profile);
436 return ret;
437 }
438 #else /* !CONFIG_LCMS2 */
439 108588 static int encode_generate_icc_profile(av_unused AVCodecContext *c, av_unused AVFrame *f)
440 {
441 108588 return 0;
442 }
443 #endif
444
445 415440 static int encode_send_frame_internal(AVCodecContext *avctx, const AVFrame *src)
446 {
447 415440 AVCodecInternal *avci = avctx->internal;
448 415440 EncodeContext *ec = encode_ctx(avci);
449 415440 AVFrame *dst = avci->buffer_frame;
450 int ret;
451
452
2/2
✓ Branch 0 taken 306852 times.
✓ Branch 1 taken 108588 times.
415440 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
453 /* extract audio service type metadata */
454 306852 AVFrameSideData *sd = av_frame_get_side_data(src, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
455
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 306852 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
306852 if (sd && sd->size >= sizeof(enum AVAudioServiceType))
456 avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
457
458 /* check for valid frame size */
459
2/2
✓ Branch 0 taken 73833 times.
✓ Branch 1 taken 233019 times.
306852 if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
460 /* if we already got an undersized frame, that must have been the last */
461
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73833 times.
73833 if (ec->last_audio_frame) {
462 av_log(avctx, AV_LOG_ERROR, "frame_size (%d) was not respected for a non-last frame\n", avctx->frame_size);
463 return AVERROR(EINVAL);
464 }
465
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73833 times.
73833 if (src->nb_samples > avctx->frame_size) {
466 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) > frame_size (%d)\n", src->nb_samples, avctx->frame_size);
467 return AVERROR(EINVAL);
468 }
469
2/2
✓ Branch 0 taken 145 times.
✓ Branch 1 taken 73688 times.
73833 if (src->nb_samples < avctx->frame_size) {
470 145 ec->last_audio_frame = 1;
471
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 91 times.
145 if (!(avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME)) {
472
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 52 times.
54 int pad_samples = avci->pad_samples ? avci->pad_samples : avctx->frame_size;
473 54 int out_samples = (src->nb_samples + pad_samples - 1) / pad_samples * pad_samples;
474
475
1/2
✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
54 if (out_samples != src->nb_samples) {
476 54 ret = pad_last_frame(avctx, dst, src, out_samples);
477
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
54 if (ret < 0)
478 return ret;
479 54 goto finish;
480 }
481 }
482 }
483 }
484 }
485
486 415386 ret = av_frame_ref(dst, src);
487
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 415386 times.
415386 if (ret < 0)
488 return ret;
489
490 415386 finish:
491
492
2/2
✓ Branch 0 taken 108588 times.
✓ Branch 1 taken 306852 times.
415440 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
493 108588 ret = encode_generate_icc_profile(avctx, dst);
494
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 108588 times.
108588 if (ret < 0)
495 return ret;
496 }
497
498 // unset frame duration unless AV_CODEC_FLAG_FRAME_DURATION is set,
499 // since otherwise we cannot be sure that whatever value it has is in the
500 // right timebase, so we would produce an incorrect value, which is worse
501 // than none at all
502
2/2
✓ Branch 0 taken 3260 times.
✓ Branch 1 taken 412180 times.
415440 if (!(avctx->flags & AV_CODEC_FLAG_FRAME_DURATION))
503 3260 dst->duration = 0;
504
505 415440 return 0;
506 }
507
508 421887 int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
509 {
510 421887 AVCodecInternal *avci = avctx->internal;
511 int ret;
512
513
2/4
✓ Branch 1 taken 421887 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 421887 times.
421887 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
514 return AVERROR(EINVAL);
515
516
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 421887 times.
421887 if (avci->draining)
517 return AVERROR_EOF;
518
519
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 421887 times.
421887 if (avci->buffer_frame->buf[0])
520 return AVERROR(EAGAIN);
521
522
2/2
✓ Branch 0 taken 6447 times.
✓ Branch 1 taken 415440 times.
421887 if (!frame) {
523 6447 avci->draining = 1;
524 } else {
525 415440 ret = encode_send_frame_internal(avctx, frame);
526
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 415440 times.
415440 if (ret < 0)
527 return ret;
528 }
529
530
2/4
✓ Branch 0 taken 421887 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 421887 times.
✗ Branch 3 not taken.
421887 if (!avci->buffer_pkt->data && !avci->buffer_pkt->side_data) {
531 421887 ret = encode_receive_packet_internal(avctx, avci->buffer_pkt);
532
5/6
✓ Branch 0 taken 16658 times.
✓ Branch 1 taken 405229 times.
✓ Branch 2 taken 4702 times.
✓ Branch 3 taken 11956 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 4702 times.
421887 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
533 return ret;
534 }
535
536 421887 avctx->frame_num++;
537
538 421887 return 0;
539 }
540
541 831191 int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
542 {
543 831191 AVCodecInternal *avci = avctx->internal;
544 int ret;
545
546 831191 av_packet_unref(avpkt);
547
548
2/4
✓ Branch 1 taken 831191 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 831191 times.
831191 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
549 return AVERROR(EINVAL);
550
551
4/4
✓ Branch 0 taken 426015 times.
✓ Branch 1 taken 405176 times.
✓ Branch 2 taken 53 times.
✓ Branch 3 taken 425962 times.
831191 if (avci->buffer_pkt->data || avci->buffer_pkt->side_data) {
552 405229 av_packet_move_ref(avpkt, avci->buffer_pkt);
553 } else {
554 425962 ret = encode_receive_packet_internal(avctx, avpkt);
555
2/2
✓ Branch 0 taken 421827 times.
✓ Branch 1 taken 4135 times.
425962 if (ret < 0)
556 421827 return ret;
557 }
558
559 409364 return 0;
560 }
561
562 18006 static int encode_preinit_video(AVCodecContext *avctx)
563 {
564 18006 const AVCodec *c = avctx->codec;
565 18006 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt);
566 int i;
567
568
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18006 times.
18006 if (!av_get_pix_fmt_name(avctx->pix_fmt)) {
569 av_log(avctx, AV_LOG_ERROR, "Invalid video pixel format: %d\n",
570 avctx->pix_fmt);
571 return AVERROR(EINVAL);
572 }
573
574
2/2
✓ Branch 0 taken 793 times.
✓ Branch 1 taken 17213 times.
18006 if (c->pix_fmts) {
575
1/2
✓ Branch 0 taken 2491 times.
✗ Branch 1 not taken.
2491 for (i = 0; c->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
576
2/2
✓ Branch 0 taken 793 times.
✓ Branch 1 taken 1698 times.
2491 if (avctx->pix_fmt == c->pix_fmts[i])
577 793 break;
578
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 793 times.
793 if (c->pix_fmts[i] == AV_PIX_FMT_NONE) {
579 av_log(avctx, AV_LOG_ERROR,
580 "Specified pixel format %s is not supported by the %s encoder.\n",
581 av_get_pix_fmt_name(avctx->pix_fmt), c->name);
582
583 av_log(avctx, AV_LOG_ERROR, "Supported pixel formats:\n");
584 for (int p = 0; c->pix_fmts[p] != AV_PIX_FMT_NONE; p++) {
585 av_log(avctx, AV_LOG_ERROR, " %s\n",
586 av_get_pix_fmt_name(c->pix_fmts[p]));
587 }
588
589 return AVERROR(EINVAL);
590 }
591
2/2
✓ Branch 0 taken 771 times.
✓ Branch 1 taken 22 times.
793 if (c->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
592
1/2
✓ Branch 0 taken 771 times.
✗ Branch 1 not taken.
771 c->pix_fmts[i] == AV_PIX_FMT_YUVJ411P ||
593
2/2
✓ Branch 0 taken 767 times.
✓ Branch 1 taken 4 times.
771 c->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
594
1/2
✓ Branch 0 taken 767 times.
✗ Branch 1 not taken.
767 c->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
595
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 758 times.
767 c->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
596 35 avctx->color_range = AVCOL_RANGE_JPEG;
597 }
598
599
1/2
✓ Branch 0 taken 18006 times.
✗ Branch 1 not taken.
18006 if ( avctx->bits_per_raw_sample < 0
600
3/4
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 17923 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 83 times.
18006 || (avctx->bits_per_raw_sample > 8 && pixdesc->comp[0].depth <= 8)) {
601 av_log(avctx, AV_LOG_WARNING, "Specified bit depth %d not possible with the specified pixel formats depth %d\n",
602 avctx->bits_per_raw_sample, pixdesc->comp[0].depth);
603 avctx->bits_per_raw_sample = pixdesc->comp[0].depth;
604 }
605
2/4
✓ Branch 0 taken 18006 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 18006 times.
18006 if (avctx->width <= 0 || avctx->height <= 0) {
606 av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
607 return AVERROR(EINVAL);
608 }
609
610 #if FF_API_TICKS_PER_FRAME
611 FF_DISABLE_DEPRECATION_WARNINGS
612
2/4
✓ Branch 0 taken 18006 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18006 times.
✗ Branch 3 not taken.
18006 if (avctx->ticks_per_frame && avctx->time_base.num &&
613
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18006 times.
18006 avctx->ticks_per_frame > INT_MAX / avctx->time_base.num) {
614 av_log(avctx, AV_LOG_ERROR,
615 "ticks_per_frame %d too large for the timebase %d/%d.",
616 avctx->ticks_per_frame,
617 avctx->time_base.num,
618 avctx->time_base.den);
619 return AVERROR(EINVAL);
620 }
621 FF_ENABLE_DEPRECATION_WARNINGS
622 #endif
623
624
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18006 times.
18006 if (avctx->hw_frames_ctx) {
625 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
626 if (frames_ctx->format != avctx->pix_fmt) {
627 av_log(avctx, AV_LOG_ERROR,
628 "Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format\n");
629 return AVERROR(EINVAL);
630 }
631 if (avctx->sw_pix_fmt != AV_PIX_FMT_NONE &&
632 avctx->sw_pix_fmt != frames_ctx->sw_format) {
633 av_log(avctx, AV_LOG_ERROR,
634 "Mismatching AVCodecContext.sw_pix_fmt (%s) "
635 "and AVHWFramesContext.sw_format (%s)\n",
636 av_get_pix_fmt_name(avctx->sw_pix_fmt),
637 av_get_pix_fmt_name(frames_ctx->sw_format));
638 return AVERROR(EINVAL);
639 }
640 avctx->sw_pix_fmt = frames_ctx->sw_format;
641 }
642
643 18006 return 0;
644 }
645
646 1263 static int encode_preinit_audio(AVCodecContext *avctx)
647 {
648 1263 const AVCodec *c = avctx->codec;
649 int i;
650
651
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1263 times.
1263 if (!av_get_sample_fmt_name(avctx->sample_fmt)) {
652 av_log(avctx, AV_LOG_ERROR, "Invalid audio sample format: %d\n",
653 avctx->sample_fmt);
654 return AVERROR(EINVAL);
655 }
656
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1263 times.
1263 if (avctx->sample_rate <= 0) {
657 av_log(avctx, AV_LOG_ERROR, "Invalid audio sample rate: %d\n",
658 avctx->sample_rate);
659 return AVERROR(EINVAL);
660 }
661
662
1/2
✓ Branch 0 taken 1263 times.
✗ Branch 1 not taken.
1263 if (c->sample_fmts) {
663
1/2
✓ Branch 0 taken 1280 times.
✗ Branch 1 not taken.
1280 for (i = 0; c->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
664
2/2
✓ Branch 0 taken 1263 times.
✓ Branch 1 taken 17 times.
1280 if (avctx->sample_fmt == c->sample_fmts[i])
665 1263 break;
666
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
21 if (avctx->ch_layout.nb_channels == 1 &&
667 4 av_get_planar_sample_fmt(avctx->sample_fmt) ==
668 4 av_get_planar_sample_fmt(c->sample_fmts[i])) {
669 avctx->sample_fmt = c->sample_fmts[i];
670 break;
671 }
672 }
673
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1263 times.
1263 if (c->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
674 av_log(avctx, AV_LOG_ERROR,
675 "Specified sample format %s is not supported by the %s encoder\n",
676 av_get_sample_fmt_name(avctx->sample_fmt), c->name);
677
678 av_log(avctx, AV_LOG_ERROR, "Supported sample formats:\n");
679 for (int p = 0; c->sample_fmts[p] != AV_SAMPLE_FMT_NONE; p++) {
680 av_log(avctx, AV_LOG_ERROR, " %s\n",
681 av_get_sample_fmt_name(c->sample_fmts[p]));
682 }
683
684 return AVERROR(EINVAL);
685 }
686 }
687
2/2
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 1202 times.
1263 if (c->supported_samplerates) {
688
1/2
✓ Branch 0 taken 135 times.
✗ Branch 1 not taken.
135 for (i = 0; c->supported_samplerates[i] != 0; i++)
689
2/2
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 74 times.
135 if (avctx->sample_rate == c->supported_samplerates[i])
690 61 break;
691
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
61 if (c->supported_samplerates[i] == 0) {
692 av_log(avctx, AV_LOG_ERROR,
693 "Specified sample rate %d is not supported by the %s encoder\n",
694 avctx->sample_rate, c->name);
695
696 av_log(avctx, AV_LOG_ERROR, "Supported sample rates:\n");
697 for (int p = 0; c->supported_samplerates[p]; p++)
698 av_log(avctx, AV_LOG_ERROR, " %d\n", c->supported_samplerates[p]);
699
700 return AVERROR(EINVAL);
701 }
702 }
703
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 1185 times.
1263 if (c->ch_layouts) {
704
1/2
✓ Branch 0 taken 125 times.
✗ Branch 1 not taken.
125 for (i = 0; c->ch_layouts[i].nb_channels; i++) {
705
2/2
✓ Branch 1 taken 78 times.
✓ Branch 2 taken 47 times.
125 if (!av_channel_layout_compare(&avctx->ch_layout, &c->ch_layouts[i]))
706 78 break;
707 }
708
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
78 if (!c->ch_layouts[i].nb_channels) {
709 char buf[512];
710 int ret = av_channel_layout_describe(&avctx->ch_layout, buf, sizeof(buf));
711 av_log(avctx, AV_LOG_ERROR,
712 "Specified channel layout '%s' is not supported by the %s encoder\n",
713 ret > 0 ? buf : "?", c->name);
714
715 av_log(avctx, AV_LOG_ERROR, "Supported channel layouts:\n");
716 for (int p = 0; c->ch_layouts[p].nb_channels; p++) {
717 ret = av_channel_layout_describe(&c->ch_layouts[p], buf, sizeof(buf));
718 av_log(avctx, AV_LOG_ERROR, " %s\n", ret > 0 ? buf : "?");
719 }
720 return AVERROR(EINVAL);
721 }
722 }
723
724
2/2
✓ Branch 0 taken 1210 times.
✓ Branch 1 taken 53 times.
1263 if (!avctx->bits_per_raw_sample)
725 1210 avctx->bits_per_raw_sample = av_get_exact_bits_per_sample(avctx->codec_id);
726
2/2
✓ Branch 0 taken 154 times.
✓ Branch 1 taken 1109 times.
1263 if (!avctx->bits_per_raw_sample)
727 154 avctx->bits_per_raw_sample = 8 * av_get_bytes_per_sample(avctx->sample_fmt);
728
729 1263 return 0;
730 }
731
732 19307 int ff_encode_preinit(AVCodecContext *avctx)
733 {
734 19307 AVCodecInternal *avci = avctx->internal;
735 19307 EncodeContext *ec = encode_ctx(avci);
736 19307 int ret = 0;
737
738
2/4
✓ Branch 0 taken 19307 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 19307 times.
19307 if (avctx->time_base.num <= 0 || avctx->time_base.den <= 0) {
739 av_log(avctx, AV_LOG_ERROR, "The encoder timebase is not set.\n");
740 return AVERROR(EINVAL);
741 }
742
743
2/2
✓ Branch 0 taken 19217 times.
✓ Branch 1 taken 90 times.
19307 if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE &&
744
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19217 times.
19217 !(avctx->codec->capabilities & AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE)) {
745 av_log(avctx, AV_LOG_ERROR, "The copy_opaque flag is set, but the "
746 "encoder does not support it.\n");
747 return AVERROR(EINVAL);
748 }
749
750
3/3
✓ Branch 0 taken 18006 times.
✓ Branch 1 taken 1263 times.
✓ Branch 2 taken 38 times.
19307 switch (avctx->codec_type) {
751 18006 case AVMEDIA_TYPE_VIDEO: ret = encode_preinit_video(avctx); break;
752 1263 case AVMEDIA_TYPE_AUDIO: ret = encode_preinit_audio(avctx); break;
753 }
754
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19307 times.
19307 if (ret < 0)
755 return ret;
756
757
4/4
✓ Branch 0 taken 1301 times.
✓ Branch 1 taken 18006 times.
✓ Branch 2 taken 1263 times.
✓ Branch 3 taken 38 times.
19307 if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
758
3/4
✓ Branch 0 taken 19248 times.
✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 19248 times.
19269 && avctx->bit_rate>0 && avctx->bit_rate<1000) {
759 av_log(avctx, AV_LOG_WARNING, "Bitrate %"PRId64" is extremely low, maybe you mean %"PRId64"k\n", avctx->bit_rate, avctx->bit_rate);
760 }
761
762
2/2
✓ Branch 0 taken 19305 times.
✓ Branch 1 taken 2 times.
19307 if (!avctx->rc_initial_buffer_occupancy)
763 19305 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3LL / 4;
764
765
2/2
✓ Branch 0 taken 18949 times.
✓ Branch 1 taken 358 times.
19307 if (avctx->codec_descriptor->props & AV_CODEC_PROP_INTRA_ONLY)
766 18949 ec->intra_only_flag = AV_PKT_FLAG_KEY;
767
768
2/2
✓ Branch 1 taken 19269 times.
✓ Branch 2 taken 38 times.
19307 if (ffcodec(avctx->codec)->cb_type == FF_CODEC_CB_TYPE_ENCODE) {
769 19269 avci->in_frame = av_frame_alloc();
770
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19269 times.
19269 if (!avci->in_frame)
771 return AVERROR(ENOMEM);
772 }
773
774
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 19305 times.
19307 if ((avctx->flags & AV_CODEC_FLAG_RECON_FRAME)) {
775
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!(avctx->codec->capabilities & AV_CODEC_CAP_ENCODER_RECON_FRAME)) {
776 av_log(avctx, AV_LOG_ERROR, "Reconstructed frame output requested "
777 "from an encoder not supporting it\n");
778 return AVERROR(ENOSYS);
779 }
780
781 2 avci->recon_frame = av_frame_alloc();
782
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!avci->recon_frame)
783 return AVERROR(ENOMEM);
784 }
785
786
2/2
✓ Branch 0 taken 173763 times.
✓ Branch 1 taken 19307 times.
193070 for (int i = 0; ff_sd_global_map[i].packet < AV_PKT_DATA_NB; i++) {
787 173763 const enum AVPacketSideDataType type_packet = ff_sd_global_map[i].packet;
788 173763 const enum AVFrameSideDataType type_frame = ff_sd_global_map[i].frame;
789 const AVFrameSideData *sd_frame;
790 AVPacketSideData *sd_packet;
791
792 173763 sd_frame = av_frame_side_data_get(avctx->decoded_side_data,
793 avctx->nb_decoded_side_data,
794 type_frame);
795
3/4
✓ Branch 0 taken 224 times.
✓ Branch 1 taken 173539 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 224 times.
173987 if (!sd_frame ||
796 224 av_packet_side_data_get(avctx->coded_side_data, avctx->nb_coded_side_data,
797 type_packet))
798
799 173539 continue;
800
801 224 sd_packet = av_packet_side_data_new(&avctx->coded_side_data, &avctx->nb_coded_side_data,
802 224 type_packet, sd_frame->size, 0);
803
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 224 times.
224 if (!sd_packet)
804 return AVERROR(ENOMEM);
805
806 224 memcpy(sd_packet->data, sd_frame->data, sd_frame->size);
807 }
808
809 if (CONFIG_FRAME_THREAD_ENCODER) {
810 19307 ret = ff_frame_thread_encoder_init(avctx);
811
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19307 times.
19307 if (ret < 0)
812 return ret;
813 }
814
815 19307 return 0;
816 }
817
818 10832 int ff_encode_alloc_frame(AVCodecContext *avctx, AVFrame *frame)
819 {
820 int ret;
821
822
1/3
✓ Branch 0 taken 10832 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
10832 switch (avctx->codec->type) {
823 10832 case AVMEDIA_TYPE_VIDEO:
824 10832 frame->format = avctx->pix_fmt;
825
3/4
✓ Branch 0 taken 10816 times.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10816 times.
10832 if (frame->width <= 0 || frame->height <= 0) {
826 16 frame->width = FFMAX(avctx->width, avctx->coded_width);
827 16 frame->height = FFMAX(avctx->height, avctx->coded_height);
828 }
829
830 10832 break;
831 case AVMEDIA_TYPE_AUDIO:
832 frame->sample_rate = avctx->sample_rate;
833 frame->format = avctx->sample_fmt;
834 if (!frame->ch_layout.nb_channels) {
835 ret = av_channel_layout_copy(&frame->ch_layout, &avctx->ch_layout);
836 if (ret < 0)
837 return ret;
838 }
839 break;
840 }
841
842 10832 ret = avcodec_default_get_buffer2(avctx, frame, 0);
843
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10832 times.
10832 if (ret < 0) {
844 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
845 av_frame_unref(frame);
846 return ret;
847 }
848
849 10832 return 0;
850 }
851
852 60 int ff_encode_receive_frame(AVCodecContext *avctx, AVFrame *frame)
853 {
854 60 AVCodecInternal *avci = avctx->internal;
855
856
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (!avci->recon_frame)
857 return AVERROR(EINVAL);
858
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (!avci->recon_frame->buf[0])
859 return avci->draining_done ? AVERROR_EOF : AVERROR(EAGAIN);
860
861 60 av_frame_move_ref(frame, avci->recon_frame);
862 60 return 0;
863 }
864
865 void ff_encode_flush_buffers(AVCodecContext *avctx)
866 {
867 AVCodecInternal *avci = avctx->internal;
868
869 if (avci->in_frame)
870 av_frame_unref(avci->in_frame);
871 if (avci->recon_frame)
872 av_frame_unref(avci->recon_frame);
873 }
874
875 19307 AVCodecInternal *ff_encode_internal_alloc(void)
876 {
877 19307 return av_mallocz(sizeof(EncodeContext));
878 }
879
880 202 AVCPBProperties *ff_encode_add_cpb_side_data(AVCodecContext *avctx)
881 {
882 AVPacketSideData *tmp;
883 AVCPBProperties *props;
884 size_t size;
885 int i;
886
887
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 202 times.
203 for (i = 0; i < avctx->nb_coded_side_data; i++)
888
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (avctx->coded_side_data[i].type == AV_PKT_DATA_CPB_PROPERTIES)
889 return (AVCPBProperties *)avctx->coded_side_data[i].data;
890
891 202 props = av_cpb_properties_alloc(&size);
892
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 202 times.
202 if (!props)
893 return NULL;
894
895 202 tmp = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*tmp));
896
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 202 times.
202 if (!tmp) {
897 av_freep(&props);
898 return NULL;
899 }
900
901 202 avctx->coded_side_data = tmp;
902 202 avctx->nb_coded_side_data++;
903
904 202 avctx->coded_side_data[avctx->nb_coded_side_data - 1].type = AV_PKT_DATA_CPB_PROPERTIES;
905 202 avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props;
906 202 avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size;
907
908 202 return props;
909 }
910