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