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 | 908455 | static EncodeContext *encode_ctx(AVCodecInternal *avci) | |
58 | { | ||
59 | 908455 | return (EncodeContext*)avci; | |
60 | } | ||
61 | |||
62 | 28151 | int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size) | |
63 | { | ||
64 |
2/4✓ Branch 0 taken 28151 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 28151 times.
|
28151 | 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 28151 times.
|
28151 | av_assert0(!avpkt->data); |
71 | |||
72 | 28151 | av_fast_padded_malloc(&avctx->internal->byte_buffer, | |
73 | 28151 | &avctx->internal->byte_buffer_size, size); | |
74 | 28151 | avpkt->data = avctx->internal->byte_buffer; | |
75 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28151 times.
|
28151 | 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 | 28151 | avpkt->size = size; | |
80 | |||
81 | 28151 | return 0; | |
82 | } | ||
83 | |||
84 | 439884 | int avcodec_default_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int flags) | |
85 | { | ||
86 | int ret; | ||
87 | |||
88 |
2/4✓ Branch 0 taken 439884 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 439884 times.
|
439884 | if (avpkt->size < 0 || avpkt->size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) |
89 | ✗ | return AVERROR(EINVAL); | |
90 | |||
91 |
2/4✓ Branch 0 taken 439884 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 439884 times.
|
439884 | 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 | 439884 | ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE); | |
97 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 439884 times.
|
439884 | 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 | 439884 | avpkt->data = avpkt->buf->data; | |
102 | |||
103 | 439884 | return 0; | |
104 | } | ||
105 | |||
106 | 439884 | 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 439884 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 439884 times.
|
439884 | if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) |
111 | ✗ | return AVERROR(EINVAL); | |
112 | |||
113 |
2/4✓ Branch 0 taken 439884 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 439884 times.
|
439884 | av_assert0(!avpkt->data && !avpkt->buf); |
114 | |||
115 | 439884 | avpkt->size = size; | |
116 | 439884 | ret = avctx->get_encode_buffer(avctx, avpkt, flags); | |
117 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 439884 times.
|
439884 | if (ret < 0) |
118 | ✗ | goto fail; | |
119 | |||
120 |
2/4✓ Branch 0 taken 439884 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 439884 times.
|
439884 | 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 | 439884 | memset(avpkt->data + avpkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE); | |
126 | |||
127 | 439884 | ret = 0; | |
128 | 439884 | fail: | |
129 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 439884 times.
|
439884 | if (ret < 0) { |
130 | ✗ | av_log(avctx, AV_LOG_ERROR, "get_encode_buffer() failed\n"); | |
131 | ✗ | av_packet_unref(avpkt); | |
132 | } | ||
133 | |||
134 | 439884 | return ret; | |
135 | } | ||
136 | |||
137 | 440458 | static int encode_make_refcounted(AVCodecContext *avctx, AVPacket *avpkt) | |
138 | { | ||
139 | 440458 | uint8_t *data = avpkt->data; | |
140 | int ret; | ||
141 | |||
142 |
2/2✓ Branch 0 taken 412307 times.
✓ Branch 1 taken 28151 times.
|
440458 | if (avpkt->buf) |
143 | 412307 | return 0; | |
144 | |||
145 | 28151 | avpkt->data = NULL; | |
146 | 28151 | ret = ff_get_encode_buffer(avctx, avpkt, avpkt->size, 0); | |
147 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28151 times.
|
28151 | if (ret < 0) |
148 | ✗ | return ret; | |
149 | 28151 | memcpy(avpkt->data, data, avpkt->size); | |
150 | |||
151 | 28151 | return 0; | |
152 | } | ||
153 | |||
154 | /** | ||
155 | * Pad last frame with silence. | ||
156 | */ | ||
157 | 54 | static int pad_last_frame(AVCodecContext *s, AVFrame *frame, const AVFrame *src, int out_samples) | |
158 | { | ||
159 | int ret; | ||
160 | |||
161 | 54 | frame->format = src->format; | |
162 | 54 | frame->nb_samples = out_samples; | |
163 | 54 | ret = av_channel_layout_copy(&frame->ch_layout, &s->ch_layout); | |
164 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
|
54 | if (ret < 0) |
165 | ✗ | goto fail; | |
166 | 54 | ret = av_frame_get_buffer(frame, 0); | |
167 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
|
54 | if (ret < 0) |
168 | ✗ | goto fail; | |
169 | |||
170 | 54 | ret = av_frame_copy_props(frame, src); | |
171 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
|
54 | if (ret < 0) |
172 | ✗ | goto fail; | |
173 | |||
174 |
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, |
175 | 54 | 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 54 times.
|
54 | if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples, |
179 | 54 | frame->nb_samples - src->nb_samples, | |
180 | s->ch_layout.nb_channels, s->sample_fmt)) < 0) | ||
181 | ✗ | goto fail; | |
182 | |||
183 | 54 | 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 | 903547 | int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame) | |
206 | { | ||
207 | 903547 | AVCodecInternal *avci = avctx->internal; | |
208 | |||
209 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 903547 times.
|
903547 | if (avci->draining) |
210 | ✗ | return AVERROR_EOF; | |
211 | |||
212 |
2/2✓ Branch 0 taken 456959 times.
✓ Branch 1 taken 446588 times.
|
903547 | if (!avci->buffer_frame->buf[0]) |
213 | 456959 | return AVERROR(EAGAIN); | |
214 | |||
215 | 446588 | av_frame_move_ref(frame, avci->buffer_frame); | |
216 | |||
217 | 446588 | return 0; | |
218 | } | ||
219 | |||
220 | 435802 | int ff_encode_reordered_opaque(AVCodecContext *avctx, | |
221 | AVPacket *pkt, const AVFrame *frame) | ||
222 | { | ||
223 |
2/2✓ Branch 0 taken 432261 times.
✓ Branch 1 taken 3541 times.
|
435802 | if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) { |
224 | 432261 | int ret = av_buffer_replace(&pkt->opaque_ref, frame->opaque_ref); | |
225 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 432261 times.
|
432261 | if (ret < 0) |
226 | ✗ | return ret; | |
227 | 432261 | pkt->opaque = frame->opaque; | |
228 | } | ||
229 | |||
230 | 435802 | return 0; | |
231 | } | ||
232 | |||
233 | 447062 | int ff_encode_encode_cb(AVCodecContext *avctx, AVPacket *avpkt, | |
234 | AVFrame *frame, int *got_packet) | ||
235 | { | ||
236 | 447062 | const FFCodec *const codec = ffcodec(avctx->codec); | |
237 | int ret; | ||
238 | |||
239 | 447062 | ret = codec->cb.encode(avctx, avpkt, frame, got_packet); | |
240 | 447062 | emms_c(); | |
241 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 447062 times.
|
447062 | av_assert0(ret <= 0); |
242 | |||
243 |
3/4✓ Branch 0 taken 447062 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6534 times.
✓ Branch 3 taken 440528 times.
|
447062 | if (!ret && *got_packet) { |
244 |
2/2✓ Branch 0 taken 440458 times.
✓ Branch 1 taken 70 times.
|
440528 | if (avpkt->data) { |
245 | 440458 | ret = encode_make_refcounted(avctx, avpkt); | |
246 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 440458 times.
|
440458 | if (ret < 0) |
247 | ✗ | goto unref; | |
248 | // Date returned by encoders must always be ref-counted | ||
249 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 440458 times.
|
440458 | av_assert0(avpkt->buf); |
250 | } | ||
251 | |||
252 | // set the timestamps for the simple no-delay case | ||
253 | // encoders with delay have to set the timestamps themselves | ||
254 |
4/4✓ Branch 0 taken 26631 times.
✓ Branch 1 taken 413897 times.
✓ Branch 2 taken 26413 times.
✓ Branch 3 taken 218 times.
|
440528 | if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || |
255 |
2/2✓ Branch 0 taken 15763 times.
✓ Branch 1 taken 10650 times.
|
26413 | (frame && (codec->caps_internal & FF_CODEC_CAP_EOF_FLUSH))) { |
256 |
2/2✓ Branch 0 taken 414335 times.
✓ Branch 1 taken 15325 times.
|
429660 | if (avpkt->pts == AV_NOPTS_VALUE) |
257 | 414335 | avpkt->pts = frame->pts; | |
258 | |||
259 |
2/2✓ Branch 0 taken 424661 times.
✓ Branch 1 taken 4999 times.
|
429660 | if (!avpkt->duration) { |
260 |
2/2✓ Branch 0 taken 404021 times.
✓ Branch 1 taken 20640 times.
|
424661 | if (frame->duration) |
261 | 404021 | avpkt->duration = frame->duration; | |
262 |
2/2✓ Branch 0 taken 3200 times.
✓ Branch 1 taken 17440 times.
|
20640 | else if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) { |
263 | 3200 | avpkt->duration = ff_samples_to_time_base(avctx, | |
264 | 3200 | frame->nb_samples); | |
265 | } | ||
266 | } | ||
267 | |||
268 | 429660 | ret = ff_encode_reordered_opaque(avctx, avpkt, frame); | |
269 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 429660 times.
|
429660 | if (ret < 0) |
270 | ✗ | goto unref; | |
271 | } | ||
272 | |||
273 | // dts equals pts unless there is reordering | ||
274 | // there can be no reordering if there is no encoder delay | ||
275 |
2/2✓ Branch 0 taken 6881 times.
✓ Branch 1 taken 433647 times.
|
440528 | if (!(avctx->codec_descriptor->props & AV_CODEC_PROP_REORDER) || |
276 |
2/2✓ Branch 0 taken 6116 times.
✓ Branch 1 taken 765 times.
|
6881 | !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || |
277 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6116 times.
|
6116 | (codec->caps_internal & FF_CODEC_CAP_EOF_FLUSH)) |
278 | 434412 | avpkt->dts = avpkt->pts; | |
279 | } else { | ||
280 | 6534 | unref: | |
281 | 6534 | av_packet_unref(avpkt); | |
282 | } | ||
283 | |||
284 |
2/2✓ Branch 0 taken 446588 times.
✓ Branch 1 taken 474 times.
|
447062 | if (frame) |
285 | 446588 | av_frame_unref(frame); | |
286 | |||
287 | 447062 | return ret; | |
288 | } | ||
289 | |||
290 | 917935 | static int encode_simple_internal(AVCodecContext *avctx, AVPacket *avpkt) | |
291 | { | ||
292 | 917935 | AVCodecInternal *avci = avctx->internal; | |
293 | 917935 | AVFrame *frame = avci->in_frame; | |
294 | 917935 | const FFCodec *const codec = ffcodec(avctx->codec); | |
295 | int got_packet; | ||
296 | int ret; | ||
297 | |||
298 |
2/2✓ Branch 0 taken 1904 times.
✓ Branch 1 taken 916031 times.
|
917935 | if (avci->draining_done) |
299 | 1904 | return AVERROR_EOF; | |
300 | |||
301 |
3/4✓ Branch 0 taken 916031 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 903547 times.
✓ Branch 3 taken 12484 times.
|
916031 | if (!frame->buf[0] && !avci->draining) { |
302 | 903547 | av_frame_unref(frame); | |
303 | 903547 | ret = ff_encode_get_frame(avctx, frame); | |
304 |
3/4✓ Branch 0 taken 456959 times.
✓ Branch 1 taken 446588 times.
✓ Branch 2 taken 456959 times.
✗ Branch 3 not taken.
|
903547 | if (ret < 0 && ret != AVERROR_EOF) |
305 | 456959 | return ret; | |
306 | } | ||
307 | |||
308 |
2/2✓ Branch 0 taken 12484 times.
✓ Branch 1 taken 446588 times.
|
459072 | if (!frame->buf[0]) { |
309 |
2/2✓ Branch 0 taken 12010 times.
✓ Branch 1 taken 474 times.
|
12484 | if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY || |
310 |
2/2✓ Branch 0 taken 6207 times.
✓ Branch 1 taken 5803 times.
|
12010 | avci->frame_thread_encoder)) |
311 | 6207 | return AVERROR_EOF; | |
312 | |||
313 | // Flushing is signaled with a NULL frame | ||
314 | 6277 | frame = NULL; | |
315 | } | ||
316 | |||
317 | 452865 | got_packet = 0; | |
318 | |||
319 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 452865 times.
|
452865 | av_assert0(codec->cb_type == FF_CODEC_CB_TYPE_ENCODE); |
320 | |||
321 |
2/2✓ Branch 0 taken 63600 times.
✓ Branch 1 taken 389265 times.
|
452865 | if (CONFIG_FRAME_THREAD_ENCODER && avci->frame_thread_encoder) |
322 | /* This will unref frame. */ | ||
323 | 63600 | ret = ff_thread_video_encode_frame(avctx, avpkt, frame, &got_packet); | |
324 | else { | ||
325 | 389265 | ret = ff_encode_encode_cb(avctx, avpkt, frame, &got_packet); | |
326 | } | ||
327 | |||
328 |
4/4✓ Branch 0 taken 6277 times.
✓ Branch 1 taken 446588 times.
✓ Branch 2 taken 1904 times.
✓ Branch 3 taken 4373 times.
|
452865 | if (avci->draining && !got_packet) |
329 | 1904 | avci->draining_done = 1; | |
330 | |||
331 | 452865 | return ret; | |
332 | } | ||
333 | |||
334 | 905598 | static int encode_simple_receive_packet(AVCodecContext *avctx, AVPacket *avpkt) | |
335 | { | ||
336 | int ret; | ||
337 | |||
338 |
4/4✓ Branch 0 taken 918005 times.
✓ Branch 1 taken 440458 times.
✓ Branch 2 taken 917935 times.
✓ Branch 3 taken 70 times.
|
1358463 | while (!avpkt->data && !avpkt->side_data) { |
339 | 917935 | ret = encode_simple_internal(avctx, avpkt); | |
340 |
2/2✓ Branch 0 taken 465070 times.
✓ Branch 1 taken 452865 times.
|
917935 | if (ret < 0) |
341 | 465070 | return ret; | |
342 | } | ||
343 | |||
344 | 440528 | return 0; | |
345 | } | ||
346 | |||
347 | 911921 | static int encode_receive_packet_internal(AVCodecContext *avctx, AVPacket *avpkt) | |
348 | { | ||
349 | 911921 | AVCodecInternal *avci = avctx->internal; | |
350 | int ret; | ||
351 | |||
352 |
2/2✓ Branch 0 taken 6323 times.
✓ Branch 1 taken 905598 times.
|
911921 | if (avci->draining_done) |
353 | 6323 | return AVERROR_EOF; | |
354 | |||
355 |
2/4✓ Branch 0 taken 905598 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 905598 times.
|
905598 | av_assert0(!avpkt->data && !avpkt->side_data); |
356 | |||
357 |
2/2✓ Branch 0 taken 291222 times.
✓ Branch 1 taken 614376 times.
|
905598 | if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) { |
358 |
3/4✓ Branch 0 taken 808 times.
✓ Branch 1 taken 290414 times.
✓ Branch 2 taken 808 times.
✗ Branch 3 not taken.
|
291222 | if ((avctx->flags & AV_CODEC_FLAG_PASS1) && avctx->stats_out) |
359 | 808 | avctx->stats_out[0] = '\0'; | |
360 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 291222 times.
|
291222 | if (av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx)) |
361 | ✗ | return AVERROR(EINVAL); | |
362 | } | ||
363 | |||
364 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 905598 times.
|
905598 | if (ffcodec(avctx->codec)->cb_type == FF_CODEC_CB_TYPE_RECEIVE_PACKET) { |
365 | ✗ | ret = ffcodec(avctx->codec)->cb.receive_packet(avctx, avpkt); | |
366 | ✗ | if (ret < 0) | |
367 | ✗ | av_packet_unref(avpkt); | |
368 | else | ||
369 | // Encoders must always return ref-counted buffers. | ||
370 | // Side-data only packets have no data and can be not ref-counted. | ||
371 | ✗ | av_assert0(!avpkt->data || avpkt->buf); | |
372 | } else | ||
373 | 905598 | ret = encode_simple_receive_packet(avctx, avpkt); | |
374 |
2/2✓ Branch 0 taken 440528 times.
✓ Branch 1 taken 465070 times.
|
905598 | if (ret >= 0) |
375 | 440528 | avpkt->flags |= encode_ctx(avci)->intra_only_flag; | |
376 | |||
377 |
2/2✓ Branch 0 taken 8111 times.
✓ Branch 1 taken 897487 times.
|
905598 | if (ret == AVERROR_EOF) |
378 | 8111 | avci->draining_done = 1; | |
379 | |||
380 | 905598 | return ret; | |
381 | } | ||
382 | |||
383 | #if CONFIG_LCMS2 | ||
384 | static int encode_generate_icc_profile(AVCodecContext *avctx, AVFrame *frame) | ||
385 | { | ||
386 | enum AVColorTransferCharacteristic trc = frame->color_trc; | ||
387 | enum AVColorPrimaries prim = frame->color_primaries; | ||
388 | const FFCodec *const codec = ffcodec(avctx->codec); | ||
389 | AVCodecInternal *avci = avctx->internal; | ||
390 | cmsHPROFILE profile; | ||
391 | int ret; | ||
392 | |||
393 | /* don't generate ICC profiles if disabled or unsupported */ | ||
394 | if (!(avctx->flags2 & AV_CODEC_FLAG2_ICC_PROFILES)) | ||
395 | return 0; | ||
396 | if (!(codec->caps_internal & FF_CODEC_CAP_ICC_PROFILES)) | ||
397 | return 0; | ||
398 | |||
399 | if (trc == AVCOL_TRC_UNSPECIFIED) | ||
400 | trc = avctx->color_trc; | ||
401 | if (prim == AVCOL_PRI_UNSPECIFIED) | ||
402 | prim = avctx->color_primaries; | ||
403 | if (trc == AVCOL_TRC_UNSPECIFIED || prim == AVCOL_PRI_UNSPECIFIED) | ||
404 | return 0; /* can't generate ICC profile with missing csp tags */ | ||
405 | |||
406 | if (av_frame_get_side_data(frame, AV_FRAME_DATA_ICC_PROFILE)) | ||
407 | return 0; /* don't overwrite existing ICC profile */ | ||
408 | |||
409 | if (!avci->icc.avctx) { | ||
410 | ret = ff_icc_context_init(&avci->icc, avctx); | ||
411 | if (ret < 0) | ||
412 | return ret; | ||
413 | } | ||
414 | |||
415 | ret = ff_icc_profile_generate(&avci->icc, prim, trc, &profile); | ||
416 | if (ret < 0) | ||
417 | return ret; | ||
418 | |||
419 | ret = ff_icc_profile_attach(&avci->icc, profile, frame); | ||
420 | cmsCloseProfile(profile); | ||
421 | return ret; | ||
422 | } | ||
423 | #else /* !CONFIG_LCMS2 */ | ||
424 | 140114 | static int encode_generate_icc_profile(av_unused AVCodecContext *c, av_unused AVFrame *f) | |
425 | { | ||
426 | 140114 | return 0; | |
427 | } | ||
428 | #endif | ||
429 | |||
430 | 446588 | static int encode_send_frame_internal(AVCodecContext *avctx, const AVFrame *src) | |
431 | { | ||
432 | 446588 | AVCodecInternal *avci = avctx->internal; | |
433 | 446588 | EncodeContext *ec = encode_ctx(avci); | |
434 | 446588 | AVFrame *dst = avci->buffer_frame; | |
435 | int ret; | ||
436 | |||
437 |
2/2✓ Branch 0 taken 306474 times.
✓ Branch 1 taken 140114 times.
|
446588 | if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) { |
438 | /* extract audio service type metadata */ | ||
439 | 306474 | AVFrameSideData *sd = av_frame_get_side_data(src, AV_FRAME_DATA_AUDIO_SERVICE_TYPE); | |
440 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 306474 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
306474 | if (sd && sd->size >= sizeof(enum AVAudioServiceType)) |
441 | ✗ | avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data; | |
442 | |||
443 | /* check for valid frame size */ | ||
444 |
2/2✓ Branch 0 taken 73612 times.
✓ Branch 1 taken 232862 times.
|
306474 | if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) { |
445 | /* if we already got an undersized frame, that must have been the last */ | ||
446 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 73612 times.
|
73612 | if (ec->last_audio_frame) { |
447 | ✗ | av_log(avctx, AV_LOG_ERROR, "frame_size (%d) was not respected for a non-last frame\n", avctx->frame_size); | |
448 | ✗ | return AVERROR(EINVAL); | |
449 | } | ||
450 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 73612 times.
|
73612 | if (src->nb_samples > avctx->frame_size) { |
451 | ✗ | av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) > frame_size (%d)\n", src->nb_samples, avctx->frame_size); | |
452 | ✗ | return AVERROR(EINVAL); | |
453 | } | ||
454 |
2/2✓ Branch 0 taken 161 times.
✓ Branch 1 taken 73451 times.
|
73612 | if (src->nb_samples < avctx->frame_size) { |
455 | 161 | ec->last_audio_frame = 1; | |
456 |
2/2✓ Branch 0 taken 54 times.
✓ Branch 1 taken 107 times.
|
161 | if (!(avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME)) { |
457 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 51 times.
|
54 | int pad_samples = avci->pad_samples ? avci->pad_samples : avctx->frame_size; |
458 | 54 | int out_samples = (src->nb_samples + pad_samples - 1) / pad_samples * pad_samples; | |
459 | |||
460 |
1/2✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
|
54 | if (out_samples != src->nb_samples) { |
461 | 54 | ret = pad_last_frame(avctx, dst, src, out_samples); | |
462 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
|
54 | if (ret < 0) |
463 | ✗ | return ret; | |
464 | 54 | goto finish; | |
465 | } | ||
466 | } | ||
467 | } | ||
468 | } | ||
469 | } | ||
470 | |||
471 | 446534 | ret = av_frame_ref(dst, src); | |
472 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 446534 times.
|
446534 | if (ret < 0) |
473 | ✗ | return ret; | |
474 | |||
475 | 446534 | finish: | |
476 | |||
477 |
2/2✓ Branch 0 taken 140114 times.
✓ Branch 1 taken 306474 times.
|
446588 | if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) { |
478 | 140114 | ret = encode_generate_icc_profile(avctx, dst); | |
479 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 140114 times.
|
140114 | if (ret < 0) |
480 | ✗ | return ret; | |
481 | } | ||
482 | |||
483 | // unset frame duration unless AV_CODEC_FLAG_FRAME_DURATION is set, | ||
484 | // since otherwise we cannot be sure that whatever value it has is in the | ||
485 | // right timebase, so we would produce an incorrect value, which is worse | ||
486 | // than none at all | ||
487 |
2/2✓ Branch 0 taken 3262 times.
✓ Branch 1 taken 443326 times.
|
446588 | if (!(avctx->flags & AV_CODEC_FLAG_FRAME_DURATION)) |
488 | 3262 | dst->duration = 0; | |
489 | |||
490 | 446588 | return 0; | |
491 | } | ||
492 | |||
493 | 454699 | int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame) | |
494 | { | ||
495 | 454699 | AVCodecInternal *avci = avctx->internal; | |
496 | int ret; | ||
497 | |||
498 |
2/4✓ Branch 1 taken 454699 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 454699 times.
|
454699 | if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec)) |
499 | ✗ | return AVERROR(EINVAL); | |
500 | |||
501 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 454699 times.
|
454699 | if (avci->draining) |
502 | ✗ | return AVERROR_EOF; | |
503 | |||
504 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 454699 times.
|
454699 | if (avci->buffer_frame->buf[0]) |
505 | ✗ | return AVERROR(EAGAIN); | |
506 | |||
507 |
2/2✓ Branch 0 taken 8111 times.
✓ Branch 1 taken 446588 times.
|
454699 | if (!frame) { |
508 | 8111 | avci->draining = 1; | |
509 | } else { | ||
510 | 446588 | ret = encode_send_frame_internal(avctx, frame); | |
511 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 446588 times.
|
446588 | if (ret < 0) |
512 | ✗ | return ret; | |
513 | } | ||
514 | |||
515 |
2/4✓ Branch 0 taken 454699 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 454699 times.
✗ Branch 3 not taken.
|
454699 | if (!avci->buffer_pkt->data && !avci->buffer_pkt->side_data) { |
516 | 454699 | ret = encode_receive_packet_internal(avctx, avci->buffer_pkt); | |
517 |
5/6✓ Branch 0 taken 16756 times.
✓ Branch 1 taken 437943 times.
✓ Branch 2 taken 6323 times.
✓ Branch 3 taken 10433 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 6323 times.
|
454699 | if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) |
518 | ✗ | return ret; | |
519 | } | ||
520 | |||
521 | 454699 | avctx->frame_num++; | |
522 | |||
523 | 454699 | return 0; | |
524 | } | ||
525 | |||
526 | 895165 | int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt) | |
527 | { | ||
528 | 895165 | AVCodecInternal *avci = avctx->internal; | |
529 | int ret; | ||
530 | |||
531 | 895165 | av_packet_unref(avpkt); | |
532 | |||
533 |
2/4✓ Branch 1 taken 895165 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 895165 times.
|
895165 | if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec)) |
534 | ✗ | return AVERROR(EINVAL); | |
535 | |||
536 |
4/4✓ Branch 0 taken 457292 times.
✓ Branch 1 taken 437873 times.
✓ Branch 2 taken 70 times.
✓ Branch 3 taken 457222 times.
|
895165 | if (avci->buffer_pkt->data || avci->buffer_pkt->side_data) { |
537 | 437943 | av_packet_move_ref(avpkt, avci->buffer_pkt); | |
538 | } else { | ||
539 | 457222 | ret = encode_receive_packet_internal(avctx, avpkt); | |
540 |
2/2✓ Branch 0 taken 454637 times.
✓ Branch 1 taken 2585 times.
|
457222 | if (ret < 0) |
541 | 454637 | return ret; | |
542 | } | ||
543 | |||
544 | 440528 | return 0; | |
545 | } | ||
546 | |||
547 | 19952 | static int encode_preinit_video(AVCodecContext *avctx) | |
548 | { | ||
549 | 19952 | const AVCodec *c = avctx->codec; | |
550 | 19952 | const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt); | |
551 | const enum AVPixelFormat *pix_fmts; | ||
552 | int ret, i, num_pix_fmts; | ||
553 | |||
554 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 19952 times.
|
19952 | if (!av_get_pix_fmt_name(avctx->pix_fmt)) { |
555 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid video pixel format: %d\n", | |
556 | ✗ | avctx->pix_fmt); | |
557 | ✗ | return AVERROR(EINVAL); | |
558 | } | ||
559 | |||
560 | 19952 | ret = avcodec_get_supported_config(avctx, NULL, AV_CODEC_CONFIG_PIX_FORMAT, | |
561 | 0, (const void **) &pix_fmts, &num_pix_fmts); | ||
562 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 19952 times.
|
19952 | if (ret < 0) |
563 | ✗ | return ret; | |
564 | |||
565 |
2/2✓ Branch 0 taken 814 times.
✓ Branch 1 taken 19138 times.
|
19952 | if (pix_fmts) { |
566 |
1/2✓ Branch 0 taken 2678 times.
✗ Branch 1 not taken.
|
2678 | for (i = 0; i < num_pix_fmts; i++) |
567 |
2/2✓ Branch 0 taken 814 times.
✓ Branch 1 taken 1864 times.
|
2678 | if (avctx->pix_fmt == pix_fmts[i]) |
568 | 814 | break; | |
569 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 814 times.
|
814 | if (i == num_pix_fmts) { |
570 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
571 | "Specified pixel format %s is not supported by the %s encoder.\n", | ||
572 | ✗ | av_get_pix_fmt_name(avctx->pix_fmt), c->name); | |
573 | |||
574 | ✗ | av_log(avctx, AV_LOG_ERROR, "Supported pixel formats:\n"); | |
575 | ✗ | for (int p = 0; pix_fmts[p] != AV_PIX_FMT_NONE; p++) { | |
576 | ✗ | av_log(avctx, AV_LOG_ERROR, " %s\n", | |
577 | ✗ | av_get_pix_fmt_name(pix_fmts[p])); | |
578 | } | ||
579 | |||
580 | ✗ | return AVERROR(EINVAL); | |
581 | } | ||
582 |
2/2✓ Branch 0 taken 793 times.
✓ Branch 1 taken 21 times.
|
814 | if (pix_fmts[i] == AV_PIX_FMT_YUVJ420P || |
583 |
1/2✓ Branch 0 taken 793 times.
✗ Branch 1 not taken.
|
793 | pix_fmts[i] == AV_PIX_FMT_YUVJ411P || |
584 |
2/2✓ Branch 0 taken 789 times.
✓ Branch 1 taken 4 times.
|
793 | pix_fmts[i] == AV_PIX_FMT_YUVJ422P || |
585 |
1/2✓ Branch 0 taken 789 times.
✗ Branch 1 not taken.
|
789 | pix_fmts[i] == AV_PIX_FMT_YUVJ440P || |
586 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 781 times.
|
789 | pix_fmts[i] == AV_PIX_FMT_YUVJ444P) |
587 | 33 | avctx->color_range = AVCOL_RANGE_JPEG; | |
588 | } | ||
589 | |||
590 |
1/2✓ Branch 0 taken 19952 times.
✗ Branch 1 not taken.
|
19952 | if ( avctx->bits_per_raw_sample < 0 |
591 |
3/4✓ Branch 0 taken 83 times.
✓ Branch 1 taken 19869 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 83 times.
|
19952 | || (avctx->bits_per_raw_sample > 8 && pixdesc->comp[0].depth <= 8)) { |
592 | ✗ | av_log(avctx, AV_LOG_WARNING, "Specified bit depth %d not possible with the specified pixel formats depth %d\n", | |
593 | ✗ | avctx->bits_per_raw_sample, pixdesc->comp[0].depth); | |
594 | ✗ | avctx->bits_per_raw_sample = pixdesc->comp[0].depth; | |
595 | } | ||
596 |
2/4✓ Branch 0 taken 19952 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 19952 times.
|
19952 | if (avctx->width <= 0 || avctx->height <= 0) { |
597 | ✗ | av_log(avctx, AV_LOG_ERROR, "dimensions not set\n"); | |
598 | ✗ | return AVERROR(EINVAL); | |
599 | } | ||
600 | |||
601 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 19952 times.
|
19952 | if (avctx->hw_frames_ctx) { |
602 | ✗ | AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data; | |
603 | ✗ | if (frames_ctx->format != avctx->pix_fmt) { | |
604 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
605 | "Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format\n"); | ||
606 | ✗ | return AVERROR(EINVAL); | |
607 | } | ||
608 | ✗ | if (avctx->sw_pix_fmt != AV_PIX_FMT_NONE && | |
609 | ✗ | avctx->sw_pix_fmt != frames_ctx->sw_format) { | |
610 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
611 | "Mismatching AVCodecContext.sw_pix_fmt (%s) " | ||
612 | "and AVHWFramesContext.sw_format (%s)\n", | ||
613 | av_get_pix_fmt_name(avctx->sw_pix_fmt), | ||
614 | av_get_pix_fmt_name(frames_ctx->sw_format)); | ||
615 | ✗ | return AVERROR(EINVAL); | |
616 | } | ||
617 | ✗ | avctx->sw_pix_fmt = frames_ctx->sw_format; | |
618 | } | ||
619 | |||
620 | 19952 | return 0; | |
621 | } | ||
622 | |||
623 | 1349 | static int encode_preinit_audio(AVCodecContext *avctx) | |
624 | { | ||
625 | 1349 | const AVCodec *c = avctx->codec; | |
626 | const enum AVSampleFormat *sample_fmts; | ||
627 | const int *supported_samplerates; | ||
628 | const AVChannelLayout *ch_layouts; | ||
629 | int ret, i, num_sample_fmts, num_samplerates, num_ch_layouts; | ||
630 | |||
631 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1349 times.
|
1349 | if (!av_get_sample_fmt_name(avctx->sample_fmt)) { |
632 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid audio sample format: %d\n", | |
633 | ✗ | avctx->sample_fmt); | |
634 | ✗ | return AVERROR(EINVAL); | |
635 | } | ||
636 | |||
637 | 1349 | ret = avcodec_get_supported_config(avctx, NULL, AV_CODEC_CONFIG_SAMPLE_FORMAT, | |
638 | 0, (const void **) &sample_fmts, | ||
639 | &num_sample_fmts); | ||
640 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1349 times.
|
1349 | if (ret < 0) |
641 | ✗ | return ret; | |
642 |
1/2✓ Branch 0 taken 1349 times.
✗ Branch 1 not taken.
|
1349 | if (sample_fmts) { |
643 |
1/2✓ Branch 0 taken 1366 times.
✗ Branch 1 not taken.
|
1366 | for (i = 0; i < num_sample_fmts; i++) { |
644 |
2/2✓ Branch 0 taken 1349 times.
✓ Branch 1 taken 17 times.
|
1366 | if (avctx->sample_fmt == sample_fmts[i]) |
645 | 1349 | break; | |
646 |
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 && |
647 | 4 | av_get_planar_sample_fmt(avctx->sample_fmt) == | |
648 | 4 | av_get_planar_sample_fmt(sample_fmts[i])) { | |
649 | ✗ | avctx->sample_fmt = sample_fmts[i]; | |
650 | ✗ | break; | |
651 | } | ||
652 | } | ||
653 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1349 times.
|
1349 | if (i == num_sample_fmts) { |
654 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
655 | "Specified sample format %s is not supported by the %s encoder\n", | ||
656 | ✗ | av_get_sample_fmt_name(avctx->sample_fmt), c->name); | |
657 | |||
658 | ✗ | av_log(avctx, AV_LOG_ERROR, "Supported sample formats:\n"); | |
659 | ✗ | for (int p = 0; sample_fmts[p] != AV_SAMPLE_FMT_NONE; p++) { | |
660 | ✗ | av_log(avctx, AV_LOG_ERROR, " %s\n", | |
661 | ✗ | av_get_sample_fmt_name(sample_fmts[p])); | |
662 | } | ||
663 | |||
664 | ✗ | return AVERROR(EINVAL); | |
665 | } | ||
666 | } | ||
667 | |||
668 | 1349 | ret = avcodec_get_supported_config(avctx, NULL, AV_CODEC_CONFIG_SAMPLE_RATE, | |
669 | 0, (const void **) &supported_samplerates, | ||
670 | &num_samplerates); | ||
671 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1349 times.
|
1349 | if (ret < 0) |
672 | ✗ | return ret; | |
673 |
2/2✓ Branch 0 taken 61 times.
✓ Branch 1 taken 1288 times.
|
1349 | if (supported_samplerates) { |
674 |
1/2✓ Branch 0 taken 132 times.
✗ Branch 1 not taken.
|
132 | for (i = 0; i < num_samplerates; i++) |
675 |
2/2✓ Branch 0 taken 61 times.
✓ Branch 1 taken 71 times.
|
132 | if (avctx->sample_rate == supported_samplerates[i]) |
676 | 61 | break; | |
677 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
|
61 | if (i == num_samplerates) { |
678 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
679 | "Specified sample rate %d is not supported by the %s encoder\n", | ||
680 | ✗ | avctx->sample_rate, c->name); | |
681 | |||
682 | ✗ | av_log(avctx, AV_LOG_ERROR, "Supported sample rates:\n"); | |
683 | ✗ | for (int p = 0; supported_samplerates[p]; p++) | |
684 | ✗ | av_log(avctx, AV_LOG_ERROR, " %d\n", supported_samplerates[p]); | |
685 | |||
686 | ✗ | return AVERROR(EINVAL); | |
687 | } | ||
688 | } | ||
689 | 1349 | ret = avcodec_get_supported_config(avctx, NULL, AV_CODEC_CONFIG_CHANNEL_LAYOUT, | |
690 | 0, (const void **) &ch_layouts, &num_ch_layouts); | ||
691 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1349 times.
|
1349 | if (ret < 0) |
692 | ✗ | return ret; | |
693 |
2/2✓ Branch 0 taken 79 times.
✓ Branch 1 taken 1270 times.
|
1349 | if (ch_layouts) { |
694 |
1/2✓ Branch 0 taken 140 times.
✗ Branch 1 not taken.
|
140 | for (i = 0; i < num_ch_layouts; i++) { |
695 |
2/2✓ Branch 1 taken 79 times.
✓ Branch 2 taken 61 times.
|
140 | if (!av_channel_layout_compare(&avctx->ch_layout, &ch_layouts[i])) |
696 | 79 | break; | |
697 | } | ||
698 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 79 times.
|
79 | if (i == num_ch_layouts) { |
699 | char buf[512]; | ||
700 | ✗ | int ret = av_channel_layout_describe(&avctx->ch_layout, buf, sizeof(buf)); | |
701 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
702 | "Specified channel layout '%s' is not supported by the %s encoder\n", | ||
703 | ✗ | ret > 0 ? buf : "?", c->name); | |
704 | |||
705 | ✗ | av_log(avctx, AV_LOG_ERROR, "Supported channel layouts:\n"); | |
706 | ✗ | for (int p = 0; ch_layouts[p].nb_channels; p++) { | |
707 | ✗ | ret = av_channel_layout_describe(&ch_layouts[p], buf, sizeof(buf)); | |
708 | ✗ | av_log(avctx, AV_LOG_ERROR, " %s\n", ret > 0 ? buf : "?"); | |
709 | } | ||
710 | ✗ | return AVERROR(EINVAL); | |
711 | } | ||
712 | } | ||
713 | |||
714 |
2/2✓ Branch 0 taken 1296 times.
✓ Branch 1 taken 53 times.
|
1349 | if (!avctx->bits_per_raw_sample) |
715 | 1296 | avctx->bits_per_raw_sample = av_get_exact_bits_per_sample(avctx->codec_id); | |
716 |
2/2✓ Branch 0 taken 171 times.
✓ Branch 1 taken 1178 times.
|
1349 | if (!avctx->bits_per_raw_sample) |
717 | 171 | avctx->bits_per_raw_sample = 8 * av_get_bytes_per_sample(avctx->sample_fmt); | |
718 | |||
719 | 1349 | return 0; | |
720 | } | ||
721 | |||
722 | 21339 | int ff_encode_preinit(AVCodecContext *avctx) | |
723 | { | ||
724 | 21339 | AVCodecInternal *avci = avctx->internal; | |
725 | 21339 | EncodeContext *ec = encode_ctx(avci); | |
726 | 21339 | int ret = 0; | |
727 | |||
728 |
2/4✓ Branch 0 taken 21339 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 21339 times.
|
21339 | if (avctx->time_base.num <= 0 || avctx->time_base.den <= 0) { |
729 | ✗ | av_log(avctx, AV_LOG_ERROR, "The encoder timebase is not set.\n"); | |
730 | ✗ | return AVERROR(EINVAL); | |
731 | } | ||
732 | |||
733 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21339 times.
|
21339 | if (avctx->bit_rate < 0) { |
734 | ✗ | av_log(avctx, AV_LOG_ERROR, "The encoder bitrate is negative.\n"); | |
735 | ✗ | return AVERROR(EINVAL); | |
736 | } | ||
737 | |||
738 |
2/2✓ Branch 0 taken 21250 times.
✓ Branch 1 taken 89 times.
|
21339 | if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE && |
739 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21250 times.
|
21250 | !(avctx->codec->capabilities & AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE)) { |
740 | ✗ | av_log(avctx, AV_LOG_ERROR, "The copy_opaque flag is set, but the " | |
741 | "encoder does not support it.\n"); | ||
742 | ✗ | return AVERROR(EINVAL); | |
743 | } | ||
744 | |||
745 |
3/3✓ Branch 0 taken 19952 times.
✓ Branch 1 taken 1349 times.
✓ Branch 2 taken 38 times.
|
21339 | switch (avctx->codec_type) { |
746 | 19952 | case AVMEDIA_TYPE_VIDEO: ret = encode_preinit_video(avctx); break; | |
747 | 1349 | case AVMEDIA_TYPE_AUDIO: ret = encode_preinit_audio(avctx); break; | |
748 | } | ||
749 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21339 times.
|
21339 | if (ret < 0) |
750 | ✗ | return ret; | |
751 | |||
752 |
4/4✓ Branch 0 taken 1387 times.
✓ Branch 1 taken 19952 times.
✓ Branch 2 taken 1349 times.
✓ Branch 3 taken 38 times.
|
21339 | if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO) |
753 |
3/4✓ Branch 0 taken 21280 times.
✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 21280 times.
|
21301 | && avctx->bit_rate>0 && avctx->bit_rate<1000) { |
754 | ✗ | av_log(avctx, AV_LOG_WARNING, "Bitrate %"PRId64" is extremely low, maybe you mean %"PRId64"k\n", avctx->bit_rate, avctx->bit_rate); | |
755 | } | ||
756 | |||
757 |
2/2✓ Branch 0 taken 21337 times.
✓ Branch 1 taken 2 times.
|
21339 | if (!avctx->rc_initial_buffer_occupancy) |
758 | 21337 | avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3LL / 4; | |
759 | |||
760 |
2/2✓ Branch 0 taken 20943 times.
✓ Branch 1 taken 396 times.
|
21339 | if (avctx->codec_descriptor->props & AV_CODEC_PROP_INTRA_ONLY) |
761 | 20943 | ec->intra_only_flag = AV_PKT_FLAG_KEY; | |
762 | |||
763 |
2/2✓ Branch 1 taken 21301 times.
✓ Branch 2 taken 38 times.
|
21339 | if (ffcodec(avctx->codec)->cb_type == FF_CODEC_CB_TYPE_ENCODE) { |
764 | 21301 | avci->in_frame = av_frame_alloc(); | |
765 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21301 times.
|
21301 | if (!avci->in_frame) |
766 | ✗ | return AVERROR(ENOMEM); | |
767 | } | ||
768 | |||
769 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 21337 times.
|
21339 | if ((avctx->flags & AV_CODEC_FLAG_RECON_FRAME)) { |
770 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!(avctx->codec->capabilities & AV_CODEC_CAP_ENCODER_RECON_FRAME)) { |
771 | ✗ | av_log(avctx, AV_LOG_ERROR, "Reconstructed frame output requested " | |
772 | "from an encoder not supporting it\n"); | ||
773 | ✗ | return AVERROR(ENOSYS); | |
774 | } | ||
775 | |||
776 | 2 | avci->recon_frame = av_frame_alloc(); | |
777 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!avci->recon_frame) |
778 | ✗ | return AVERROR(ENOMEM); | |
779 | } | ||
780 | |||
781 |
2/2✓ Branch 0 taken 213390 times.
✓ Branch 1 taken 21339 times.
|
234729 | for (int i = 0; ff_sd_global_map[i].packet < AV_PKT_DATA_NB; i++) { |
782 | 213390 | const enum AVPacketSideDataType type_packet = ff_sd_global_map[i].packet; | |
783 | 213390 | const enum AVFrameSideDataType type_frame = ff_sd_global_map[i].frame; | |
784 | const AVFrameSideData *sd_frame; | ||
785 | AVPacketSideData *sd_packet; | ||
786 | |||
787 | 213390 | sd_frame = av_frame_side_data_get(avctx->decoded_side_data, | |
788 | avctx->nb_decoded_side_data, | ||
789 | type_frame); | ||
790 |
3/4✓ Branch 0 taken 234 times.
✓ Branch 1 taken 213156 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 234 times.
|
213624 | if (!sd_frame || |
791 | 234 | av_packet_side_data_get(avctx->coded_side_data, avctx->nb_coded_side_data, | |
792 | type_packet)) | ||
793 | |||
794 | 213156 | continue; | |
795 | |||
796 | 234 | sd_packet = av_packet_side_data_new(&avctx->coded_side_data, &avctx->nb_coded_side_data, | |
797 | 234 | type_packet, sd_frame->size, 0); | |
798 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 234 times.
|
234 | if (!sd_packet) |
799 | ✗ | return AVERROR(ENOMEM); | |
800 | |||
801 | 234 | memcpy(sd_packet->data, sd_frame->data, sd_frame->size); | |
802 | } | ||
803 | |||
804 | if (CONFIG_FRAME_THREAD_ENCODER) { | ||
805 | 21339 | ret = ff_frame_thread_encoder_init(avctx); | |
806 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21339 times.
|
21339 | if (ret < 0) |
807 | ✗ | return ret; | |
808 | } | ||
809 | |||
810 | 21339 | return 0; | |
811 | } | ||
812 | |||
813 | 10884 | int ff_encode_alloc_frame(AVCodecContext *avctx, AVFrame *frame) | |
814 | { | ||
815 | int ret; | ||
816 | |||
817 |
1/3✓ Branch 0 taken 10884 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
|
10884 | switch (avctx->codec->type) { |
818 | 10884 | case AVMEDIA_TYPE_VIDEO: | |
819 | 10884 | frame->format = avctx->pix_fmt; | |
820 |
3/4✓ Branch 0 taken 10868 times.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10868 times.
|
10884 | if (frame->width <= 0 || frame->height <= 0) { |
821 | 16 | frame->width = FFMAX(avctx->width, avctx->coded_width); | |
822 | 16 | frame->height = FFMAX(avctx->height, avctx->coded_height); | |
823 | } | ||
824 | |||
825 | 10884 | break; | |
826 | ✗ | case AVMEDIA_TYPE_AUDIO: | |
827 | ✗ | frame->sample_rate = avctx->sample_rate; | |
828 | ✗ | frame->format = avctx->sample_fmt; | |
829 | ✗ | if (!frame->ch_layout.nb_channels) { | |
830 | ✗ | ret = av_channel_layout_copy(&frame->ch_layout, &avctx->ch_layout); | |
831 | ✗ | if (ret < 0) | |
832 | ✗ | return ret; | |
833 | } | ||
834 | ✗ | break; | |
835 | } | ||
836 | |||
837 | 10884 | ret = avcodec_default_get_buffer2(avctx, frame, 0); | |
838 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10884 times.
|
10884 | if (ret < 0) { |
839 | ✗ | av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); | |
840 | ✗ | av_frame_unref(frame); | |
841 | ✗ | return ret; | |
842 | } | ||
843 | |||
844 | 10884 | return 0; | |
845 | } | ||
846 | |||
847 | 62 | int ff_encode_receive_frame(AVCodecContext *avctx, AVFrame *frame) | |
848 | { | ||
849 | 62 | AVCodecInternal *avci = avctx->internal; | |
850 | |||
851 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
|
62 | if (!avci->recon_frame) |
852 | ✗ | return AVERROR(EINVAL); | |
853 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
|
62 | if (!avci->recon_frame->buf[0]) |
854 | ✗ | return avci->draining_done ? AVERROR_EOF : AVERROR(EAGAIN); | |
855 | |||
856 | 62 | av_frame_move_ref(frame, avci->recon_frame); | |
857 | 62 | return 0; | |
858 | } | ||
859 | |||
860 | ✗ | void ff_encode_flush_buffers(AVCodecContext *avctx) | |
861 | { | ||
862 | ✗ | AVCodecInternal *avci = avctx->internal; | |
863 | |||
864 | ✗ | if (avci->in_frame) | |
865 | ✗ | av_frame_unref(avci->in_frame); | |
866 | ✗ | if (avci->recon_frame) | |
867 | ✗ | av_frame_unref(avci->recon_frame); | |
868 | ✗ | } | |
869 | |||
870 | 21339 | AVCodecInternal *ff_encode_internal_alloc(void) | |
871 | { | ||
872 | 21339 | return av_mallocz(sizeof(EncodeContext)); | |
873 | } | ||
874 | |||
875 | 204 | AVCPBProperties *ff_encode_add_cpb_side_data(AVCodecContext *avctx) | |
876 | { | ||
877 | AVPacketSideData *tmp; | ||
878 | AVCPBProperties *props; | ||
879 | size_t size; | ||
880 | int i; | ||
881 | |||
882 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 204 times.
|
205 | for (i = 0; i < avctx->nb_coded_side_data; i++) |
883 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (avctx->coded_side_data[i].type == AV_PKT_DATA_CPB_PROPERTIES) |
884 | ✗ | return (AVCPBProperties *)avctx->coded_side_data[i].data; | |
885 | |||
886 | 204 | props = av_cpb_properties_alloc(&size); | |
887 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 204 times.
|
204 | if (!props) |
888 | ✗ | return NULL; | |
889 | |||
890 | 204 | tmp = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*tmp)); | |
891 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 204 times.
|
204 | if (!tmp) { |
892 | ✗ | av_freep(&props); | |
893 | ✗ | return NULL; | |
894 | } | ||
895 | |||
896 | 204 | avctx->coded_side_data = tmp; | |
897 | 204 | avctx->nb_coded_side_data++; | |
898 | |||
899 | 204 | avctx->coded_side_data[avctx->nb_coded_side_data - 1].type = AV_PKT_DATA_CPB_PROPERTIES; | |
900 | 204 | avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props; | |
901 | 204 | avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size; | |
902 | |||
903 | 204 | return props; | |
904 | } | ||
905 | |||
906 | 1612 | int ff_check_codec_matrices(AVCodecContext *avctx, unsigned types, uint16_t min, uint16_t max) | |
907 | { | ||
908 | 1612 | uint16_t *matrices[] = {avctx->intra_matrix, avctx->inter_matrix, avctx->chroma_intra_matrix}; | |
909 | 1612 | const char *names[] = {"Intra", "Inter", "Chroma Intra"}; | |
910 | static_assert(FF_ARRAY_ELEMS(matrices) == FF_ARRAY_ELEMS(names), "matrix count mismatch"); | ||
911 |
2/2✓ Branch 0 taken 4836 times.
✓ Branch 1 taken 1612 times.
|
6448 | for (int m = 0; m < FF_ARRAY_ELEMS(matrices); m++) { |
912 | 4836 | uint16_t *matrix = matrices[m]; | |
913 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 4836 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
4836 | if (matrix && (types & (1U << m))) { |
914 | ✗ | for (int i = 0; i < 64; i++) { | |
915 | ✗ | if (matrix[i] < min || matrix[i] > max) { | |
916 | ✗ | av_log(avctx, AV_LOG_ERROR, "%s matrix[%d] is %d which is out of the allowed range [%"PRIu16"-%"PRIu16"].\n", names[m], i, matrix[i], min, max); | |
917 | ✗ | return AVERROR(EINVAL); | |
918 | } | ||
919 | } | ||
920 | } | ||
921 | } | ||
922 | 1612 | return 0; | |
923 | } | ||
924 |