FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/encode.c
Date: 2024-02-16 17:37:06
Exec Total Coverage
Lines: 316 457 69.1%
Functions: 24 25 96.0%
Branches: 240 376 63.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/pixdesc.h"
29 #include "libavutil/samplefmt.h"
30
31 #include "avcodec.h"
32 #include "avcodec_internal.h"
33 #include "codec_desc.h"
34 #include "codec_internal.h"
35 #include "encode.h"
36 #include "frame_thread_encoder.h"
37 #include "internal.h"
38
39 typedef struct EncodeContext {
40 AVCodecInternal avci;
41
42 /**
43 * This is set to AV_PKT_FLAG_KEY for encoders that encode intra-only
44 * formats (i.e. whose codec descriptor has AV_CODEC_PROP_INTRA_ONLY set).
45 * This is used to set said flag generically for said encoders.
46 */
47 int intra_only_flag;
48
49 /**
50 * An audio frame with less than required samples has been submitted (and
51 * potentially padded with silence). Reject all subsequent frames.
52 */
53 int last_audio_frame;
54 } EncodeContext;
55
56 879615 static EncodeContext *encode_ctx(AVCodecInternal *avci)
57 {
58 879615 return (EncodeContext*)avci;
59 }
60
61 36147 int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
62 {
63
2/4
✓ Branch 0 taken 36147 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 36147 times.
36147 if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
64 av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
65 size, INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
66 return AVERROR(EINVAL);
67 }
68
69
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36147 times.
36147 av_assert0(!avpkt->data);
70
71 36147 av_fast_padded_malloc(&avctx->internal->byte_buffer,
72 36147 &avctx->internal->byte_buffer_size, size);
73 36147 avpkt->data = avctx->internal->byte_buffer;
74
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36147 times.
36147 if (!avpkt->data) {
75 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
76 return AVERROR(ENOMEM);
77 }
78 36147 avpkt->size = size;
79
80 36147 return 0;
81 }
82
83 427055 int avcodec_default_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int flags)
84 {
85 int ret;
86
87
2/4
✓ Branch 0 taken 427055 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 427055 times.
427055 if (avpkt->size < 0 || avpkt->size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
88 return AVERROR(EINVAL);
89
90
2/4
✓ Branch 0 taken 427055 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 427055 times.
427055 if (avpkt->data || avpkt->buf) {
91 av_log(avctx, AV_LOG_ERROR, "avpkt->{data,buf} != NULL in avcodec_default_get_encode_buffer()\n");
92 return AVERROR(EINVAL);
93 }
94
95 427055 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 427055 times.
427055 if (ret < 0) {
97 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", avpkt->size);
98 return ret;
99 }
100 427055 avpkt->data = avpkt->buf->data;
101
102 427055 return 0;
103 }
104
105 427055 int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
106 {
107 int ret;
108
109
2/4
✓ Branch 0 taken 427055 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 427055 times.
427055 if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
110 return AVERROR(EINVAL);
111
112
2/4
✓ Branch 0 taken 427055 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 427055 times.
427055 av_assert0(!avpkt->data && !avpkt->buf);
113
114 427055 avpkt->size = size;
115 427055 ret = avctx->get_encode_buffer(avctx, avpkt, flags);
116
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 427055 times.
427055 if (ret < 0)
117 goto fail;
118
119
2/4
✓ Branch 0 taken 427055 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 427055 times.
427055 if (!avpkt->data || !avpkt->buf) {
120 av_log(avctx, AV_LOG_ERROR, "No buffer returned by get_encode_buffer()\n");
121 ret = AVERROR(EINVAL);
122 goto fail;
123 }
124 427055 memset(avpkt->data + avpkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
125
126 427055 ret = 0;
127 427055 fail:
128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 427055 times.
427055 if (ret < 0) {
129 av_log(avctx, AV_LOG_ERROR, "get_encode_buffer() failed\n");
130 av_packet_unref(avpkt);
131 }
132
133 427055 return ret;
134 }
135
136 427119 static int encode_make_refcounted(AVCodecContext *avctx, AVPacket *avpkt)
137 {
138 427119 uint8_t *data = avpkt->data;
139 int ret;
140
141
2/2
✓ Branch 0 taken 390972 times.
✓ Branch 1 taken 36147 times.
427119 if (avpkt->buf)
142 390972 return 0;
143
144 36147 avpkt->data = NULL;
145 36147 ret = ff_get_encode_buffer(avctx, avpkt, avpkt->size, 0);
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36147 times.
36147 if (ret < 0)
147 return ret;
148 36147 memcpy(avpkt->data, data, avpkt->size);
149
150 36147 return 0;
151 }
152
153 /**
154 * Pad last frame with silence.
155 */
156 52 static int pad_last_frame(AVCodecContext *s, AVFrame *frame, const AVFrame *src, int out_samples)
157 {
158 int ret;
159
160 52 frame->format = src->format;
161 52 frame->nb_samples = out_samples;
162 52 ret = av_channel_layout_copy(&frame->ch_layout, &s->ch_layout);
163
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
52 if (ret < 0)
164 goto fail;
165 52 ret = av_frame_get_buffer(frame, 0);
166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
52 if (ret < 0)
167 goto fail;
168
169 52 ret = av_frame_copy_props(frame, src);
170
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
52 if (ret < 0)
171 goto fail;
172
173
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
52 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
174 52 src->nb_samples, s->ch_layout.nb_channels,
175 s->sample_fmt)) < 0)
176 goto fail;
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
52 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
178 52 frame->nb_samples - src->nb_samples,
179 s->ch_layout.nb_channels, s->sample_fmt)) < 0)
180 goto fail;
181
182 52 return 0;
183
184 fail:
185 av_frame_unref(frame);
186 encode_ctx(s->internal)->last_audio_frame = 0;
187 return ret;
188 }
189
190 822 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
191 const AVSubtitle *sub)
192 {
193 int ret;
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 822 times.
822 if (sub->start_display_time) {
195 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
196 return -1;
197 }
198
199 822 ret = ffcodec(avctx->codec)->cb.encode_sub(avctx, buf, buf_size, sub);
200 822 avctx->frame_num++;
201 #if FF_API_AVCTX_FRAME_NUMBER
202 FF_DISABLE_DEPRECATION_WARNINGS
203 822 avctx->frame_number = avctx->frame_num;
204 FF_ENABLE_DEPRECATION_WARNINGS
205 #endif
206 822 return ret;
207 }
208
209 878470 int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame)
210 {
211 878470 AVCodecInternal *avci = avctx->internal;
212
213
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 878470 times.
878470 if (avci->draining)
214 return AVERROR_EOF;
215
216
2/2
✓ Branch 0 taken 445222 times.
✓ Branch 1 taken 433248 times.
878470 if (!avci->buffer_frame->buf[0])
217 445222 return AVERROR(EAGAIN);
218
219 433248 av_frame_move_ref(frame, avci->buffer_frame);
220
221 #if FF_API_FRAME_KEY
222 FF_DISABLE_DEPRECATION_WARNINGS
223
2/2
✓ Branch 0 taken 367091 times.
✓ Branch 1 taken 66157 times.
433248 if (frame->key_frame)
224 367091 frame->flags |= AV_FRAME_FLAG_KEY;
225 FF_ENABLE_DEPRECATION_WARNINGS
226 #endif
227 #if FF_API_INTERLACED_FRAME
228 FF_DISABLE_DEPRECATION_WARNINGS
229
2/2
✓ Branch 0 taken 7933 times.
✓ Branch 1 taken 425315 times.
433248 if (frame->interlaced_frame)
230 7933 frame->flags |= AV_FRAME_FLAG_INTERLACED;
231
2/2
✓ Branch 0 taken 6124 times.
✓ Branch 1 taken 427124 times.
433248 if (frame->top_field_first)
232 6124 frame->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST;
233 FF_ENABLE_DEPRECATION_WARNINGS
234 #endif
235
236 433248 return 0;
237 }
238
239 422052 int ff_encode_reordered_opaque(AVCodecContext *avctx,
240 AVPacket *pkt, const AVFrame *frame)
241 {
242 #if FF_API_REORDERED_OPAQUE
243 FF_DISABLE_DEPRECATION_WARNINGS
244 422052 avctx->reordered_opaque = frame->reordered_opaque;
245 FF_ENABLE_DEPRECATION_WARNINGS
246 #endif
247
248
2/2
✓ Branch 0 taken 418513 times.
✓ Branch 1 taken 3539 times.
422052 if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) {
249 418513 int ret = av_buffer_replace(&pkt->opaque_ref, frame->opaque_ref);
250
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 418513 times.
418513 if (ret < 0)
251 return ret;
252 418513 pkt->opaque = frame->opaque;
253 }
254
255 422052 return 0;
256 }
257
258 433637 int ff_encode_encode_cb(AVCodecContext *avctx, AVPacket *avpkt,
259 AVFrame *frame, int *got_packet)
260 {
261 433637 const FFCodec *const codec = ffcodec(avctx->codec);
262 int ret;
263
264 433637 ret = codec->cb.encode(avctx, avpkt, frame, got_packet);
265 433637 emms_c();
266
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 433637 times.
433637 av_assert0(ret <= 0);
267
268
3/4
✓ Branch 0 taken 433637 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6483 times.
✓ Branch 3 taken 427154 times.
433637 if (!ret && *got_packet) {
269
2/2
✓ Branch 0 taken 427119 times.
✓ Branch 1 taken 35 times.
427154 if (avpkt->data) {
270 427119 ret = encode_make_refcounted(avctx, avpkt);
271
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 427119 times.
427119 if (ret < 0)
272 goto unref;
273 // Date returned by encoders must always be ref-counted
274
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 427119 times.
427119 av_assert0(avpkt->buf);
275 }
276
277 // set the timestamps for the simple no-delay case
278 // encoders with delay have to set the timestamps themselves
279
4/4
✓ Branch 0 taken 25882 times.
✓ Branch 1 taken 401272 times.
✓ Branch 2 taken 25698 times.
✓ Branch 3 taken 184 times.
427154 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) ||
280
2/2
✓ Branch 0 taken 14763 times.
✓ Branch 1 taken 10935 times.
25698 (frame && (codec->caps_internal & FF_CODEC_CAP_EOF_FLUSH))) {
281
2/2
✓ Branch 0 taken 400760 times.
✓ Branch 1 taken 15275 times.
416035 if (avpkt->pts == AV_NOPTS_VALUE)
282 400760 avpkt->pts = frame->pts;
283
284
2/2
✓ Branch 0 taken 411036 times.
✓ Branch 1 taken 4999 times.
416035 if (!avpkt->duration) {
285
2/2
✓ Branch 0 taken 390911 times.
✓ Branch 1 taken 20125 times.
411036 if (frame->duration)
286 390911 avpkt->duration = frame->duration;
287
2/2
✓ Branch 0 taken 3200 times.
✓ Branch 1 taken 16925 times.
20125 else if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
288 3200 avpkt->duration = ff_samples_to_time_base(avctx,
289 3200 frame->nb_samples);
290 }
291 }
292
293 416035 ret = ff_encode_reordered_opaque(avctx, avpkt, frame);
294
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 416035 times.
416035 if (ret < 0)
295 goto unref;
296 }
297
298 // dts equals pts unless there is reordering
299 // there can be no reordering if there is no encoder delay
300
2/2
✓ Branch 0 taken 6756 times.
✓ Branch 1 taken 420398 times.
427154 if (!(avctx->codec_descriptor->props & AV_CODEC_PROP_REORDER) ||
301
2/2
✓ Branch 0 taken 5991 times.
✓ Branch 1 taken 765 times.
6756 !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) ||
302
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5991 times.
5991 (codec->caps_internal & FF_CODEC_CAP_EOF_FLUSH))
303 421163 avpkt->dts = avpkt->pts;
304 } else {
305 6483 unref:
306 6483 av_packet_unref(avpkt);
307 }
308
309
2/2
✓ Branch 0 taken 433248 times.
✓ Branch 1 taken 389 times.
433637 if (frame)
310 433248 av_frame_unref(frame);
311
312 433637 return ret;
313 }
314
315 892628 static int encode_simple_internal(AVCodecContext *avctx, AVPacket *avpkt)
316 {
317 892628 AVCodecInternal *avci = avctx->internal;
318 892628 AVFrame *frame = avci->in_frame;
319 892628 const FFCodec *const codec = ffcodec(avctx->codec);
320 int got_packet;
321 int ret;
322
323
2/2
✓ Branch 0 taken 1799 times.
✓ Branch 1 taken 890829 times.
892628 if (avci->draining_done)
324 1799 return AVERROR_EOF;
325
326
3/4
✓ Branch 0 taken 890829 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 878470 times.
✓ Branch 3 taken 12359 times.
890829 if (!frame->buf[0] && !avci->draining) {
327 878470 av_frame_unref(frame);
328 878470 ret = ff_encode_get_frame(avctx, frame);
329
3/4
✓ Branch 0 taken 445222 times.
✓ Branch 1 taken 433248 times.
✓ Branch 2 taken 445222 times.
✗ Branch 3 not taken.
878470 if (ret < 0 && ret != AVERROR_EOF)
330 445222 return ret;
331 }
332
333
2/2
✓ Branch 0 taken 12359 times.
✓ Branch 1 taken 433248 times.
445607 if (!frame->buf[0]) {
334
2/2
✓ Branch 0 taken 11970 times.
✓ Branch 1 taken 389 times.
12359 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
335
2/2
✓ Branch 0 taken 4618 times.
✓ Branch 1 taken 7352 times.
11970 avci->frame_thread_encoder))
336 4618 return AVERROR_EOF;
337
338 // Flushing is signaled with a NULL frame
339 7741 frame = NULL;
340 }
341
342 440989 got_packet = 0;
343
344
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 440989 times.
440989 av_assert0(codec->cb_type == FF_CODEC_CB_TYPE_ENCODE);
345
346
2/2
✓ Branch 0 taken 63529 times.
✓ Branch 1 taken 377460 times.
440989 if (CONFIG_FRAME_THREAD_ENCODER && avci->frame_thread_encoder)
347 /* This will unref frame. */
348 63529 ret = ff_thread_video_encode_frame(avctx, avpkt, frame, &got_packet);
349 else {
350 377460 ret = ff_encode_encode_cb(avctx, avpkt, frame, &got_packet);
351 }
352
353
4/4
✓ Branch 0 taken 7741 times.
✓ Branch 1 taken 433248 times.
✓ Branch 2 taken 1799 times.
✓ Branch 3 taken 5942 times.
440989 if (avci->draining && !got_packet)
354 1799 avci->draining_done = 1;
355
356 440989 return ret;
357 }
358
359 878793 static int encode_simple_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
360 {
361 int ret;
362
363
4/4
✓ Branch 0 taken 892663 times.
✓ Branch 1 taken 427119 times.
✓ Branch 2 taken 892628 times.
✓ Branch 3 taken 35 times.
1319782 while (!avpkt->data && !avpkt->side_data) {
364 892628 ret = encode_simple_internal(avctx, avpkt);
365
2/2
✓ Branch 0 taken 451639 times.
✓ Branch 1 taken 440989 times.
892628 if (ret < 0)
366 451639 return ret;
367 }
368
369 427154 return 0;
370 }
371
372 883492 static int encode_receive_packet_internal(AVCodecContext *avctx, AVPacket *avpkt)
373 {
374 883492 AVCodecInternal *avci = avctx->internal;
375 int ret;
376
377
2/2
✓ Branch 0 taken 4699 times.
✓ Branch 1 taken 878793 times.
883492 if (avci->draining_done)
378 4699 return AVERROR_EOF;
379
380
2/4
✓ Branch 0 taken 878793 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 878793 times.
878793 av_assert0(!avpkt->data && !avpkt->side_data);
381
382
2/2
✓ Branch 0 taken 227302 times.
✓ Branch 1 taken 651491 times.
878793 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
383
3/4
✓ Branch 0 taken 404 times.
✓ Branch 1 taken 226898 times.
✓ Branch 2 taken 404 times.
✗ Branch 3 not taken.
227302 if ((avctx->flags & AV_CODEC_FLAG_PASS1) && avctx->stats_out)
384 404 avctx->stats_out[0] = '\0';
385
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 227302 times.
227302 if (av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx))
386 return AVERROR(EINVAL);
387 }
388
389
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 878793 times.
878793 if (ffcodec(avctx->codec)->cb_type == FF_CODEC_CB_TYPE_RECEIVE_PACKET) {
390 ret = ffcodec(avctx->codec)->cb.receive_packet(avctx, avpkt);
391 if (ret < 0)
392 av_packet_unref(avpkt);
393 else
394 // Encoders must always return ref-counted buffers.
395 // Side-data only packets have no data and can be not ref-counted.
396 av_assert0(!avpkt->data || avpkt->buf);
397 } else
398 878793 ret = encode_simple_receive_packet(avctx, avpkt);
399
2/2
✓ Branch 0 taken 427154 times.
✓ Branch 1 taken 451639 times.
878793 if (ret >= 0)
400 427154 avpkt->flags |= encode_ctx(avci)->intra_only_flag;
401
402
2/2
✓ Branch 0 taken 6417 times.
✓ Branch 1 taken 872376 times.
878793 if (ret == AVERROR_EOF)
403 6417 avci->draining_done = 1;
404
405 878793 return ret;
406 }
407
408 #if CONFIG_LCMS2
409 static int encode_generate_icc_profile(AVCodecContext *avctx, AVFrame *frame)
410 {
411 enum AVColorTransferCharacteristic trc = frame->color_trc;
412 enum AVColorPrimaries prim = frame->color_primaries;
413 const FFCodec *const codec = ffcodec(avctx->codec);
414 AVCodecInternal *avci = avctx->internal;
415 cmsHPROFILE profile;
416 int ret;
417
418 /* don't generate ICC profiles if disabled or unsupported */
419 if (!(avctx->flags2 & AV_CODEC_FLAG2_ICC_PROFILES))
420 return 0;
421 if (!(codec->caps_internal & FF_CODEC_CAP_ICC_PROFILES))
422 return 0;
423
424 if (trc == AVCOL_TRC_UNSPECIFIED)
425 trc = avctx->color_trc;
426 if (prim == AVCOL_PRI_UNSPECIFIED)
427 prim = avctx->color_primaries;
428 if (trc == AVCOL_TRC_UNSPECIFIED || prim == AVCOL_PRI_UNSPECIFIED)
429 return 0; /* can't generate ICC profile with missing csp tags */
430
431 if (av_frame_get_side_data(frame, AV_FRAME_DATA_ICC_PROFILE))
432 return 0; /* don't overwrite existing ICC profile */
433
434 if (!avci->icc.avctx) {
435 ret = ff_icc_context_init(&avci->icc, avctx);
436 if (ret < 0)
437 return ret;
438 }
439
440 ret = ff_icc_profile_generate(&avci->icc, prim, trc, &profile);
441 if (ret < 0)
442 return ret;
443
444 ret = ff_icc_profile_attach(&avci->icc, profile, frame);
445 cmsCloseProfile(profile);
446 return ret;
447 }
448 #else /* !CONFIG_LCMS2 */
449 108147 static int encode_generate_icc_profile(av_unused AVCodecContext *c, av_unused AVFrame *f)
450 {
451 108147 return 0;
452 }
453 #endif
454
455 433248 static int encode_send_frame_internal(AVCodecContext *avctx, const AVFrame *src)
456 {
457 433248 AVCodecInternal *avci = avctx->internal;
458 433248 EncodeContext *ec = encode_ctx(avci);
459 433248 AVFrame *dst = avci->buffer_frame;
460 int ret;
461
462
2/2
✓ Branch 0 taken 325101 times.
✓ Branch 1 taken 108147 times.
433248 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
463 /* extract audio service type metadata */
464 325101 AVFrameSideData *sd = av_frame_get_side_data(src, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
465
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 325101 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
325101 if (sd && sd->size >= sizeof(enum AVAudioServiceType))
466 avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
467
468 /* check for valid frame size */
469
2/2
✓ Branch 0 taken 73611 times.
✓ Branch 1 taken 251490 times.
325101 if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
470 /* if we already got an undersized frame, that must have been the last */
471
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73611 times.
73611 if (ec->last_audio_frame) {
472 av_log(avctx, AV_LOG_ERROR, "frame_size (%d) was not respected for a non-last frame\n", avctx->frame_size);
473 return AVERROR(EINVAL);
474 }
475
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73611 times.
73611 if (src->nb_samples > avctx->frame_size) {
476 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) > frame_size (%d)\n", src->nb_samples, avctx->frame_size);
477 return AVERROR(EINVAL);
478 }
479
2/2
✓ Branch 0 taken 125 times.
✓ Branch 1 taken 73486 times.
73611 if (src->nb_samples < avctx->frame_size) {
480 125 ec->last_audio_frame = 1;
481
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 73 times.
125 if (!(avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME)) {
482
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 50 times.
52 int pad_samples = avci->pad_samples ? avci->pad_samples : avctx->frame_size;
483 52 int out_samples = (src->nb_samples + pad_samples - 1) / pad_samples * pad_samples;
484
485
1/2
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
52 if (out_samples != src->nb_samples) {
486 52 ret = pad_last_frame(avctx, dst, src, out_samples);
487
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
52 if (ret < 0)
488 return ret;
489 52 goto finish;
490 }
491 }
492 }
493 }
494 }
495
496 433196 ret = av_frame_ref(dst, src);
497
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 433196 times.
433196 if (ret < 0)
498 return ret;
499
500 433196 finish:
501
502
2/2
✓ Branch 0 taken 108147 times.
✓ Branch 1 taken 325101 times.
433248 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
503 108147 ret = encode_generate_icc_profile(avctx, dst);
504
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 108147 times.
108147 if (ret < 0)
505 return ret;
506 }
507
508 // unset frame duration unless AV_CODEC_FLAG_FRAME_DURATION is set,
509 // since otherwise we cannot be sure that whatever value it has is in the
510 // right timebase, so we would produce an incorrect value, which is worse
511 // than none at all
512
2/2
✓ Branch 0 taken 3260 times.
✓ Branch 1 taken 429988 times.
433248 if (!(avctx->flags & AV_CODEC_FLAG_FRAME_DURATION))
513 3260 dst->duration = 0;
514
515 433248 return 0;
516 }
517
518 439665 int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
519 {
520 439665 AVCodecInternal *avci = avctx->internal;
521 int ret;
522
523
2/4
✓ Branch 1 taken 439665 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 439665 times.
439665 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
524 return AVERROR(EINVAL);
525
526
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 439665 times.
439665 if (avci->draining)
527 return AVERROR_EOF;
528
529
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 439665 times.
439665 if (avci->buffer_frame->buf[0])
530 return AVERROR(EAGAIN);
531
532
2/2
✓ Branch 0 taken 6417 times.
✓ Branch 1 taken 433248 times.
439665 if (!frame) {
533 6417 avci->draining = 1;
534 } else {
535 433248 ret = encode_send_frame_internal(avctx, frame);
536
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 433248 times.
433248 if (ret < 0)
537 return ret;
538 }
539
540
2/4
✓ Branch 0 taken 439665 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 439665 times.
✗ Branch 3 not taken.
439665 if (!avci->buffer_pkt->data && !avci->buffer_pkt->side_data) {
541 439665 ret = encode_receive_packet_internal(avctx, avci->buffer_pkt);
542
5/6
✓ Branch 0 taken 16735 times.
✓ Branch 1 taken 422930 times.
✓ Branch 2 taken 4699 times.
✓ Branch 3 taken 12036 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 4699 times.
439665 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
543 return ret;
544 }
545
546 439665 avctx->frame_num++;
547 #if FF_API_AVCTX_FRAME_NUMBER
548 FF_DISABLE_DEPRECATION_WARNINGS
549 439665 avctx->frame_number = avctx->frame_num;
550 FF_ENABLE_DEPRECATION_WARNINGS
551 #endif
552
553 439665 return 0;
554 }
555
556 866757 int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
557 {
558 866757 AVCodecInternal *avci = avctx->internal;
559 int ret;
560
561 866757 av_packet_unref(avpkt);
562
563
2/4
✓ Branch 1 taken 866757 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 866757 times.
866757 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
564 return AVERROR(EINVAL);
565
566
4/4
✓ Branch 0 taken 443862 times.
✓ Branch 1 taken 422895 times.
✓ Branch 2 taken 35 times.
✓ Branch 3 taken 443827 times.
866757 if (avci->buffer_pkt->data || avci->buffer_pkt->side_data) {
567 422930 av_packet_move_ref(avpkt, avci->buffer_pkt);
568 } else {
569 443827 ret = encode_receive_packet_internal(avctx, avpkt);
570
2/2
✓ Branch 0 taken 439603 times.
✓ Branch 1 taken 4224 times.
443827 if (ret < 0)
571 439603 return ret;
572 }
573
574 427154 return 0;
575 }
576
577 17930 static int encode_preinit_video(AVCodecContext *avctx)
578 {
579 17930 const AVCodec *c = avctx->codec;
580 17930 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt);
581 int i;
582
583
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 17930 times.
17930 if (!av_get_pix_fmt_name(avctx->pix_fmt)) {
584 av_log(avctx, AV_LOG_ERROR, "Invalid video pixel format: %d\n",
585 avctx->pix_fmt);
586 return AVERROR(EINVAL);
587 }
588
589
2/2
✓ Branch 0 taken 790 times.
✓ Branch 1 taken 17140 times.
17930 if (c->pix_fmts) {
590
1/2
✓ Branch 0 taken 2488 times.
✗ Branch 1 not taken.
2488 for (i = 0; c->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
591
2/2
✓ Branch 0 taken 790 times.
✓ Branch 1 taken 1698 times.
2488 if (avctx->pix_fmt == c->pix_fmts[i])
592 790 break;
593
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 790 times.
790 if (c->pix_fmts[i] == AV_PIX_FMT_NONE) {
594 av_log(avctx, AV_LOG_ERROR,
595 "Specified pixel format %s is not supported by the %s encoder.\n",
596 av_get_pix_fmt_name(avctx->pix_fmt), c->name);
597
598 av_log(avctx, AV_LOG_ERROR, "Supported pixel formats:\n");
599 for (int p = 0; c->pix_fmts[p] != AV_PIX_FMT_NONE; p++) {
600 av_log(avctx, AV_LOG_ERROR, " %s\n",
601 av_get_pix_fmt_name(c->pix_fmts[p]));
602 }
603
604 return AVERROR(EINVAL);
605 }
606
2/2
✓ Branch 0 taken 768 times.
✓ Branch 1 taken 22 times.
790 if (c->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
607
1/2
✓ Branch 0 taken 768 times.
✗ Branch 1 not taken.
768 c->pix_fmts[i] == AV_PIX_FMT_YUVJ411P ||
608
2/2
✓ Branch 0 taken 764 times.
✓ Branch 1 taken 4 times.
768 c->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
609
1/2
✓ Branch 0 taken 764 times.
✗ Branch 1 not taken.
764 c->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
610
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 755 times.
764 c->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
611 35 avctx->color_range = AVCOL_RANGE_JPEG;
612 }
613
614
1/2
✓ Branch 0 taken 17930 times.
✗ Branch 1 not taken.
17930 if ( avctx->bits_per_raw_sample < 0
615
3/4
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 17847 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 83 times.
17930 || (avctx->bits_per_raw_sample > 8 && pixdesc->comp[0].depth <= 8)) {
616 av_log(avctx, AV_LOG_WARNING, "Specified bit depth %d not possible with the specified pixel formats depth %d\n",
617 avctx->bits_per_raw_sample, pixdesc->comp[0].depth);
618 avctx->bits_per_raw_sample = pixdesc->comp[0].depth;
619 }
620
2/4
✓ Branch 0 taken 17930 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 17930 times.
17930 if (avctx->width <= 0 || avctx->height <= 0) {
621 av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
622 return AVERROR(EINVAL);
623 }
624
625 #if FF_API_TICKS_PER_FRAME
626 FF_DISABLE_DEPRECATION_WARNINGS
627
2/4
✓ Branch 0 taken 17930 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17930 times.
✗ Branch 3 not taken.
17930 if (avctx->ticks_per_frame && avctx->time_base.num &&
628
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17930 times.
17930 avctx->ticks_per_frame > INT_MAX / avctx->time_base.num) {
629 av_log(avctx, AV_LOG_ERROR,
630 "ticks_per_frame %d too large for the timebase %d/%d.",
631 avctx->ticks_per_frame,
632 avctx->time_base.num,
633 avctx->time_base.den);
634 return AVERROR(EINVAL);
635 }
636 FF_ENABLE_DEPRECATION_WARNINGS
637 #endif
638
639
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17930 times.
17930 if (avctx->hw_frames_ctx) {
640 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
641 if (frames_ctx->format != avctx->pix_fmt) {
642 av_log(avctx, AV_LOG_ERROR,
643 "Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format\n");
644 return AVERROR(EINVAL);
645 }
646 if (avctx->sw_pix_fmt != AV_PIX_FMT_NONE &&
647 avctx->sw_pix_fmt != frames_ctx->sw_format) {
648 av_log(avctx, AV_LOG_ERROR,
649 "Mismatching AVCodecContext.sw_pix_fmt (%s) "
650 "and AVHWFramesContext.sw_format (%s)\n",
651 av_get_pix_fmt_name(avctx->sw_pix_fmt),
652 av_get_pix_fmt_name(frames_ctx->sw_format));
653 return AVERROR(EINVAL);
654 }
655 avctx->sw_pix_fmt = frames_ctx->sw_format;
656 }
657
658 17930 return 0;
659 }
660
661 1245 static int encode_preinit_audio(AVCodecContext *avctx)
662 {
663 1245 const AVCodec *c = avctx->codec;
664 int i;
665
666
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1245 times.
1245 if (!av_get_sample_fmt_name(avctx->sample_fmt)) {
667 av_log(avctx, AV_LOG_ERROR, "Invalid audio sample format: %d\n",
668 avctx->sample_fmt);
669 return AVERROR(EINVAL);
670 }
671
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1245 times.
1245 if (avctx->sample_rate <= 0) {
672 av_log(avctx, AV_LOG_ERROR, "Invalid audio sample rate: %d\n",
673 avctx->sample_rate);
674 return AVERROR(EINVAL);
675 }
676
677
1/2
✓ Branch 0 taken 1245 times.
✗ Branch 1 not taken.
1245 if (c->sample_fmts) {
678
1/2
✓ Branch 0 taken 1262 times.
✗ Branch 1 not taken.
1262 for (i = 0; c->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
679
2/2
✓ Branch 0 taken 1245 times.
✓ Branch 1 taken 17 times.
1262 if (avctx->sample_fmt == c->sample_fmts[i])
680 1245 break;
681
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 &&
682 4 av_get_planar_sample_fmt(avctx->sample_fmt) ==
683 4 av_get_planar_sample_fmt(c->sample_fmts[i])) {
684 avctx->sample_fmt = c->sample_fmts[i];
685 break;
686 }
687 }
688
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1245 times.
1245 if (c->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
689 av_log(avctx, AV_LOG_ERROR,
690 "Specified sample format %s is not supported by the %s encoder\n",
691 av_get_sample_fmt_name(avctx->sample_fmt), c->name);
692
693 av_log(avctx, AV_LOG_ERROR, "Supported sample formats:\n");
694 for (int p = 0; c->sample_fmts[p] != AV_SAMPLE_FMT_NONE; p++) {
695 av_log(avctx, AV_LOG_ERROR, " %s\n",
696 av_get_sample_fmt_name(c->sample_fmts[p]));
697 }
698
699 return AVERROR(EINVAL);
700 }
701 }
702
2/2
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 1184 times.
1245 if (c->supported_samplerates) {
703
1/2
✓ Branch 0 taken 135 times.
✗ Branch 1 not taken.
135 for (i = 0; c->supported_samplerates[i] != 0; i++)
704
2/2
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 74 times.
135 if (avctx->sample_rate == c->supported_samplerates[i])
705 61 break;
706
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
61 if (c->supported_samplerates[i] == 0) {
707 av_log(avctx, AV_LOG_ERROR,
708 "Specified sample rate %d is not supported by the %s encoder\n",
709 avctx->sample_rate, c->name);
710
711 av_log(avctx, AV_LOG_ERROR, "Supported sample rates:\n");
712 for (int p = 0; c->supported_samplerates[p]; p++)
713 av_log(avctx, AV_LOG_ERROR, " %d\n", c->supported_samplerates[p]);
714
715 return AVERROR(EINVAL);
716 }
717 }
718
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 1167 times.
1245 if (c->ch_layouts) {
719
1/2
✓ Branch 0 taken 125 times.
✗ Branch 1 not taken.
125 for (i = 0; c->ch_layouts[i].nb_channels; i++) {
720
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]))
721 78 break;
722 }
723
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
78 if (!c->ch_layouts[i].nb_channels) {
724 char buf[512];
725 int ret = av_channel_layout_describe(&avctx->ch_layout, buf, sizeof(buf));
726 av_log(avctx, AV_LOG_ERROR,
727 "Specified channel layout '%s' is not supported by the %s encoder\n",
728 ret > 0 ? buf : "?", c->name);
729
730 av_log(avctx, AV_LOG_ERROR, "Supported channel layouts:\n");
731 for (int p = 0; c->ch_layouts[p].nb_channels; p++) {
732 ret = av_channel_layout_describe(&c->ch_layouts[p], buf, sizeof(buf));
733 av_log(avctx, AV_LOG_ERROR, " %s\n", ret > 0 ? buf : "?");
734 }
735 return AVERROR(EINVAL);
736 }
737 }
738
739
2/2
✓ Branch 0 taken 1192 times.
✓ Branch 1 taken 53 times.
1245 if (!avctx->bits_per_raw_sample)
740 1192 avctx->bits_per_raw_sample = 8 * av_get_bytes_per_sample(avctx->sample_fmt);
741
742 1245 return 0;
743 }
744
745 19213 int ff_encode_preinit(AVCodecContext *avctx)
746 {
747 19213 AVCodecInternal *avci = avctx->internal;
748 19213 EncodeContext *ec = encode_ctx(avci);
749 19213 int ret = 0;
750
751
2/4
✓ Branch 0 taken 19213 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 19213 times.
19213 if (avctx->time_base.num <= 0 || avctx->time_base.den <= 0) {
752 av_log(avctx, AV_LOG_ERROR, "The encoder timebase is not set.\n");
753 return AVERROR(EINVAL);
754 }
755
756
2/2
✓ Branch 0 taken 19123 times.
✓ Branch 1 taken 90 times.
19213 if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE &&
757
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19123 times.
19123 !(avctx->codec->capabilities & AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE)) {
758 av_log(avctx, AV_LOG_ERROR, "The copy_opaque flag is set, but the "
759 "encoder does not support it.\n");
760 return AVERROR(EINVAL);
761 }
762
763
3/3
✓ Branch 0 taken 17930 times.
✓ Branch 1 taken 1245 times.
✓ Branch 2 taken 38 times.
19213 switch (avctx->codec_type) {
764 17930 case AVMEDIA_TYPE_VIDEO: ret = encode_preinit_video(avctx); break;
765 1245 case AVMEDIA_TYPE_AUDIO: ret = encode_preinit_audio(avctx); break;
766 }
767
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19213 times.
19213 if (ret < 0)
768 return ret;
769
770
4/4
✓ Branch 0 taken 1283 times.
✓ Branch 1 taken 17930 times.
✓ Branch 2 taken 1245 times.
✓ Branch 3 taken 38 times.
19213 if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
771
3/4
✓ Branch 0 taken 19154 times.
✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 19154 times.
19175 && avctx->bit_rate>0 && avctx->bit_rate<1000) {
772 av_log(avctx, AV_LOG_WARNING, "Bitrate %"PRId64" is extremely low, maybe you mean %"PRId64"k\n", avctx->bit_rate, avctx->bit_rate);
773 }
774
775
2/2
✓ Branch 0 taken 19211 times.
✓ Branch 1 taken 2 times.
19213 if (!avctx->rc_initial_buffer_occupancy)
776 19211 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3LL / 4;
777
778
2/2
✓ Branch 0 taken 18854 times.
✓ Branch 1 taken 359 times.
19213 if (avctx->codec_descriptor->props & AV_CODEC_PROP_INTRA_ONLY)
779 18854 ec->intra_only_flag = AV_PKT_FLAG_KEY;
780
781
2/2
✓ Branch 1 taken 19175 times.
✓ Branch 2 taken 38 times.
19213 if (ffcodec(avctx->codec)->cb_type == FF_CODEC_CB_TYPE_ENCODE) {
782 19175 avci->in_frame = av_frame_alloc();
783
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19175 times.
19175 if (!avci->in_frame)
784 return AVERROR(ENOMEM);
785 }
786
787
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 19211 times.
19213 if ((avctx->flags & AV_CODEC_FLAG_RECON_FRAME)) {
788
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!(avctx->codec->capabilities & AV_CODEC_CAP_ENCODER_RECON_FRAME)) {
789 av_log(avctx, AV_LOG_ERROR, "Reconstructed frame output requested "
790 "from an encoder not supporting it\n");
791 return AVERROR(ENOSYS);
792 }
793
794 2 avci->recon_frame = av_frame_alloc();
795
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!avci->recon_frame)
796 return AVERROR(ENOMEM);
797 }
798
799 if (CONFIG_FRAME_THREAD_ENCODER) {
800 19213 ret = ff_frame_thread_encoder_init(avctx);
801
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19213 times.
19213 if (ret < 0)
802 return ret;
803 }
804
805 19213 return 0;
806 }
807
808 10757 int ff_encode_alloc_frame(AVCodecContext *avctx, AVFrame *frame)
809 {
810 int ret;
811
812
1/3
✓ Branch 0 taken 10757 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
10757 switch (avctx->codec->type) {
813 10757 case AVMEDIA_TYPE_VIDEO:
814 10757 frame->format = avctx->pix_fmt;
815
3/4
✓ Branch 0 taken 10741 times.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10741 times.
10757 if (frame->width <= 0 || frame->height <= 0) {
816 16 frame->width = FFMAX(avctx->width, avctx->coded_width);
817 16 frame->height = FFMAX(avctx->height, avctx->coded_height);
818 }
819
820 10757 break;
821 case AVMEDIA_TYPE_AUDIO:
822 frame->sample_rate = avctx->sample_rate;
823 frame->format = avctx->sample_fmt;
824 if (!frame->ch_layout.nb_channels) {
825 ret = av_channel_layout_copy(&frame->ch_layout, &avctx->ch_layout);
826 if (ret < 0)
827 return ret;
828 }
829 break;
830 }
831
832 10757 ret = avcodec_default_get_buffer2(avctx, frame, 0);
833
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10757 times.
10757 if (ret < 0) {
834 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
835 av_frame_unref(frame);
836 return ret;
837 }
838
839 10757 return 0;
840 }
841
842 60 int ff_encode_receive_frame(AVCodecContext *avctx, AVFrame *frame)
843 {
844 60 AVCodecInternal *avci = avctx->internal;
845
846
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (!avci->recon_frame)
847 return AVERROR(EINVAL);
848
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (!avci->recon_frame->buf[0])
849 return avci->draining_done ? AVERROR_EOF : AVERROR(EAGAIN);
850
851 60 av_frame_move_ref(frame, avci->recon_frame);
852 60 return 0;
853 }
854
855 void ff_encode_flush_buffers(AVCodecContext *avctx)
856 {
857 AVCodecInternal *avci = avctx->internal;
858
859 if (avci->in_frame)
860 av_frame_unref(avci->in_frame);
861 if (avci->recon_frame)
862 av_frame_unref(avci->recon_frame);
863 }
864
865 19213 AVCodecInternal *ff_encode_internal_alloc(void)
866 {
867 19213 return av_mallocz(sizeof(EncodeContext));
868 }
869
870 200 AVCPBProperties *ff_encode_add_cpb_side_data(AVCodecContext *avctx)
871 {
872 AVPacketSideData *tmp;
873 AVCPBProperties *props;
874 size_t size;
875 int i;
876
877
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
200 for (i = 0; i < avctx->nb_coded_side_data; i++)
878 if (avctx->coded_side_data[i].type == AV_PKT_DATA_CPB_PROPERTIES)
879 return (AVCPBProperties *)avctx->coded_side_data[i].data;
880
881 200 props = av_cpb_properties_alloc(&size);
882
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
200 if (!props)
883 return NULL;
884
885 200 tmp = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*tmp));
886
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
200 if (!tmp) {
887 av_freep(&props);
888 return NULL;
889 }
890
891 200 avctx->coded_side_data = tmp;
892 200 avctx->nb_coded_side_data++;
893
894 200 avctx->coded_side_data[avctx->nb_coded_side_data - 1].type = AV_PKT_DATA_CPB_PROPERTIES;
895 200 avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props;
896 200 avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size;
897
898 200 return props;
899 }
900