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