FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/encode.c
Date: 2022-07-07 01:21:54
Exec Total Coverage
Lines: 244 352 69.3%
Branches: 218 332 65.7%

Line Branch Exec Source
1 /*
2 * generic encoding-related code
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "libavutil/attributes.h"
22 #include "libavutil/avassert.h"
23 #include "libavutil/channel_layout.h"
24 #include "libavutil/frame.h"
25 #include "libavutil/imgutils.h"
26 #include "libavutil/internal.h"
27 #include "libavutil/samplefmt.h"
28
29 #include "avcodec.h"
30 #include "codec_internal.h"
31 #include "encode.h"
32 #include "frame_thread_encoder.h"
33 #include "internal.h"
34
35 33398 int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
36 {
37
2/4
✓ Branch 0 taken 33398 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 33398 times.
33398 if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
38 av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
39 size, INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
40 return AVERROR(EINVAL);
41 }
42
43
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33398 times.
33398 av_assert0(!avpkt->data);
44
45 33398 av_fast_padded_malloc(&avctx->internal->byte_buffer,
46 33398 &avctx->internal->byte_buffer_size, size);
47 33398 avpkt->data = avctx->internal->byte_buffer;
48
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33398 times.
33398 if (!avpkt->data) {
49 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
50 return AVERROR(ENOMEM);
51 }
52 33398 avpkt->size = size;
53
54 33398 return 0;
55 }
56
57 365001 int avcodec_default_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int flags)
58 {
59 int ret;
60
61
2/4
✓ Branch 0 taken 365001 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 365001 times.
365001 if (avpkt->size < 0 || avpkt->size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
62 return AVERROR(EINVAL);
63
64
2/4
✓ Branch 0 taken 365001 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 365001 times.
365001 if (avpkt->data || avpkt->buf) {
65 av_log(avctx, AV_LOG_ERROR, "avpkt->{data,buf} != NULL in avcodec_default_get_encode_buffer()\n");
66 return AVERROR(EINVAL);
67 }
68
69 365001 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
70
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 365001 times.
365001 if (ret < 0) {
71 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", avpkt->size);
72 return ret;
73 }
74 365001 avpkt->data = avpkt->buf->data;
75
76 365001 return 0;
77 }
78
79 365001 int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
80 {
81 int ret;
82
83
2/4
✓ Branch 0 taken 365001 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 365001 times.
365001 if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
84 return AVERROR(EINVAL);
85
86
2/4
✓ Branch 0 taken 365001 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 365001 times.
365001 av_assert0(!avpkt->data && !avpkt->buf);
87
88 365001 avpkt->size = size;
89 365001 ret = avctx->get_encode_buffer(avctx, avpkt, flags);
90
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 365001 times.
365001 if (ret < 0)
91 goto fail;
92
93
2/4
✓ Branch 0 taken 365001 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 365001 times.
365001 if (!avpkt->data || !avpkt->buf) {
94 av_log(avctx, AV_LOG_ERROR, "No buffer returned by get_encode_buffer()\n");
95 ret = AVERROR(EINVAL);
96 goto fail;
97 }
98 365001 memset(avpkt->data + avpkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
99
100 365001 ret = 0;
101 365001 fail:
102
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 365001 times.
365001 if (ret < 0) {
103 av_log(avctx, AV_LOG_ERROR, "get_encode_buffer() failed\n");
104 av_packet_unref(avpkt);
105 }
106
107 365001 return ret;
108 }
109
110 /**
111 * Pad last frame with silence.
112 */
113 49 static int pad_last_frame(AVCodecContext *s, AVFrame *frame, const AVFrame *src)
114 {
115 int ret;
116
117 49 frame->format = src->format;
118 49 frame->nb_samples = s->frame_size;
119 49 ret = av_channel_layout_copy(&frame->ch_layout, &s->ch_layout);
120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 if (ret < 0)
121 goto fail;
122 49 ret = av_frame_get_buffer(frame, 0);
123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 if (ret < 0)
124 goto fail;
125
126 49 ret = av_frame_copy_props(frame, src);
127
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 if (ret < 0)
128 goto fail;
129
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
131 49 src->nb_samples, s->ch_layout.nb_channels,
132 s->sample_fmt)) < 0)
133 goto fail;
134
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
135 49 frame->nb_samples - src->nb_samples,
136 s->ch_layout.nb_channels, s->sample_fmt)) < 0)
137 goto fail;
138
139 49 return 0;
140
141 fail:
142 av_frame_unref(frame);
143 return ret;
144 }
145
146 806 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
147 const AVSubtitle *sub)
148 {
149 int ret;
150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 806 times.
806 if (sub->start_display_time) {
151 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
152 return -1;
153 }
154
155 806 ret = ffcodec(avctx->codec)->cb.encode_sub(avctx, buf, buf_size, sub);
156 806 avctx->frame_number++;
157 806 return ret;
158 }
159
160 818559 int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame)
161 {
162 818559 AVCodecInternal *avci = avctx->internal;
163
164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 818559 times.
818559 if (avci->draining)
165 return AVERROR_EOF;
166
167
2/2
✓ Branch 0 taken 413967 times.
✓ Branch 1 taken 404592 times.
818559 if (!avci->buffer_frame->buf[0])
168 413967 return AVERROR(EAGAIN);
169
170 404592 av_frame_move_ref(frame, avci->buffer_frame);
171
172 404592 return 0;
173 }
174
175 829535 static int encode_simple_internal(AVCodecContext *avctx, AVPacket *avpkt)
176 {
177 829535 AVCodecInternal *avci = avctx->internal;
178 829535 AVFrame *frame = avci->in_frame;
179 829535 const FFCodec *const codec = ffcodec(avctx->codec);
180 int got_packet;
181 int ret;
182
183
2/2
✓ Branch 0 taken 1697 times.
✓ Branch 1 taken 827838 times.
829535 if (avci->draining_done)
184 1697 return AVERROR_EOF;
185
186
3/4
✓ Branch 0 taken 827838 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 818559 times.
✓ Branch 3 taken 9279 times.
827838 if (!frame->buf[0] && !avci->draining) {
187 818559 av_frame_unref(frame);
188 818559 ret = ff_encode_get_frame(avctx, frame);
189
3/4
✓ Branch 0 taken 413967 times.
✓ Branch 1 taken 404592 times.
✓ Branch 2 taken 413967 times.
✗ Branch 3 not taken.
818559 if (ret < 0 && ret != AVERROR_EOF)
190 413967 return ret;
191 }
192
193
2/2
✓ Branch 0 taken 9279 times.
✓ Branch 1 taken 404592 times.
413871 if (!frame->buf[0]) {
194
2/2
✓ Branch 0 taken 8937 times.
✓ Branch 1 taken 342 times.
9279 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
195
3/4
✓ Branch 0 taken 4617 times.
✓ Branch 1 taken 4320 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4617 times.
8937 (avci->frame_thread_encoder && avctx->active_thread_type & FF_THREAD_FRAME)))
196 4320 return AVERROR_EOF;
197
198 // Flushing is signaled with a NULL frame
199 4959 frame = NULL;
200 }
201
202 409551 got_packet = 0;
203
204
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 409551 times.
409551 av_assert0(codec->cb_type == FF_CODEC_CB_TYPE_ENCODE);
205
206 409551 if (CONFIG_FRAME_THREAD_ENCODER &&
207
3/4
✓ Branch 0 taken 59436 times.
✓ Branch 1 taken 350115 times.
✓ Branch 2 taken 59436 times.
✗ Branch 3 not taken.
409551 avci->frame_thread_encoder && (avctx->active_thread_type & FF_THREAD_FRAME))
208 /* This might modify frame, but it doesn't matter, because
209 * the frame properties used below are not used for video
210 * (due to the delay inherent in frame threaded encoding, it makes
211 * no sense to use the properties of the current frame anyway). */
212 59436 ret = ff_thread_video_encode_frame(avctx, avpkt, frame, &got_packet);
213 else {
214 350115 ret = codec->cb.encode(avctx, avpkt, frame, &got_packet);
215
5/6
✓ Branch 0 taken 46844 times.
✓ Branch 1 taken 303271 times.
✓ Branch 2 taken 46844 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 46577 times.
✓ Branch 5 taken 267 times.
350115 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO && !ret && got_packet &&
216
2/2
✓ Branch 0 taken 39911 times.
✓ Branch 1 taken 6666 times.
46577 !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
217 39911 avpkt->pts = avpkt->dts = frame->pts;
218 }
219
220
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 409551 times.
409551 av_assert0(ret <= 0);
221
222 409551 emms_c();
223
224
3/4
✓ Branch 0 taken 409551 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 398479 times.
✓ Branch 3 taken 11072 times.
409551 if (!ret && got_packet) {
225
2/2
✓ Branch 0 taken 398463 times.
✓ Branch 1 taken 16 times.
398479 if (avpkt->data) {
226 398463 ret = av_packet_make_refcounted(avpkt);
227
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 398463 times.
398463 if (ret < 0)
228 goto end;
229 }
230
231
4/4
✓ Branch 0 taken 395217 times.
✓ Branch 1 taken 3262 times.
✓ Branch 2 taken 370877 times.
✓ Branch 3 taken 24340 times.
398479 if (frame && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
232
2/2
✓ Branch 0 taken 279248 times.
✓ Branch 1 taken 91629 times.
370877 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
233
2/2
✓ Branch 0 taken 267925 times.
✓ Branch 1 taken 11323 times.
279248 if (avpkt->pts == AV_NOPTS_VALUE)
234 267925 avpkt->pts = frame->pts;
235
2/2
✓ Branch 0 taken 278193 times.
✓ Branch 1 taken 1055 times.
279248 if (!avpkt->duration)
236 278193 avpkt->duration = ff_samples_to_time_base(avctx,
237 278193 frame->nb_samples);
238 }
239 }
240
2/2
✓ Branch 0 taken 297083 times.
✓ Branch 1 taken 101396 times.
398479 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
241 297083 avpkt->dts = avpkt->pts;
242 }
243 398479 avpkt->flags |= avci->intra_only_flag;
244 }
245
246
4/4
✓ Branch 0 taken 404592 times.
✓ Branch 1 taken 4959 times.
✓ Branch 2 taken 3262 times.
✓ Branch 3 taken 1697 times.
409551 if (avci->draining && !got_packet)
247 1697 avci->draining_done = 1;
248
249 407854 end:
250
3/4
✓ Branch 0 taken 409551 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11072 times.
✓ Branch 3 taken 398479 times.
409551 if (ret < 0 || !got_packet)
251 11072 av_packet_unref(avpkt);
252
253
2/2
✓ Branch 0 taken 404592 times.
✓ Branch 1 taken 4959 times.
409551 if (frame)
254 404592 av_frame_unref(frame);
255
256
2/2
✓ Branch 0 taken 398479 times.
✓ Branch 1 taken 11072 times.
409551 if (got_packet)
257 // Encoders must always return ref-counted buffers.
258 // Side-data only packets have no data and can be not ref-counted.
259
3/4
✓ Branch 0 taken 398463 times.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 398463 times.
398479 av_assert0(!avpkt->data || avpkt->buf);
260
261 409551 return ret;
262 }
263
264 818463 static int encode_simple_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
265 {
266 int ret;
267
268
4/4
✓ Branch 0 taken 829551 times.
✓ Branch 1 taken 398463 times.
✓ Branch 2 taken 829535 times.
✓ Branch 3 taken 16 times.
1228014 while (!avpkt->data && !avpkt->side_data) {
269 829535 ret = encode_simple_internal(avctx, avpkt);
270
2/2
✓ Branch 0 taken 419984 times.
✓ Branch 1 taken 409551 times.
829535 if (ret < 0)
271 419984 return ret;
272 }
273
274 398479 return 0;
275 }
276
277 822867 static int encode_receive_packet_internal(AVCodecContext *avctx, AVPacket *avpkt)
278 {
279 822867 AVCodecInternal *avci = avctx->internal;
280 int ret;
281
282
2/2
✓ Branch 0 taken 4404 times.
✓ Branch 1 taken 818463 times.
822867 if (avci->draining_done)
283 4404 return AVERROR_EOF;
284
285
2/4
✓ Branch 0 taken 818463 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 818463 times.
818463 av_assert0(!avpkt->data && !avpkt->side_data);
286
287
2/2
✓ Branch 0 taken 210881 times.
✓ Branch 1 taken 607582 times.
818463 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
288
3/4
✓ Branch 0 taken 404 times.
✓ Branch 1 taken 210477 times.
✓ Branch 2 taken 404 times.
✗ Branch 3 not taken.
210881 if ((avctx->flags & AV_CODEC_FLAG_PASS1) && avctx->stats_out)
289 404 avctx->stats_out[0] = '\0';
290
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 210881 times.
210881 if (av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx))
291 return AVERROR(EINVAL);
292 }
293
294
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 818463 times.
818463 if (ffcodec(avctx->codec)->cb_type == FF_CODEC_CB_TYPE_RECEIVE_PACKET) {
295 ret = ffcodec(avctx->codec)->cb.receive_packet(avctx, avpkt);
296 if (ret < 0)
297 av_packet_unref(avpkt);
298 else
299 // Encoders must always return ref-counted buffers.
300 // Side-data only packets have no data and can be not ref-counted.
301 av_assert0(!avpkt->data || avpkt->buf);
302 } else
303 818463 ret = encode_simple_receive_packet(avctx, avpkt);
304
305
2/2
✓ Branch 0 taken 6017 times.
✓ Branch 1 taken 812446 times.
818463 if (ret == AVERROR_EOF)
306 6017 avci->draining_done = 1;
307
308 818463 return ret;
309 }
310
311 404592 static int encode_send_frame_internal(AVCodecContext *avctx, const AVFrame *src)
312 {
313 404592 AVCodecInternal *avci = avctx->internal;
314 404592 AVFrame *dst = avci->buffer_frame;
315 int ret;
316
317
2/2
✓ Branch 0 taken 303196 times.
✓ Branch 1 taken 101396 times.
404592 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
318 /* extract audio service type metadata */
319 303196 AVFrameSideData *sd = av_frame_get_side_data(src, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
320
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 303196 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
303196 if (sd && sd->size >= sizeof(enum AVAudioServiceType))
321 avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
322
323 /* check for valid frame size */
324
2/2
✓ Branch 0 taken 12746 times.
✓ Branch 1 taken 290450 times.
303196 if (avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) {
325
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12746 times.
12746 if (src->nb_samples > avctx->frame_size) {
326 av_log(avctx, AV_LOG_ERROR, "more samples than frame size\n");
327 return AVERROR(EINVAL);
328 }
329
2/2
✓ Branch 0 taken 44518 times.
✓ Branch 1 taken 245932 times.
290450 } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
330 /* if we already got an undersized frame, that must have been the last */
331
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44518 times.
44518 if (avctx->internal->last_audio_frame) {
332 av_log(avctx, AV_LOG_ERROR, "frame_size (%d) was not respected for a non-last frame\n", avctx->frame_size);
333 return AVERROR(EINVAL);
334 }
335
336
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 44469 times.
44518 if (src->nb_samples < avctx->frame_size) {
337 49 ret = pad_last_frame(avctx, dst, src);
338
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 if (ret < 0)
339 return ret;
340
341 49 avctx->internal->last_audio_frame = 1;
342
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44469 times.
44469 } else if (src->nb_samples > avctx->frame_size) {
343 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d)\n", src->nb_samples, avctx->frame_size);
344 return AVERROR(EINVAL);
345 }
346 }
347 }
348
349
2/2
✓ Branch 0 taken 404543 times.
✓ Branch 1 taken 49 times.
404592 if (!dst->data[0]) {
350 404543 ret = av_frame_ref(dst, src);
351
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 404543 times.
404543 if (ret < 0)
352 return ret;
353 }
354
355 404592 return 0;
356 }
357
358 410609 int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
359 {
360 410609 AVCodecInternal *avci = avctx->internal;
361 int ret;
362
363
2/4
✓ Branch 1 taken 410609 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 410609 times.
410609 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
364 return AVERROR(EINVAL);
365
366
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 410609 times.
410609 if (avci->draining)
367 return AVERROR_EOF;
368
369
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 410609 times.
410609 if (avci->buffer_frame->buf[0])
370 return AVERROR(EAGAIN);
371
372
2/2
✓ Branch 0 taken 6017 times.
✓ Branch 1 taken 404592 times.
410609 if (!frame) {
373 6017 avci->draining = 1;
374 } else {
375 404592 ret = encode_send_frame_internal(avctx, frame);
376
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 404592 times.
404592 if (ret < 0)
377 return ret;
378 }
379
380
2/4
✓ Branch 0 taken 410609 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 410609 times.
✗ Branch 3 not taken.
410609 if (!avci->buffer_pkt->data && !avci->buffer_pkt->side_data) {
381 410609 ret = encode_receive_packet_internal(avctx, avci->buffer_pkt);
382
5/6
✓ Branch 0 taken 13779 times.
✓ Branch 1 taken 396830 times.
✓ Branch 2 taken 4404 times.
✓ Branch 3 taken 9375 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 4404 times.
410609 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
383 return ret;
384 }
385
386 410609 avctx->frame_number++;
387
388 410609 return 0;
389 }
390
391 809088 int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
392 {
393 809088 AVCodecInternal *avci = avctx->internal;
394 int ret;
395
396 809088 av_packet_unref(avpkt);
397
398
2/4
✓ Branch 1 taken 809088 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 809088 times.
809088 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
399 return AVERROR(EINVAL);
400
401
4/4
✓ Branch 0 taken 412274 times.
✓ Branch 1 taken 396814 times.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 412258 times.
809088 if (avci->buffer_pkt->data || avci->buffer_pkt->side_data) {
402 396830 av_packet_move_ref(avpkt, avci->buffer_pkt);
403 } else {
404 412258 ret = encode_receive_packet_internal(avctx, avpkt);
405
2/2
✓ Branch 0 taken 410609 times.
✓ Branch 1 taken 1649 times.
412258 if (ret < 0)
406 410609 return ret;
407 }
408
409 398479 return 0;
410 }
411
412 16986 static int encode_preinit_video(AVCodecContext *avctx)
413 {
414 16986 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt);
415 int i;
416
417
2/2
✓ Branch 0 taken 740 times.
✓ Branch 1 taken 16246 times.
16986 if (avctx->codec->pix_fmts) {
418
1/2
✓ Branch 0 taken 2163 times.
✗ Branch 1 not taken.
2163 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
419
2/2
✓ Branch 0 taken 740 times.
✓ Branch 1 taken 1423 times.
2163 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
420 740 break;
421
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 740 times.
740 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
422 char buf[128];
423 snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
424 av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
425 (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
426 return AVERROR(EINVAL);
427 }
428
2/2
✓ Branch 0 taken 718 times.
✓ Branch 1 taken 22 times.
740 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
429
1/2
✓ Branch 0 taken 718 times.
✗ Branch 1 not taken.
718 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ411P ||
430
2/2
✓ Branch 0 taken 714 times.
✓ Branch 1 taken 4 times.
718 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
431
1/2
✓ Branch 0 taken 714 times.
✗ Branch 1 not taken.
714 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
432
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 705 times.
714 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
433 35 avctx->color_range = AVCOL_RANGE_JPEG;
434 }
435
436
1/2
✓ Branch 0 taken 16986 times.
✗ Branch 1 not taken.
16986 if ( avctx->bits_per_raw_sample < 0
437
3/4
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 16921 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 65 times.
16986 || (avctx->bits_per_raw_sample > 8 && pixdesc->comp[0].depth <= 8)) {
438 av_log(avctx, AV_LOG_WARNING, "Specified bit depth %d not possible with the specified pixel formats depth %d\n",
439 avctx->bits_per_raw_sample, pixdesc->comp[0].depth);
440 avctx->bits_per_raw_sample = pixdesc->comp[0].depth;
441 }
442
2/4
✓ Branch 0 taken 16986 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 16986 times.
16986 if (avctx->width <= 0 || avctx->height <= 0) {
443 av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
444 return AVERROR(EINVAL);
445 }
446
447
2/4
✓ Branch 0 taken 16986 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16986 times.
✗ Branch 3 not taken.
16986 if (avctx->ticks_per_frame && avctx->time_base.num &&
448
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16986 times.
16986 avctx->ticks_per_frame > INT_MAX / avctx->time_base.num) {
449 av_log(avctx, AV_LOG_ERROR,
450 "ticks_per_frame %d too large for the timebase %d/%d.",
451 avctx->ticks_per_frame,
452 avctx->time_base.num,
453 avctx->time_base.den);
454 return AVERROR(EINVAL);
455 }
456
457
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16986 times.
16986 if (avctx->hw_frames_ctx) {
458 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
459 if (frames_ctx->format != avctx->pix_fmt) {
460 av_log(avctx, AV_LOG_ERROR,
461 "Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format\n");
462 return AVERROR(EINVAL);
463 }
464 if (avctx->sw_pix_fmt != AV_PIX_FMT_NONE &&
465 avctx->sw_pix_fmt != frames_ctx->sw_format) {
466 av_log(avctx, AV_LOG_ERROR,
467 "Mismatching AVCodecContext.sw_pix_fmt (%s) "
468 "and AVHWFramesContext.sw_format (%s)\n",
469 av_get_pix_fmt_name(avctx->sw_pix_fmt),
470 av_get_pix_fmt_name(frames_ctx->sw_format));
471 return AVERROR(EINVAL);
472 }
473 avctx->sw_pix_fmt = frames_ctx->sw_format;
474 }
475
476 16986 return 0;
477 }
478
479 1163 static int encode_preinit_audio(AVCodecContext *avctx)
480 {
481 int i;
482
483
1/2
✓ Branch 0 taken 1163 times.
✗ Branch 1 not taken.
1163 if (avctx->codec->sample_fmts) {
484
1/2
✓ Branch 0 taken 1175 times.
✗ Branch 1 not taken.
1175 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
485
2/2
✓ Branch 0 taken 1163 times.
✓ Branch 1 taken 12 times.
1175 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
486 1163 break;
487
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
15 if (avctx->ch_layout.nb_channels == 1 &&
488 3 av_get_planar_sample_fmt(avctx->sample_fmt) ==
489 3 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
490 avctx->sample_fmt = avctx->codec->sample_fmts[i];
491 break;
492 }
493 }
494
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1163 times.
1163 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
495 char buf[128];
496 snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
497 av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
498 (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
499 return AVERROR(EINVAL);
500 }
501 }
502
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 1115 times.
1163 if (avctx->codec->supported_samplerates) {
503
1/2
✓ Branch 0 taken 111 times.
✗ Branch 1 not taken.
111 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
504
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 63 times.
111 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
505 48 break;
506
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 if (avctx->codec->supported_samplerates[i] == 0) {
507 av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
508 avctx->sample_rate);
509 return AVERROR(EINVAL);
510 }
511 }
512
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1163 times.
1163 if (avctx->sample_rate < 0) {
513 av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
514 avctx->sample_rate);
515 return AVERROR(EINVAL);
516 }
517
2/2
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 1098 times.
1163 if (avctx->codec->ch_layouts) {
518
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 65 times.
65 if (!av_channel_layout_check(&avctx->ch_layout)) {
519 av_log(avctx, AV_LOG_WARNING, "Channel layout not specified correctly\n");
520 return AVERROR(EINVAL);
521 }
522
523
1/2
✓ Branch 0 taken 97 times.
✗ Branch 1 not taken.
97 for (i = 0; avctx->codec->ch_layouts[i].nb_channels; i++) {
524
2/2
✓ Branch 1 taken 65 times.
✓ Branch 2 taken 32 times.
97 if (!av_channel_layout_compare(&avctx->ch_layout, &avctx->codec->ch_layouts[i]))
525 65 break;
526 }
527
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
65 if (!avctx->codec->ch_layouts[i].nb_channels) {
528 char buf[512];
529 int ret = av_channel_layout_describe(&avctx->ch_layout, buf, sizeof(buf));
530 if (ret > 0)
531 av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
532 return AVERROR(EINVAL);
533 }
534 }
535 #if FF_API_OLD_CHANNEL_LAYOUT
536 FF_DISABLE_DEPRECATION_WARNINGS
537
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1163 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1163 if (avctx->channel_layout && avctx->channels) {
538 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
539 if (channels != avctx->channels) {
540 char buf[512];
541 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
542 av_log(avctx, AV_LOG_ERROR,
543 "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
544 buf, channels, avctx->channels);
545 return AVERROR(EINVAL);
546 }
547
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1163 times.
1163 } else if (avctx->channel_layout) {
548 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
549 }
550
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1163 times.
1163 if (avctx->channels < 0) {
551 av_log(avctx, AV_LOG_ERROR, "Specified number of channels %d is not supported\n",
552 avctx->channels);
553 return AVERROR(EINVAL);
554 }
555 FF_ENABLE_DEPRECATION_WARNINGS
556 #endif
557
558
2/2
✓ Branch 0 taken 1128 times.
✓ Branch 1 taken 35 times.
1163 if (!avctx->bits_per_raw_sample)
559 1128 avctx->bits_per_raw_sample = 8 * av_get_bytes_per_sample(avctx->sample_fmt);
560
561 1163 return 0;
562 }
563
564 18185 int ff_encode_preinit(AVCodecContext *avctx)
565 {
566 18185 AVCodecInternal *avci = avctx->internal;
567 18185 int ret = 0;
568
569
2/4
✓ Branch 0 taken 18185 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 18185 times.
18185 if (avctx->time_base.num <= 0 || avctx->time_base.den <= 0) {
570 av_log(avctx, AV_LOG_ERROR, "The encoder timebase is not set.\n");
571 return AVERROR(EINVAL);
572 }
573
574
3/3
✓ Branch 0 taken 16986 times.
✓ Branch 1 taken 1163 times.
✓ Branch 2 taken 36 times.
18185 switch (avctx->codec_type) {
575 16986 case AVMEDIA_TYPE_VIDEO: ret = encode_preinit_video(avctx); break;
576 1163 case AVMEDIA_TYPE_AUDIO: ret = encode_preinit_audio(avctx); break;
577 }
578
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18185 times.
18185 if (ret < 0)
579 return ret;
580
581
4/4
✓ Branch 0 taken 1199 times.
✓ Branch 1 taken 16986 times.
✓ Branch 2 taken 1163 times.
✓ Branch 3 taken 36 times.
18185 if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
582
3/4
✓ Branch 0 taken 18129 times.
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18129 times.
18149 && avctx->bit_rate>0 && avctx->bit_rate<1000) {
583 av_log(avctx, AV_LOG_WARNING, "Bitrate %"PRId64" is extremely low, maybe you mean %"PRId64"k\n", avctx->bit_rate, avctx->bit_rate);
584 }
585
586
2/2
✓ Branch 0 taken 18183 times.
✓ Branch 1 taken 2 times.
18185 if (!avctx->rc_initial_buffer_occupancy)
587 18183 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3LL / 4;
588
589
2/2
✓ Branch 0 taken 17848 times.
✓ Branch 1 taken 337 times.
18185 if (avctx->codec_descriptor->props & AV_CODEC_PROP_INTRA_ONLY)
590 17848 avctx->internal->intra_only_flag = AV_PKT_FLAG_KEY;
591
592
2/2
✓ Branch 1 taken 18149 times.
✓ Branch 2 taken 36 times.
18185 if (ffcodec(avctx->codec)->cb_type == FF_CODEC_CB_TYPE_ENCODE) {
593 18149 avci->in_frame = av_frame_alloc();
594
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18149 times.
18149 if (!avci->in_frame)
595 return AVERROR(ENOMEM);
596 }
597
598 18185 return 0;
599 }
600
601 9089 int ff_encode_alloc_frame(AVCodecContext *avctx, AVFrame *frame)
602 {
603 int ret;
604
605
1/3
✓ Branch 0 taken 9089 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
9089 switch (avctx->codec->type) {
606 9089 case AVMEDIA_TYPE_VIDEO:
607 9089 frame->format = avctx->pix_fmt;
608
3/4
✓ Branch 0 taken 9073 times.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9073 times.
9089 if (frame->width <= 0 || frame->height <= 0) {
609 16 frame->width = FFMAX(avctx->width, avctx->coded_width);
610 16 frame->height = FFMAX(avctx->height, avctx->coded_height);
611 }
612
613 9089 break;
614 case AVMEDIA_TYPE_AUDIO:
615 frame->sample_rate = avctx->sample_rate;
616 frame->format = avctx->sample_fmt;
617 if (!frame->ch_layout.nb_channels) {
618 ret = av_channel_layout_copy(&frame->ch_layout, &avctx->ch_layout);
619 if (ret < 0)
620 return ret;
621 }
622 break;
623 }
624
625 9089 ret = avcodec_default_get_buffer2(avctx, frame, 0);
626
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9089 times.
9089 if (ret < 0) {
627 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
628 av_frame_unref(frame);
629 return ret;
630 }
631
632 9089 return 0;
633 }
634