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