FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/encode.c
Date: 2021-09-24 20:55:06
Exec Total Coverage
Lines: 220 310 71.0%
Branches: 209 306 68.3%

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 "encode.h"
31 #include "frame_thread_encoder.h"
32 #include "internal.h"
33
34 32775 int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
35 {
36
2/4
✓ Branch 0 taken 32775 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 32775 times.
32775 if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
37 av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
38 size, INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
39 return AVERROR(EINVAL);
40 }
41
42
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32775 times.
32775 av_assert0(!avpkt->data);
43
44 32775 av_fast_padded_malloc(&avctx->internal->byte_buffer,
45 32775 &avctx->internal->byte_buffer_size, size);
46 32775 avpkt->data = avctx->internal->byte_buffer;
47
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32775 times.
32775 if (!avpkt->data) {
48 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
49 return AVERROR(ENOMEM);
50 }
51 32775 avpkt->size = size;
52
53 32775 return 0;
54 }
55
56 364967 int avcodec_default_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int flags)
57 {
58 int ret;
59
60
2/4
✓ Branch 0 taken 364967 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 364967 times.
364967 if (avpkt->size < 0 || avpkt->size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
61 return AVERROR(EINVAL);
62
63
2/4
✓ Branch 0 taken 364967 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 364967 times.
364967 if (avpkt->data || avpkt->buf) {
64 av_log(avctx, AV_LOG_ERROR, "avpkt->{data,buf} != NULL in avcodec_default_get_encode_buffer()\n");
65 return AVERROR(EINVAL);
66 }
67
68 364967 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
69
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 364967 times.
364967 if (ret < 0) {
70 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", avpkt->size);
71 return ret;
72 }
73 364967 avpkt->data = avpkt->buf->data;
74
75 364967 return 0;
76 }
77
78 364967 int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
79 {
80 int ret;
81
82
2/4
✓ Branch 0 taken 364967 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 364967 times.
364967 if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
83 return AVERROR(EINVAL);
84
85
2/4
✓ Branch 0 taken 364967 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 364967 times.
364967 av_assert0(!avpkt->data && !avpkt->buf);
86
87 364967 avpkt->size = size;
88 364967 ret = avctx->get_encode_buffer(avctx, avpkt, flags);
89
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 364967 times.
364967 if (ret < 0)
90 goto fail;
91
92
2/4
✓ Branch 0 taken 364967 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 364967 times.
364967 if (!avpkt->data || !avpkt->buf) {
93 av_log(avctx, AV_LOG_ERROR, "No buffer returned by get_encode_buffer()\n");
94 ret = AVERROR(EINVAL);
95 goto fail;
96 }
97 364967 memset(avpkt->data + avpkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
98
99 364967 ret = 0;
100 364967 fail:
101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 364967 times.
364967 if (ret < 0) {
102 av_log(avctx, AV_LOG_ERROR, "get_encode_buffer() failed\n");
103 av_packet_unref(avpkt);
104 }
105
106 364967 return ret;
107 }
108
109 /**
110 * Pad last frame with silence.
111 */
112 49 static int pad_last_frame(AVCodecContext *s, AVFrame *frame, const AVFrame *src)
113 {
114 int ret;
115
116 49 frame->format = src->format;
117 49 frame->channel_layout = src->channel_layout;
118 49 frame->channels = src->channels;
119 49 frame->nb_samples = s->frame_size;
120 49 ret = av_frame_get_buffer(frame, 0);
121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 if (ret < 0)
122 goto fail;
123
124 49 ret = av_frame_copy_props(frame, src);
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 if (ret < 0)
126 goto fail;
127
128
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,
129 49 src->nb_samples, s->channels, s->sample_fmt)) < 0)
130 goto fail;
131
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
132 49 frame->nb_samples - src->nb_samples,
133 s->channels, s->sample_fmt)) < 0)
134 goto fail;
135
136 49 return 0;
137
138 fail:
139 av_frame_unref(frame);
140 return ret;
141 }
142
143 756 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
144 const AVSubtitle *sub)
145 {
146 int ret;
147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 756 times.
756 if (sub->start_display_time) {
148 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
149 return -1;
150 }
151
152 756 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
153 756 avctx->frame_number++;
154 756 return ret;
155 }
156
157 820034 int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame)
158 {
159 820034 AVCodecInternal *avci = avctx->internal;
160
161
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 820034 times.
820034 if (avci->draining)
162 return AVERROR_EOF;
163
164
2/2
✓ Branch 0 taken 416129 times.
✓ Branch 1 taken 403905 times.
820034 if (!avci->buffer_frame->buf[0])
165 416129 return AVERROR(EAGAIN);
166
167 403905 av_frame_move_ref(frame, avci->buffer_frame);
168
169 403905 return 0;
170 }
171
172 826333 static int encode_simple_internal(AVCodecContext *avctx, AVPacket *avpkt)
173 {
174 826333 AVCodecInternal *avci = avctx->internal;
175 826333 EncodeSimpleContext *es = &avci->es;
176 826333 AVFrame *frame = es->in_frame;
177 int got_packet;
178 int ret;
179
180
2/2
✓ Branch 0 taken 188 times.
✓ Branch 1 taken 826145 times.
826333 if (avci->draining_done)
181 188 return AVERROR_EOF;
182
183
3/4
✓ Branch 0 taken 826145 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 820034 times.
✓ Branch 3 taken 6111 times.
826145 if (!frame->buf[0] && !avci->draining) {
184 820034 av_frame_unref(frame);
185 820034 ret = ff_encode_get_frame(avctx, frame);
186
3/4
✓ Branch 0 taken 416129 times.
✓ Branch 1 taken 403905 times.
✓ Branch 2 taken 416129 times.
✗ Branch 3 not taken.
820034 if (ret < 0 && ret != AVERROR_EOF)
187 416129 return ret;
188 }
189
190
2/2
✓ Branch 0 taken 6111 times.
✓ Branch 1 taken 403905 times.
410016 if (!frame->buf[0]) {
191
2/2
✓ Branch 0 taken 5781 times.
✓ Branch 1 taken 330 times.
6111 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
192
3/4
✓ Branch 0 taken 149 times.
✓ Branch 1 taken 5632 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 149 times.
5781 (avci->frame_thread_encoder && avctx->active_thread_type & FF_THREAD_FRAME)))
193 5632 return AVERROR_EOF;
194
195 // Flushing is signaled with a NULL frame
196 479 frame = NULL;
197 }
198
199 404384 got_packet = 0;
200
201
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 404384 times.
404384 av_assert0(avctx->codec->encode2);
202
203 404384 if (CONFIG_FRAME_THREAD_ENCODER &&
204
3/4
✓ Branch 0 taken 914 times.
✓ Branch 1 taken 403470 times.
✓ Branch 2 taken 914 times.
✗ Branch 3 not taken.
404384 avci->frame_thread_encoder && (avctx->active_thread_type & FF_THREAD_FRAME))
205 /* This might modify frame, but it doesn't matter, because
206 * the frame properties used below are not used for video
207 * (due to the delay inherent in frame threaded encoding, it makes
208 * no sense to use the properties of the current frame anyway). */
209 914 ret = ff_thread_video_encode_frame(avctx, avpkt, frame, &got_packet);
210 else {
211 403470 ret = avctx->codec->encode2(avctx, avpkt, frame, &got_packet);
212
5/6
✓ Branch 0 taken 99123 times.
✓ Branch 1 taken 304347 times.
✓ Branch 2 taken 99123 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 98868 times.
✓ Branch 5 taken 255 times.
403470 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO && !ret && got_packet &&
213
2/2
✓ Branch 0 taken 92807 times.
✓ Branch 1 taken 6061 times.
98868 !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
214 92807 avpkt->pts = avpkt->dts = frame->pts;
215 }
216
217
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 404384 times.
404384 av_assert0(ret <= 0);
218
219 404384 emms_c();
220
221
3/4
✓ Branch 0 taken 404384 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 397792 times.
✓ Branch 3 taken 6592 times.
404384 if (!ret && got_packet) {
222
2/2
✓ Branch 0 taken 397777 times.
✓ Branch 1 taken 15 times.
397792 if (avpkt->data) {
223 397777 ret = av_packet_make_refcounted(avpkt);
224
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 397777 times.
397777 if (ret < 0)
225 goto end;
226 }
227
228
4/4
✓ Branch 0 taken 397501 times.
✓ Branch 1 taken 291 times.
✓ Branch 2 taken 365500 times.
✓ Branch 3 taken 32001 times.
397792 if (frame && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
229
2/2
✓ Branch 0 taken 272059 times.
✓ Branch 1 taken 93441 times.
365500 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
230
2/2
✓ Branch 0 taken 261011 times.
✓ Branch 1 taken 11048 times.
272059 if (avpkt->pts == AV_NOPTS_VALUE)
231 261011 avpkt->pts = frame->pts;
232
2/2
✓ Branch 0 taken 271004 times.
✓ Branch 1 taken 1055 times.
272059 if (!avpkt->duration)
233 271004 avpkt->duration = ff_samples_to_time_base(avctx,
234 271004 frame->nb_samples);
235 }
236 }
237
2/2
✓ Branch 0 taken 298159 times.
✓ Branch 1 taken 99633 times.
397792 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
238 /* NOTE: if we add any audio encoders which output non-keyframe packets,
239 * this needs to be moved to the encoders, but for now we can do it
240 * here to simplify things */
241 298159 avpkt->flags |= AV_PKT_FLAG_KEY;
242 298159 avpkt->dts = avpkt->pts;
243 }
244 }
245
246
4/4
✓ Branch 0 taken 403905 times.
✓ Branch 1 taken 479 times.
✓ Branch 2 taken 291 times.
✓ Branch 3 taken 188 times.
404384 if (avci->draining && !got_packet)
247 188 avci->draining_done = 1;
248
249 404196 end:
250
3/4
✓ Branch 0 taken 404384 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6592 times.
✓ Branch 3 taken 397792 times.
404384 if (ret < 0 || !got_packet)
251 6592 av_packet_unref(avpkt);
252
253
2/2
✓ Branch 0 taken 403905 times.
✓ Branch 1 taken 479 times.
404384 if (frame) {
254
1/2
✓ Branch 0 taken 403905 times.
✗ Branch 1 not taken.
403905 if (!ret)
255 403905 avctx->frame_number++;
256 403905 av_frame_unref(frame);
257 }
258
259
2/2
✓ Branch 0 taken 397792 times.
✓ Branch 1 taken 6592 times.
404384 if (got_packet)
260 // Encoders must always return ref-counted buffers.
261 // Side-data only packets have no data and can be not ref-counted.
262
3/4
✓ Branch 0 taken 397777 times.
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 397777 times.
397792 av_assert0(!avpkt->data || avpkt->buf);
263
264 404384 return ret;
265 }
266
267 819741 static int encode_simple_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
268 {
269 int ret;
270
271
4/4
✓ Branch 0 taken 826348 times.
✓ Branch 1 taken 397777 times.
✓ Branch 2 taken 826333 times.
✓ Branch 3 taken 15 times.
1224125 while (!avpkt->data && !avpkt->side_data) {
272 826333 ret = encode_simple_internal(avctx, avpkt);
273
2/2
✓ Branch 0 taken 421949 times.
✓ Branch 1 taken 404384 times.
826333 if (ret < 0)
274 421949 return ret;
275 }
276
277 397792 return 0;
278 }
279
280 825442 static int encode_receive_packet_internal(AVCodecContext *avctx, AVPacket *avpkt)
281 {
282 825442 AVCodecInternal *avci = avctx->internal;
283 int ret;
284
285
2/2
✓ Branch 0 taken 5701 times.
✓ Branch 1 taken 819741 times.
825442 if (avci->draining_done)
286 5701 return AVERROR_EOF;
287
288
2/4
✓ Branch 0 taken 819741 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 819741 times.
819741 av_assert0(!avpkt->data && !avpkt->side_data);
289
290
2/2
✓ Branch 0 taken 208892 times.
✓ Branch 1 taken 610849 times.
819741 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
291
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 208892 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
208892 if ((avctx->flags & AV_CODEC_FLAG_PASS1) && avctx->stats_out)
292 avctx->stats_out[0] = '\0';
293
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 208892 times.
208892 if (av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx))
294 return AVERROR(EINVAL);
295 }
296
297
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 819741 times.
819741 if (avctx->codec->receive_packet) {
298 ret = avctx->codec->receive_packet(avctx, avpkt);
299 if (ret < 0)
300 av_packet_unref(avpkt);
301 else
302 // Encoders must always return ref-counted buffers.
303 // Side-data only packets have no data and can be not ref-counted.
304 av_assert0(!avpkt->data || avpkt->buf);
305 } else
306 819741 ret = encode_simple_receive_packet(avctx, avpkt);
307
308
2/2
✓ Branch 0 taken 5820 times.
✓ Branch 1 taken 813921 times.
819741 if (ret == AVERROR_EOF)
309 5820 avci->draining_done = 1;
310
311 819741 return ret;
312 }
313
314 403905 static int encode_send_frame_internal(AVCodecContext *avctx, const AVFrame *src)
315 {
316 403905 AVCodecInternal *avci = avctx->internal;
317 403905 AVFrame *dst = avci->buffer_frame;
318 int ret;
319
320
2/2
✓ Branch 0 taken 304272 times.
✓ Branch 1 taken 99633 times.
403905 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
321 /* extract audio service type metadata */
322 304272 AVFrameSideData *sd = av_frame_get_side_data(src, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
323
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 304272 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
304272 if (sd && sd->size >= sizeof(enum AVAudioServiceType))
324 avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
325
326 /* check for valid frame size */
327
2/2
✓ Branch 0 taken 12742 times.
✓ Branch 1 taken 291530 times.
304272 if (avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) {
328
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12742 times.
12742 if (src->nb_samples > avctx->frame_size) {
329 av_log(avctx, AV_LOG_ERROR, "more samples than frame size\n");
330 return AVERROR(EINVAL);
331 }
332
2/2
✓ Branch 0 taken 52512 times.
✓ Branch 1 taken 239018 times.
291530 } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
333 /* if we already got an undersized frame, that must have been the last */
334
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52512 times.
52512 if (avctx->internal->last_audio_frame) {
335 av_log(avctx, AV_LOG_ERROR, "frame_size (%d) was not respected for a non-last frame\n", avctx->frame_size);
336 return AVERROR(EINVAL);
337 }
338
339
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 52463 times.
52512 if (src->nb_samples < avctx->frame_size) {
340 49 ret = pad_last_frame(avctx, dst, src);
341
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 if (ret < 0)
342 return ret;
343
344 49 avctx->internal->last_audio_frame = 1;
345
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52463 times.
52463 } else if (src->nb_samples > avctx->frame_size) {
346 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d)\n", src->nb_samples, avctx->frame_size);
347 return AVERROR(EINVAL);
348 }
349 }
350 }
351
352
2/2
✓ Branch 0 taken 403856 times.
✓ Branch 1 taken 49 times.
403905 if (!dst->data[0]) {
353 403856 ret = av_frame_ref(dst, src);
354
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 403856 times.
403856 if (ret < 0)
355 return ret;
356 }
357
358 403905 return 0;
359 }
360
361 409725 int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
362 {
363 409725 AVCodecInternal *avci = avctx->internal;
364 int ret;
365
366
2/4
✓ Branch 1 taken 409725 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 409725 times.
409725 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
367 return AVERROR(EINVAL);
368
369
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 409725 times.
409725 if (avci->draining)
370 return AVERROR_EOF;
371
372
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 409725 times.
409725 if (avci->buffer_frame->data[0])
373 return AVERROR(EAGAIN);
374
375
2/2
✓ Branch 0 taken 5820 times.
✓ Branch 1 taken 403905 times.
409725 if (!frame) {
376 5820 avci->draining = 1;
377 } else {
378 403905 ret = encode_send_frame_internal(avctx, frame);
379
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 403905 times.
403905 if (ret < 0)
380 return ret;
381 }
382
383
2/4
✓ Branch 0 taken 409725 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 409725 times.
✗ Branch 3 not taken.
409725 if (!avci->buffer_pkt->data && !avci->buffer_pkt->side_data) {
384 409725 ret = encode_receive_packet_internal(avctx, avci->buffer_pkt);
385
5/6
✓ Branch 0 taken 12105 times.
✓ Branch 1 taken 397620 times.
✓ Branch 2 taken 5701 times.
✓ Branch 3 taken 6404 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5701 times.
409725 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
386 return ret;
387 }
388
389 409725 return 0;
390 }
391
392 813337 int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
393 {
394 813337 AVCodecInternal *avci = avctx->internal;
395 int ret;
396
397 813337 av_packet_unref(avpkt);
398
399
2/4
✓ Branch 1 taken 813337 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 813337 times.
813337 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
400 return AVERROR(EINVAL);
401
402
4/4
✓ Branch 0 taken 415732 times.
✓ Branch 1 taken 397605 times.
✓ Branch 2 taken 15 times.
✓ Branch 3 taken 415717 times.
813337 if (avci->buffer_pkt->data || avci->buffer_pkt->side_data) {
403 397620 av_packet_move_ref(avpkt, avci->buffer_pkt);
404 } else {
405 415717 ret = encode_receive_packet_internal(avctx, avpkt);
406
2/2
✓ Branch 0 taken 415545 times.
✓ Branch 1 taken 172 times.
415717 if (ret < 0)
407 415545 return ret;
408 }
409
410 397792 return 0;
411 }
412
413 6002 int ff_encode_preinit(AVCodecContext *avctx)
414 {
415 int i;
416
417
2/4
✓ Branch 0 taken 6002 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6002 times.
6002 if (avctx->time_base.num <= 0 || avctx->time_base.den <= 0) {
418 av_log(avctx, AV_LOG_ERROR, "The encoder timebase is not set.\n");
419 return AVERROR(EINVAL);
420 }
421
422
2/2
✓ Branch 0 taken 1147 times.
✓ Branch 1 taken 4855 times.
6002 if (avctx->codec->sample_fmts) {
423
1/2
✓ Branch 0 taken 1159 times.
✗ Branch 1 not taken.
1159 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
424
2/2
✓ Branch 0 taken 1147 times.
✓ Branch 1 taken 12 times.
1159 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
425 1147 break;
426
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
15 if (avctx->channels == 1 &&
427 3 av_get_planar_sample_fmt(avctx->sample_fmt) ==
428 3 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
429 avctx->sample_fmt = avctx->codec->sample_fmts[i];
430 break;
431 }
432 }
433
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1147 times.
1147 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
434 char buf[128];
435 snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
436 av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
437 (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
438 return AVERROR(EINVAL);
439 }
440 }
441
2/2
✓ Branch 0 taken 702 times.
✓ Branch 1 taken 5300 times.
6002 if (avctx->codec->pix_fmts) {
442
1/2
✓ Branch 0 taken 2101 times.
✗ Branch 1 not taken.
2101 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
443
2/2
✓ Branch 0 taken 702 times.
✓ Branch 1 taken 1399 times.
2101 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
444 702 break;
445
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 702 times.
702 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
446 char buf[128];
447 snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
448 av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
449 (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
450 return AVERROR(EINVAL);
451 }
452
2/2
✓ Branch 0 taken 680 times.
✓ Branch 1 taken 22 times.
702 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
453
1/2
✓ Branch 0 taken 680 times.
✗ Branch 1 not taken.
680 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ411P ||
454
2/2
✓ Branch 0 taken 676 times.
✓ Branch 1 taken 4 times.
680 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
455
1/2
✓ Branch 0 taken 676 times.
✗ Branch 1 not taken.
676 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
456
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 668 times.
676 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
457 34 avctx->color_range = AVCOL_RANGE_JPEG;
458 }
459
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 5958 times.
6002 if (avctx->codec->supported_samplerates) {
460
1/2
✓ Branch 0 taken 103 times.
✗ Branch 1 not taken.
103 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
461
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 59 times.
103 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
462 44 break;
463
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 if (avctx->codec->supported_samplerates[i] == 0) {
464 av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
465 avctx->sample_rate);
466 return AVERROR(EINVAL);
467 }
468 }
469
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6002 times.
6002 if (avctx->sample_rate < 0) {
470 av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
471 avctx->sample_rate);
472 return AVERROR(EINVAL);
473 }
474
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 5960 times.
6002 if (avctx->codec->channel_layouts) {
475
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (!avctx->channel_layout) {
476 av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
477 } else {
478
1/2
✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
58 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
479
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 16 times.
58 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
480 42 break;
481
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (avctx->codec->channel_layouts[i] == 0) {
482 char buf[512];
483 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
484 av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
485 return AVERROR(EINVAL);
486 }
487 }
488 }
489
4/4
✓ Branch 0 taken 1139 times.
✓ Branch 1 taken 4863 times.
✓ Branch 2 taken 1123 times.
✓ Branch 3 taken 16 times.
7125 if (avctx->channel_layout && avctx->channels) {
490 1123 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
491
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1123 times.
1123 if (channels != avctx->channels) {
492 char buf[512];
493 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
494 av_log(avctx, AV_LOG_ERROR,
495 "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
496 buf, channels, avctx->channels);
497 return AVERROR(EINVAL);
498 }
499
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4863 times.
4879 } else if (avctx->channel_layout) {
500 16 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
501 }
502
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6002 times.
6002 if (avctx->channels < 0) {
503 av_log(avctx, AV_LOG_ERROR, "Specified number of channels %d is not supported\n",
504 avctx->channels);
505 return AVERROR(EINVAL);
506 }
507
2/2
✓ Branch 0 taken 4821 times.
✓ Branch 1 taken 1181 times.
6002 if(avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
508 4821 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt);
509
1/2
✓ Branch 0 taken 4821 times.
✗ Branch 1 not taken.
4821 if ( avctx->bits_per_raw_sample < 0
510
3/4
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 4778 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 43 times.
4821 || (avctx->bits_per_raw_sample > 8 && pixdesc->comp[0].depth <= 8)) {
511 av_log(avctx, AV_LOG_WARNING, "Specified bit depth %d not possible with the specified pixel formats depth %d\n",
512 avctx->bits_per_raw_sample, pixdesc->comp[0].depth);
513 avctx->bits_per_raw_sample = pixdesc->comp[0].depth;
514 }
515
2/4
✓ Branch 0 taken 4821 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4821 times.
4821 if (avctx->width <= 0 || avctx->height <= 0) {
516 av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
517 return AVERROR(EINVAL);
518 }
519 }
520
4/4
✓ Branch 0 taken 1181 times.
✓ Branch 1 taken 4821 times.
✓ Branch 2 taken 1147 times.
✓ Branch 3 taken 34 times.
6002 if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
521
3/4
✓ Branch 0 taken 5952 times.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5952 times.
5968 && avctx->bit_rate>0 && avctx->bit_rate<1000) {
522 av_log(avctx, AV_LOG_WARNING, "Bitrate %"PRId64" is extremely low, maybe you mean %"PRId64"k\n", avctx->bit_rate, avctx->bit_rate);
523 }
524
525
2/2
✓ Branch 0 taken 6000 times.
✓ Branch 1 taken 2 times.
6002 if (!avctx->rc_initial_buffer_occupancy)
526 6000 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3LL / 4;
527
528
3/4
✓ Branch 0 taken 5968 times.
✓ Branch 1 taken 34 times.
✓ Branch 2 taken 5968 times.
✗ Branch 3 not taken.
6002 if (avctx->ticks_per_frame && avctx->time_base.num &&
529
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5968 times.
5968 avctx->ticks_per_frame > INT_MAX / avctx->time_base.num) {
530 av_log(avctx, AV_LOG_ERROR,
531 "ticks_per_frame %d too large for the timebase %d/%d.",
532 avctx->ticks_per_frame,
533 avctx->time_base.num,
534 avctx->time_base.den);
535 return AVERROR(EINVAL);
536 }
537
538
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6002 times.
6002 if (avctx->hw_frames_ctx) {
539 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
540 if (frames_ctx->format != avctx->pix_fmt) {
541 av_log(avctx, AV_LOG_ERROR,
542 "Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format\n");
543 return AVERROR(EINVAL);
544 }
545 if (avctx->sw_pix_fmt != AV_PIX_FMT_NONE &&
546 avctx->sw_pix_fmt != frames_ctx->sw_format) {
547 av_log(avctx, AV_LOG_ERROR,
548 "Mismatching AVCodecContext.sw_pix_fmt (%s) "
549 "and AVHWFramesContext.sw_format (%s)\n",
550 av_get_pix_fmt_name(avctx->sw_pix_fmt),
551 av_get_pix_fmt_name(frames_ctx->sw_format));
552 return AVERROR(EINVAL);
553 }
554 avctx->sw_pix_fmt = frames_ctx->sw_format;
555 }
556
557 6002 return 0;
558 }
559