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