FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/encode.c
Date: 2022-01-16 20:33:26
Exec Total Coverage
Lines: 221 311 71.1%
Branches: 210 306 68.6%

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 32878 int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
35 {
36
2/4
✓ Branch 0 taken 32878 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 32878 times.
32878 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 32878 times.
32878 av_assert0(!avpkt->data);
43
44 32878 av_fast_padded_malloc(&avctx->internal->byte_buffer,
45 32878 &avctx->internal->byte_buffer_size, size);
46 32878 avpkt->data = avctx->internal->byte_buffer;
47
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32878 times.
32878 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 32878 avpkt->size = size;
52
53 32878 return 0;
54 }
55
56 371033 int avcodec_default_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int flags)
57 {
58 int ret;
59
60
2/4
✓ Branch 0 taken 371033 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 371033 times.
371033 if (avpkt->size < 0 || avpkt->size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
61 return AVERROR(EINVAL);
62
63
2/4
✓ Branch 0 taken 371033 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 371033 times.
371033 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 371033 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
69
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 371033 times.
371033 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 371033 avpkt->data = avpkt->buf->data;
74
75 371033 return 0;
76 }
77
78 371033 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 371033 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 371033 times.
371033 if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
83 return AVERROR(EINVAL);
84
85
2/4
✓ Branch 0 taken 371033 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 371033 times.
371033 av_assert0(!avpkt->data && !avpkt->buf);
86
87 371033 avpkt->size = size;
88 371033 ret = avctx->get_encode_buffer(avctx, avpkt, flags);
89
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 371033 times.
371033 if (ret < 0)
90 goto fail;
91
92
2/4
✓ Branch 0 taken 371033 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 371033 times.
371033 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 371033 memset(avpkt->data + avpkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
98
99 371033 ret = 0;
100 371033 fail:
101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 371033 times.
371033 if (ret < 0) {
102 av_log(avctx, AV_LOG_ERROR, "get_encode_buffer() failed\n");
103 av_packet_unref(avpkt);
104 }
105
106 371033 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 799 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 799 times.
799 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 799 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
153 799 avctx->frame_number++;
154 799 return ret;
155 }
156
157 835450 int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame)
158 {
159 835450 AVCodecInternal *avci = avctx->internal;
160
161
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 835450 times.
835450 if (avci->draining)
162 return AVERROR_EOF;
163
164
2/2
✓ Branch 0 taken 425376 times.
✓ Branch 1 taken 410074 times.
835450 if (!avci->buffer_frame->buf[0])
165 425376 return AVERROR(EAGAIN);
166
167 410074 av_frame_move_ref(frame, avci->buffer_frame);
168
169 410074 return 0;
170 }
171
172 846307 static int encode_simple_internal(AVCodecContext *avctx, AVPacket *avpkt)
173 {
174 846307 AVCodecInternal *avci = avctx->internal;
175 846307 EncodeSimpleContext *es = &avci->es;
176 846307 AVFrame *frame = es->in_frame;
177 int got_packet;
178 int ret;
179
180
2/2
✓ Branch 0 taken 1667 times.
✓ Branch 1 taken 844640 times.
846307 if (avci->draining_done)
181 1667 return AVERROR_EOF;
182
183
3/4
✓ Branch 0 taken 844640 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 835450 times.
✓ Branch 3 taken 9190 times.
844640 if (!frame->buf[0] && !avci->draining) {
184 835450 av_frame_unref(frame);
185 835450 ret = ff_encode_get_frame(avctx, frame);
186
3/4
✓ Branch 0 taken 425376 times.
✓ Branch 1 taken 410074 times.
✓ Branch 2 taken 425376 times.
✗ Branch 3 not taken.
835450 if (ret < 0 && ret != AVERROR_EOF)
187 425376 return ret;
188 }
189
190
2/2
✓ Branch 0 taken 9190 times.
✓ Branch 1 taken 410074 times.
419264 if (!frame->buf[0]) {
191
2/2
✓ Branch 0 taken 8856 times.
✓ Branch 1 taken 334 times.
9190 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
192
3/4
✓ Branch 0 taken 4588 times.
✓ Branch 1 taken 4268 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4588 times.
8856 (avci->frame_thread_encoder && avctx->active_thread_type & FF_THREAD_FRAME)))
193 4268 return AVERROR_EOF;
194
195 // Flushing is signaled with a NULL frame
196 4922 frame = NULL;
197 }
198
199 414996 got_packet = 0;
200
201
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 414996 times.
414996 av_assert0(avctx->codec->encode2);
202
203 414996 if (CONFIG_FRAME_THREAD_ENCODER &&
204
3/4
✓ Branch 0 taken 59028 times.
✓ Branch 1 taken 355968 times.
✓ Branch 2 taken 59028 times.
✗ Branch 3 not taken.
414996 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 59028 ret = ff_thread_video_encode_frame(avctx, avpkt, frame, &got_packet);
210 else {
211 355968 ret = avctx->codec->encode2(avctx, avpkt, frame, &got_packet);
212
5/6
✓ Branch 0 taken 45761 times.
✓ Branch 1 taken 310207 times.
✓ Branch 2 taken 45761 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 45504 times.
✓ Branch 5 taken 257 times.
355968 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO && !ret && got_packet &&
213
2/2
✓ Branch 0 taken 39343 times.
✓ Branch 1 taken 6161 times.
45504 !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
214 39343 avpkt->pts = avpkt->dts = frame->pts;
215 }
216
217
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 414996 times.
414996 av_assert0(ret <= 0);
218
219 414996 emms_c();
220
221
3/4
✓ Branch 0 taken 414996 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 403962 times.
✓ Branch 3 taken 11034 times.
414996 if (!ret && got_packet) {
222
2/2
✓ Branch 0 taken 403946 times.
✓ Branch 1 taken 16 times.
403962 if (avpkt->data) {
223 403946 ret = av_packet_make_refcounted(avpkt);
224
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 403946 times.
403946 if (ret < 0)
225 goto end;
226 }
227
228
4/4
✓ Branch 0 taken 400707 times.
✓ Branch 1 taken 3255 times.
✓ Branch 2 taken 368602 times.
✓ Branch 3 taken 32105 times.
403962 if (frame && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
229
2/2
✓ Branch 0 taken 277913 times.
✓ Branch 1 taken 90689 times.
368602 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
230
2/2
✓ Branch 0 taken 266691 times.
✓ Branch 1 taken 11222 times.
277913 if (avpkt->pts == AV_NOPTS_VALUE)
231 266691 avpkt->pts = frame->pts;
232
2/2
✓ Branch 0 taken 276858 times.
✓ Branch 1 taken 1055 times.
277913 if (!avpkt->duration)
233 276858 avpkt->duration = ff_samples_to_time_base(avctx,
234 276858 frame->nb_samples);
235 }
236 }
237
2/2
✓ Branch 0 taken 304018 times.
✓ Branch 1 taken 99944 times.
403962 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
238 304018 avpkt->dts = avpkt->pts;
239 }
240 403962 avpkt->flags |= avci->intra_only_flag;
241 }
242
243
4/4
✓ Branch 0 taken 410074 times.
✓ Branch 1 taken 4922 times.
✓ Branch 2 taken 3255 times.
✓ Branch 3 taken 1667 times.
414996 if (avci->draining && !got_packet)
244 1667 avci->draining_done = 1;
245
246 413329 end:
247
3/4
✓ Branch 0 taken 414996 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11034 times.
✓ Branch 3 taken 403962 times.
414996 if (ret < 0 || !got_packet)
248 11034 av_packet_unref(avpkt);
249
250
2/2
✓ Branch 0 taken 410074 times.
✓ Branch 1 taken 4922 times.
414996 if (frame)
251 410074 av_frame_unref(frame);
252
253
2/2
✓ Branch 0 taken 403962 times.
✓ Branch 1 taken 11034 times.
414996 if (got_packet)
254 // Encoders must always return ref-counted buffers.
255 // Side-data only packets have no data and can be not ref-counted.
256
3/4
✓ Branch 0 taken 403946 times.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 403946 times.
403962 av_assert0(!avpkt->data || avpkt->buf);
257
258 414996 return ret;
259 }
260
261 835273 static int encode_simple_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
262 {
263 int ret;
264
265
4/4
✓ Branch 0 taken 846323 times.
✓ Branch 1 taken 403946 times.
✓ Branch 2 taken 846307 times.
✓ Branch 3 taken 16 times.
1250269 while (!avpkt->data && !avpkt->side_data) {
266 846307 ret = encode_simple_internal(avctx, avpkt);
267
2/2
✓ Branch 0 taken 431311 times.
✓ Branch 1 taken 414996 times.
846307 if (ret < 0)
268 431311 return ret;
269 }
270
271 403962 return 0;
272 }
273
274 839618 static int encode_receive_packet_internal(AVCodecContext *avctx, AVPacket *avpkt)
275 {
276 839618 AVCodecInternal *avci = avctx->internal;
277 int ret;
278
279
2/2
✓ Branch 0 taken 4345 times.
✓ Branch 1 taken 835273 times.
839618 if (avci->draining_done)
280 4345 return AVERROR_EOF;
281
282
2/4
✓ Branch 0 taken 835273 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 835273 times.
835273 av_assert0(!avpkt->data && !avpkt->side_data);
283
284
2/2
✓ Branch 0 taken 212691 times.
✓ Branch 1 taken 622582 times.
835273 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
285
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 212691 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
212691 if ((avctx->flags & AV_CODEC_FLAG_PASS1) && avctx->stats_out)
286 avctx->stats_out[0] = '\0';
287
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 212691 times.
212691 if (av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx))
288 return AVERROR(EINVAL);
289 }
290
291
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 835273 times.
835273 if (avctx->codec->receive_packet) {
292 ret = avctx->codec->receive_packet(avctx, avpkt);
293 if (ret < 0)
294 av_packet_unref(avpkt);
295 else
296 // Encoders must always return ref-counted buffers.
297 // Side-data only packets have no data and can be not ref-counted.
298 av_assert0(!avpkt->data || avpkt->buf);
299 } else
300 835273 ret = encode_simple_receive_packet(avctx, avpkt);
301
302
2/2
✓ Branch 0 taken 5935 times.
✓ Branch 1 taken 829338 times.
835273 if (ret == AVERROR_EOF)
303 5935 avci->draining_done = 1;
304
305 835273 return ret;
306 }
307
308 410074 static int encode_send_frame_internal(AVCodecContext *avctx, const AVFrame *src)
309 {
310 410074 AVCodecInternal *avci = avctx->internal;
311 410074 AVFrame *dst = avci->buffer_frame;
312 int ret;
313
314
2/2
✓ Branch 0 taken 310130 times.
✓ Branch 1 taken 99944 times.
410074 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
315 /* extract audio service type metadata */
316 310130 AVFrameSideData *sd = av_frame_get_side_data(src, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
317
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 310130 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
310130 if (sd && sd->size >= sizeof(enum AVAudioServiceType))
318 avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
319
320 /* check for valid frame size */
321
2/2
✓ Branch 0 taken 12746 times.
✓ Branch 1 taken 297384 times.
310130 if (avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) {
322
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12746 times.
12746 if (src->nb_samples > avctx->frame_size) {
323 av_log(avctx, AV_LOG_ERROR, "more samples than frame size\n");
324 return AVERROR(EINVAL);
325 }
326
2/2
✓ Branch 0 taken 52686 times.
✓ Branch 1 taken 244698 times.
297384 } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
327 /* if we already got an undersized frame, that must have been the last */
328
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52686 times.
52686 if (avctx->internal->last_audio_frame) {
329 av_log(avctx, AV_LOG_ERROR, "frame_size (%d) was not respected for a non-last frame\n", avctx->frame_size);
330 return AVERROR(EINVAL);
331 }
332
333
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 52637 times.
52686 if (src->nb_samples < avctx->frame_size) {
334 49 ret = pad_last_frame(avctx, dst, src);
335
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 if (ret < 0)
336 return ret;
337
338 49 avctx->internal->last_audio_frame = 1;
339
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52637 times.
52637 } else if (src->nb_samples > avctx->frame_size) {
340 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d)\n", src->nb_samples, avctx->frame_size);
341 return AVERROR(EINVAL);
342 }
343 }
344 }
345
346
2/2
✓ Branch 0 taken 410025 times.
✓ Branch 1 taken 49 times.
410074 if (!dst->data[0]) {
347 410025 ret = av_frame_ref(dst, src);
348
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 410025 times.
410025 if (ret < 0)
349 return ret;
350 }
351
352 410074 return 0;
353 }
354
355 416009 int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
356 {
357 416009 AVCodecInternal *avci = avctx->internal;
358 int ret;
359
360
2/4
✓ Branch 1 taken 416009 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 416009 times.
416009 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
361 return AVERROR(EINVAL);
362
363
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 416009 times.
416009 if (avci->draining)
364 return AVERROR_EOF;
365
366
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 416009 times.
416009 if (avci->buffer_frame->buf[0])
367 return AVERROR(EAGAIN);
368
369
2/2
✓ Branch 0 taken 5935 times.
✓ Branch 1 taken 410074 times.
416009 if (!frame) {
370 5935 avci->draining = 1;
371 } else {
372 410074 ret = encode_send_frame_internal(avctx, frame);
373
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 410074 times.
410074 if (ret < 0)
374 return ret;
375 }
376
377
2/4
✓ Branch 0 taken 416009 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 416009 times.
✗ Branch 3 not taken.
416009 if (!avci->buffer_pkt->data && !avci->buffer_pkt->side_data) {
378 416009 ret = encode_receive_packet_internal(avctx, avci->buffer_pkt);
379
5/6
✓ Branch 0 taken 13712 times.
✓ Branch 1 taken 402297 times.
✓ Branch 2 taken 4345 times.
✓ Branch 3 taken 9367 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 4345 times.
416009 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
380 return ret;
381 }
382
383 416009 avctx->frame_number++;
384
385 416009 return 0;
386 }
387
388 825906 int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
389 {
390 825906 AVCodecInternal *avci = avctx->internal;
391 int ret;
392
393 825906 av_packet_unref(avpkt);
394
395
2/4
✓ Branch 1 taken 825906 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 825906 times.
825906 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
396 return AVERROR(EINVAL);
397
398
4/4
✓ Branch 0 taken 423625 times.
✓ Branch 1 taken 402281 times.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 423609 times.
825906 if (avci->buffer_pkt->data || avci->buffer_pkt->side_data) {
399 402297 av_packet_move_ref(avpkt, avci->buffer_pkt);
400 } else {
401 423609 ret = encode_receive_packet_internal(avctx, avpkt);
402
2/2
✓ Branch 0 taken 421944 times.
✓ Branch 1 taken 1665 times.
423609 if (ret < 0)
403 421944 return ret;
404 }
405
406 403962 return 0;
407 }
408
409 17926 int ff_encode_preinit(AVCodecContext *avctx)
410 {
411 int i;
412
413
2/4
✓ Branch 0 taken 17926 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 17926 times.
17926 if (avctx->time_base.num <= 0 || avctx->time_base.den <= 0) {
414 av_log(avctx, AV_LOG_ERROR, "The encoder timebase is not set.\n");
415 return AVERROR(EINVAL);
416 }
417
418
2/2
✓ Branch 0 taken 1155 times.
✓ Branch 1 taken 16771 times.
17926 if (avctx->codec->sample_fmts) {
419
1/2
✓ Branch 0 taken 1167 times.
✗ Branch 1 not taken.
1167 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
420
2/2
✓ Branch 0 taken 1155 times.
✓ Branch 1 taken 12 times.
1167 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
421 1155 break;
422
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 &&
423 3 av_get_planar_sample_fmt(avctx->sample_fmt) ==
424 3 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
425 avctx->sample_fmt = avctx->codec->sample_fmts[i];
426 break;
427 }
428 }
429
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1155 times.
1155 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
430 char buf[128];
431 snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
432 av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
433 (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
434 return AVERROR(EINVAL);
435 }
436 }
437
2/2
✓ Branch 0 taken 708 times.
✓ Branch 1 taken 17218 times.
17926 if (avctx->codec->pix_fmts) {
438
1/2
✓ Branch 0 taken 2109 times.
✗ Branch 1 not taken.
2109 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
439
2/2
✓ Branch 0 taken 708 times.
✓ Branch 1 taken 1401 times.
2109 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
440 708 break;
441
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 708 times.
708 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
442 char buf[128];
443 snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
444 av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
445 (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
446 return AVERROR(EINVAL);
447 }
448
2/2
✓ Branch 0 taken 686 times.
✓ Branch 1 taken 22 times.
708 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
449
1/2
✓ Branch 0 taken 686 times.
✗ Branch 1 not taken.
686 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ411P ||
450
2/2
✓ Branch 0 taken 682 times.
✓ Branch 1 taken 4 times.
686 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
451
1/2
✓ Branch 0 taken 682 times.
✗ Branch 1 not taken.
682 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
452
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 674 times.
682 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
453 34 avctx->color_range = AVCOL_RANGE_JPEG;
454 }
455
2/2
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 17879 times.
17926 if (avctx->codec->supported_samplerates) {
456
1/2
✓ Branch 0 taken 109 times.
✗ Branch 1 not taken.
109 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
457
2/2
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 62 times.
109 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
458 47 break;
459
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
47 if (avctx->codec->supported_samplerates[i] == 0) {
460 av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
461 avctx->sample_rate);
462 return AVERROR(EINVAL);
463 }
464 }
465
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17926 times.
17926 if (avctx->sample_rate < 0) {
466 av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
467 avctx->sample_rate);
468 return AVERROR(EINVAL);
469 }
470
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 17881 times.
17926 if (avctx->codec->channel_layouts) {
471
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
45 if (!avctx->channel_layout) {
472 av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
473 } else {
474
1/2
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
61 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
475
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 16 times.
61 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
476 45 break;
477
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
45 if (avctx->codec->channel_layouts[i] == 0) {
478 char buf[512];
479 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
480 av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
481 return AVERROR(EINVAL);
482 }
483 }
484 }
485
4/4
✓ Branch 0 taken 1147 times.
✓ Branch 1 taken 16779 times.
✓ Branch 2 taken 1131 times.
✓ Branch 3 taken 16 times.
19057 if (avctx->channel_layout && avctx->channels) {
486 1131 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
487
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1131 times.
1131 if (channels != avctx->channels) {
488 char buf[512];
489 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
490 av_log(avctx, AV_LOG_ERROR,
491 "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
492 buf, channels, avctx->channels);
493 return AVERROR(EINVAL);
494 }
495
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 16779 times.
16795 } else if (avctx->channel_layout) {
496 16 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
497 }
498
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17926 times.
17926 if (avctx->channels < 0) {
499 av_log(avctx, AV_LOG_ERROR, "Specified number of channels %d is not supported\n",
500 avctx->channels);
501 return AVERROR(EINVAL);
502 }
503
2/2
✓ Branch 0 taken 16736 times.
✓ Branch 1 taken 1190 times.
17926 if(avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
504 16736 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt);
505
1/2
✓ Branch 0 taken 16736 times.
✗ Branch 1 not taken.
16736 if ( avctx->bits_per_raw_sample < 0
506
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 16735 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
16736 || (avctx->bits_per_raw_sample > 8 && pixdesc->comp[0].depth <= 8)) {
507 av_log(avctx, AV_LOG_WARNING, "Specified bit depth %d not possible with the specified pixel formats depth %d\n",
508 avctx->bits_per_raw_sample, pixdesc->comp[0].depth);
509 avctx->bits_per_raw_sample = pixdesc->comp[0].depth;
510 }
511
2/4
✓ Branch 0 taken 16736 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 16736 times.
16736 if (avctx->width <= 0 || avctx->height <= 0) {
512 av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
513 return AVERROR(EINVAL);
514 }
515 }
516
4/4
✓ Branch 0 taken 1190 times.
✓ Branch 1 taken 16736 times.
✓ Branch 2 taken 1155 times.
✓ Branch 3 taken 35 times.
17926 if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
517
3/4
✓ Branch 0 taken 17872 times.
✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 17872 times.
17891 && avctx->bit_rate>0 && avctx->bit_rate<1000) {
518 av_log(avctx, AV_LOG_WARNING, "Bitrate %"PRId64" is extremely low, maybe you mean %"PRId64"k\n", avctx->bit_rate, avctx->bit_rate);
519 }
520
521
2/2
✓ Branch 0 taken 17924 times.
✓ Branch 1 taken 2 times.
17926 if (!avctx->rc_initial_buffer_occupancy)
522 17924 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3LL / 4;
523
524
3/4
✓ Branch 0 taken 17891 times.
✓ Branch 1 taken 35 times.
✓ Branch 2 taken 17891 times.
✗ Branch 3 not taken.
17926 if (avctx->ticks_per_frame && avctx->time_base.num &&
525
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17891 times.
17891 avctx->ticks_per_frame > INT_MAX / avctx->time_base.num) {
526 av_log(avctx, AV_LOG_ERROR,
527 "ticks_per_frame %d too large for the timebase %d/%d.",
528 avctx->ticks_per_frame,
529 avctx->time_base.num,
530 avctx->time_base.den);
531 return AVERROR(EINVAL);
532 }
533
534
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17926 times.
17926 if (avctx->hw_frames_ctx) {
535 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
536 if (frames_ctx->format != avctx->pix_fmt) {
537 av_log(avctx, AV_LOG_ERROR,
538 "Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format\n");
539 return AVERROR(EINVAL);
540 }
541 if (avctx->sw_pix_fmt != AV_PIX_FMT_NONE &&
542 avctx->sw_pix_fmt != frames_ctx->sw_format) {
543 av_log(avctx, AV_LOG_ERROR,
544 "Mismatching AVCodecContext.sw_pix_fmt (%s) "
545 "and AVHWFramesContext.sw_format (%s)\n",
546 av_get_pix_fmt_name(avctx->sw_pix_fmt),
547 av_get_pix_fmt_name(frames_ctx->sw_format));
548 return AVERROR(EINVAL);
549 }
550 avctx->sw_pix_fmt = frames_ctx->sw_format;
551 }
552
2/2
✓ Branch 0 taken 17613 times.
✓ Branch 1 taken 313 times.
17926 if (avctx->codec_descriptor->props & AV_CODEC_PROP_INTRA_ONLY)
553 17613 avctx->internal->intra_only_flag = AV_PKT_FLAG_KEY;
554
555 17926 return 0;
556 }
557