libavcodec/encode.c
| 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/avassert.h" | ||
| 22 | #include "libavutil/channel_layout.h" | ||
| 23 | #include "libavutil/emms.h" | ||
| 24 | #include "libavutil/frame.h" | ||
| 25 | #include "libavutil/hwcontext.h" | ||
| 26 | #include "libavutil/internal.h" | ||
| 27 | #include "libavutil/intreadwrite.h" | ||
| 28 | #include "libavutil/mem.h" | ||
| 29 | #include "libavutil/opt.h" | ||
| 30 | #include "libavutil/pixdesc.h" | ||
| 31 | #include "libavutil/samplefmt.h" | ||
| 32 | |||
| 33 | #include "avcodec.h" | ||
| 34 | #include "avcodec_internal.h" | ||
| 35 | #include "codec_desc.h" | ||
| 36 | #include "codec_internal.h" | ||
| 37 | #include "encode.h" | ||
| 38 | #include "frame_thread_encoder.h" | ||
| 39 | #include "internal.h" | ||
| 40 | |||
| 41 | typedef struct EncodeContext { | ||
| 42 | AVCodecInternal avci; | ||
| 43 | |||
| 44 | /** | ||
| 45 | * This is set to AV_PKT_FLAG_KEY for encoders that encode intra-only | ||
| 46 | * formats (i.e. whose codec descriptor has AV_CODEC_PROP_INTRA_ONLY set). | ||
| 47 | * This is used to set said flag generically for said encoders. | ||
| 48 | */ | ||
| 49 | int intra_only_flag; | ||
| 50 | |||
| 51 | /** | ||
| 52 | * An audio frame with less than required samples has been submitted (and | ||
| 53 | * potentially padded with silence). Reject all subsequent frames. | ||
| 54 | */ | ||
| 55 | int last_audio_frame; | ||
| 56 | } EncodeContext; | ||
| 57 | |||
| 58 | 1687879 | static EncodeContext *encode_ctx(AVCodecInternal *avci) | |
| 59 | { | ||
| 60 | 1687879 | return (EncodeContext*)avci; | |
| 61 | } | ||
| 62 | |||
| 63 | 38021 | int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size) | |
| 64 | { | ||
| 65 | 38021 | if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) { | |
| 66 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n", | |
| 67 | size, INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE); | ||
| 68 | ✗ | return AVERROR(EINVAL); | |
| 69 | } | ||
| 70 | |||
| 71 | 38021 | av_assert0(!avpkt->data); | |
| 72 | |||
| 73 | 38021 | av_fast_padded_malloc(&avctx->internal->byte_buffer, | |
| 74 | 38021 | &avctx->internal->byte_buffer_size, size); | |
| 75 | 38021 | avpkt->data = avctx->internal->byte_buffer; | |
| 76 | 38021 | if (!avpkt->data) { | |
| 77 | ✗ | av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size); | |
| 78 | ✗ | return AVERROR(ENOMEM); | |
| 79 | } | ||
| 80 | 38021 | avpkt->size = size; | |
| 81 | |||
| 82 | 38021 | return 0; | |
| 83 | } | ||
| 84 | |||
| 85 | 561592 | int avcodec_default_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int flags) | |
| 86 | { | ||
| 87 | int ret; | ||
| 88 | |||
| 89 | 561592 | if (avpkt->size < 0 || avpkt->size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) | |
| 90 | ✗ | return AVERROR(EINVAL); | |
| 91 | |||
| 92 | 561592 | if (avpkt->data || avpkt->buf) { | |
| 93 | ✗ | av_log(avctx, AV_LOG_ERROR, "avpkt->{data,buf} != NULL in avcodec_default_get_encode_buffer()\n"); | |
| 94 | ✗ | return AVERROR(EINVAL); | |
| 95 | } | ||
| 96 | |||
| 97 | 561592 | ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE); | |
| 98 | 561592 | if (ret < 0) { | |
| 99 | ✗ | av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", avpkt->size); | |
| 100 | ✗ | return ret; | |
| 101 | } | ||
| 102 | 561592 | avpkt->data = avpkt->buf->data; | |
| 103 | |||
| 104 | 561592 | return 0; | |
| 105 | } | ||
| 106 | |||
| 107 | 561592 | int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags) | |
| 108 | { | ||
| 109 | int ret; | ||
| 110 | |||
| 111 | 561592 | if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) | |
| 112 | ✗ | return AVERROR(EINVAL); | |
| 113 | |||
| 114 | 561592 | av_assert0(!avpkt->data && !avpkt->buf); | |
| 115 | |||
| 116 | 561592 | avpkt->size = size; | |
| 117 | 561592 | ret = avctx->get_encode_buffer(avctx, avpkt, flags); | |
| 118 | 561592 | if (ret < 0) | |
| 119 | ✗ | goto fail; | |
| 120 | |||
| 121 | 561592 | if (!avpkt->data || !avpkt->buf) { | |
| 122 | ✗ | av_log(avctx, AV_LOG_ERROR, "No buffer returned by get_encode_buffer()\n"); | |
| 123 | ✗ | ret = AVERROR(EINVAL); | |
| 124 | ✗ | goto fail; | |
| 125 | } | ||
| 126 | 561592 | memset(avpkt->data + avpkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE); | |
| 127 | |||
| 128 | 561592 | ret = 0; | |
| 129 | 561592 | fail: | |
| 130 | 561592 | if (ret < 0) { | |
| 131 | ✗ | av_log(avctx, AV_LOG_ERROR, "get_encode_buffer() failed\n"); | |
| 132 | ✗ | av_packet_unref(avpkt); | |
| 133 | } | ||
| 134 | |||
| 135 | 561592 | return ret; | |
| 136 | } | ||
| 137 | |||
| 138 | 562278 | static int encode_make_refcounted(AVCodecContext *avctx, AVPacket *avpkt) | |
| 139 | { | ||
| 140 | 562278 | uint8_t *data = avpkt->data; | |
| 141 | int ret; | ||
| 142 | |||
| 143 | 562278 | if (avpkt->buf) | |
| 144 | 524257 | return 0; | |
| 145 | |||
| 146 | 38021 | avpkt->data = NULL; | |
| 147 | 38021 | ret = ff_get_encode_buffer(avctx, avpkt, avpkt->size, 0); | |
| 148 | 38021 | if (ret < 0) | |
| 149 | ✗ | return ret; | |
| 150 | 38021 | memcpy(avpkt->data, data, avpkt->size); | |
| 151 | |||
| 152 | 38021 | return 0; | |
| 153 | } | ||
| 154 | |||
| 155 | /** | ||
| 156 | * Pad last frame with silence. | ||
| 157 | */ | ||
| 158 | 226 | static int pad_last_frame(AVCodecContext *s, AVFrame *frame, const AVFrame *src, int out_samples) | |
| 159 | { | ||
| 160 | AVFrameSideData *sd; | ||
| 161 | int discard_padding; | ||
| 162 | int ret; | ||
| 163 | |||
| 164 | 226 | frame->format = src->format; | |
| 165 | 226 | frame->nb_samples = out_samples; | |
| 166 | 226 | ret = av_channel_layout_copy(&frame->ch_layout, &s->ch_layout); | |
| 167 | 226 | if (ret < 0) | |
| 168 | ✗ | goto fail; | |
| 169 | 226 | ret = av_frame_get_buffer(frame, 0); | |
| 170 | 226 | if (ret < 0) | |
| 171 | ✗ | goto fail; | |
| 172 | |||
| 173 | 226 | ret = av_frame_copy_props(frame, src); | |
| 174 | 226 | if (ret < 0) | |
| 175 | ✗ | goto fail; | |
| 176 | |||
| 177 | 226 | if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0, | |
| 178 | 226 | src->nb_samples, s->ch_layout.nb_channels, | |
| 179 | s->sample_fmt)) < 0) | ||
| 180 | ✗ | goto fail; | |
| 181 | 226 | if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples, | |
| 182 | 226 | frame->nb_samples - src->nb_samples, | |
| 183 | s->ch_layout.nb_channels, s->sample_fmt)) < 0) | ||
| 184 | ✗ | goto fail; | |
| 185 | |||
| 186 | 226 | discard_padding = frame->nb_samples - src->nb_samples; | |
| 187 | av_assert1(discard_padding > 0); | ||
| 188 | 226 | sd = av_frame_new_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES, 10); | |
| 189 | 226 | if (!sd) { | |
| 190 | ✗ | ret = AVERROR(ENOMEM); | |
| 191 | ✗ | goto fail; | |
| 192 | } | ||
| 193 | 226 | AV_WL32A(sd->data, 0); | |
| 194 | 226 | AV_WL32A(sd->data + 4, discard_padding); | |
| 195 | 226 | AV_WL16A(sd->data + 8, 0); | |
| 196 | |||
| 197 | 226 | return 0; | |
| 198 | |||
| 199 | ✗ | fail: | |
| 200 | ✗ | av_frame_unref(frame); | |
| 201 | ✗ | encode_ctx(s->internal)->last_audio_frame = 0; | |
| 202 | ✗ | return ret; | |
| 203 | } | ||
| 204 | |||
| 205 | 1061 | int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size, | |
| 206 | const AVSubtitle *sub) | ||
| 207 | { | ||
| 208 | int ret; | ||
| 209 | 1061 | if (sub->start_display_time) { | |
| 210 | ✗ | av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n"); | |
| 211 | ✗ | return -1; | |
| 212 | } | ||
| 213 | 1061 | av_assert0(buf_size >= 0); | |
| 214 | |||
| 215 | 1061 | ret = ffcodec(avctx->codec)->cb.encode_sub(avctx, buf, buf_size, sub); | |
| 216 | 1061 | avctx->frame_num++; | |
| 217 | 1061 | return ret; | |
| 218 | } | ||
| 219 | |||
| 220 | 1127854 | int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame) | |
| 221 | { | ||
| 222 | 1127854 | AVCodecInternal *avci = avctx->internal; | |
| 223 | |||
| 224 | 1127854 | if (avci->draining) | |
| 225 | ✗ | return AVERROR_EOF; | |
| 226 | |||
| 227 | 1127854 | if (!avci->buffer_frame->buf[0]) | |
| 228 | 565608 | return AVERROR(EAGAIN); | |
| 229 | |||
| 230 | 562246 | av_frame_move_ref(frame, avci->buffer_frame); | |
| 231 | |||
| 232 | 562246 | return 0; | |
| 233 | } | ||
| 234 | |||
| 235 | 540152 | static int encode_set_packet_props(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame) | |
| 236 | { | ||
| 237 | 540152 | AVCodecInternal *avci = avctx->internal; | |
| 238 | 540152 | EncodeContext *ec = encode_ctx(avci); | |
| 239 | |||
| 240 | 540152 | if (avpkt->pts == AV_NOPTS_VALUE) { | |
| 241 | 514839 | avpkt->pts = frame->pts; | |
| 242 | 514839 | if (avctx->codec->type == AVMEDIA_TYPE_AUDIO && avpkt->pts != AV_NOPTS_VALUE) | |
| 243 | 377071 | avpkt->pts -= ff_samples_to_time_base(avctx, avctx->initial_padding); | |
| 244 | } | ||
| 245 | |||
| 246 | 540152 | if (!avpkt->duration) { | |
| 247 | 535563 | if (frame->duration) | |
| 248 | 513494 | avpkt->duration = frame->duration; | |
| 249 | 22069 | else if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) { | |
| 250 | 3200 | avpkt->duration = ff_samples_to_time_base(avctx, | |
| 251 | 3200 | frame->nb_samples); | |
| 252 | } | ||
| 253 | 535563 | if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) { | |
| 254 | 400991 | AVFrameSideData *frame_sd = av_frame_get_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES); | |
| 255 | |||
| 256 | 400991 | if (frame_sd && frame_sd->size >= 10) { | |
| 257 | 264 | int skip_samples = AV_RL32(frame_sd->data + 0); | |
| 258 | 264 | int discard_padding = AV_RL32(frame_sd->data + 4); | |
| 259 | |||
| 260 | 264 | if (discard_padding > 0 && avctx->frame_size && ec->last_audio_frame) { | |
| 261 | 222 | if (!frame->duration) | |
| 262 | ✗ | avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples - discard_padding); | |
| 263 | 222 | avpkt->duration = av_sat_add64(avpkt->duration, ff_samples_to_time_base(avctx, avctx->initial_padding)); | |
| 264 | 222 | avpkt->duration = FFMIN(avpkt->duration, ff_samples_to_time_base(avctx, frame->nb_samples)); | |
| 265 | 222 | discard_padding = FFMAX(0, discard_padding - avctx->initial_padding); | |
| 266 | } | ||
| 267 | |||
| 268 | 264 | if (skip_samples > 0 || discard_padding > 0) { | |
| 269 | 141 | uint8_t *packet_sd = av_packet_new_side_data(avpkt, AV_PKT_DATA_SKIP_SAMPLES, 10); | |
| 270 | 141 | if (!packet_sd) | |
| 271 | ✗ | return AVERROR(ENOMEM); | |
| 272 | 141 | AV_WL32A(packet_sd + 0, skip_samples); | |
| 273 | 141 | AV_WL32A(packet_sd + 4, discard_padding); | |
| 274 | 141 | AV_WL8 (packet_sd + 8, AV_RB8(frame_sd->data + 8)); | |
| 275 | 141 | AV_WL8 (packet_sd + 9, AV_RB8(frame_sd->data + 9)); | |
| 276 | } | ||
| 277 | } | ||
| 278 | } | ||
| 279 | } | ||
| 280 | |||
| 281 | 540152 | return 0; | |
| 282 | } | ||
| 283 | |||
| 284 | 554198 | int ff_encode_reordered_opaque(AVCodecContext *avctx, | |
| 285 | AVPacket *pkt, const AVFrame *frame) | ||
| 286 | { | ||
| 287 | 554198 | if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) { | |
| 288 | 550521 | int ret = av_buffer_replace(&pkt->opaque_ref, frame->opaque_ref); | |
| 289 | 550521 | if (ret < 0) | |
| 290 | ✗ | return ret; | |
| 291 | 550521 | pkt->opaque = frame->opaque; | |
| 292 | } | ||
| 293 | |||
| 294 | 554198 | return 0; | |
| 295 | } | ||
| 296 | |||
| 297 | 563056 | int ff_encode_encode_cb(AVCodecContext *avctx, AVPacket *avpkt, | |
| 298 | AVFrame *frame, int *got_packet) | ||
| 299 | { | ||
| 300 | 563056 | const FFCodec *const codec = ffcodec(avctx->codec); | |
| 301 | int ret; | ||
| 302 | |||
| 303 | 563056 | ret = codec->cb.encode(avctx, avpkt, frame, got_packet); | |
| 304 | ff_assert1_fpu(); | ||
| 305 | 563056 | av_assert0(ret <= 0); | |
| 306 | |||
| 307 | 563056 | if (!ret && *got_packet) { | |
| 308 | 562377 | if (avpkt->data) { | |
| 309 | 562278 | ret = encode_make_refcounted(avctx, avpkt); | |
| 310 | 562278 | if (ret < 0) | |
| 311 | ✗ | goto unref; | |
| 312 | // Date returned by encoders must always be ref-counted | ||
| 313 | 562278 | av_assert0(avpkt->buf); | |
| 314 | } | ||
| 315 | |||
| 316 | // set the timestamps for the simple no-delay case | ||
| 317 | // encoders with delay have to set the timestamps themselves | ||
| 318 | 562377 | if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || | |
| 319 | 37877 | (frame && (codec->caps_internal & FF_CODEC_CAP_EOF_FLUSH))) { | |
| 320 | 540152 | ret = encode_set_packet_props(avctx, avpkt, frame); | |
| 321 | 540152 | if (ret < 0) | |
| 322 | ✗ | goto unref; | |
| 323 | |||
| 324 | 540152 | ret = ff_encode_reordered_opaque(avctx, avpkt, frame); | |
| 325 | 540152 | if (ret < 0) | |
| 326 | ✗ | goto unref; | |
| 327 | } | ||
| 328 | |||
| 329 | // dts equals pts unless there is reordering | ||
| 330 | // there can be no reordering if there is no encoder delay | ||
| 331 | 562377 | if (!(avctx->codec_descriptor->props & AV_CODEC_PROP_REORDER) || | |
| 332 | 14785 | !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || | |
| 333 | 14020 | (codec->caps_internal & FF_CODEC_CAP_EOF_FLUSH)) | |
| 334 | 548357 | avpkt->dts = avpkt->pts; | |
| 335 | } else { | ||
| 336 | 679 | unref: | |
| 337 | 679 | av_packet_unref(avpkt); | |
| 338 | } | ||
| 339 | |||
| 340 | 563056 | if (frame) | |
| 341 | 562246 | av_frame_unref(frame); | |
| 342 | |||
| 343 | 563056 | return ret; | |
| 344 | } | ||
| 345 | |||
| 346 | 1142380 | static int encode_simple_internal(AVCodecContext *avctx, AVPacket *avpkt) | |
| 347 | { | ||
| 348 | 1142380 | AVCodecInternal *avci = avctx->internal; | |
| 349 | 1142380 | AVFrame *frame = avci->in_frame; | |
| 350 | 1142380 | const FFCodec *const codec = ffcodec(avctx->codec); | |
| 351 | int got_packet; | ||
| 352 | int ret; | ||
| 353 | |||
| 354 | 1142380 | if (avci->draining_done) | |
| 355 | 2155 | return AVERROR_EOF; | |
| 356 | |||
| 357 | 1140225 | if (!frame->buf[0] && !avci->draining) { | |
| 358 | 1127854 | av_frame_unref(frame); | |
| 359 | 1127854 | ret = ff_encode_get_frame(avctx, frame); | |
| 360 | 1127854 | if (ret < 0 && ret != AVERROR_EOF) | |
| 361 | 565608 | return ret; | |
| 362 | } | ||
| 363 | |||
| 364 | 574617 | if (!frame->buf[0]) { | |
| 365 | 12371 | if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY || | |
| 366 | 11561 | avci->frame_thread_encoder)) | |
| 367 | 6652 | return AVERROR_EOF; | |
| 368 | |||
| 369 | // Flushing is signaled with a NULL frame | ||
| 370 | 5719 | frame = NULL; | |
| 371 | } | ||
| 372 | |||
| 373 | 567965 | got_packet = 0; | |
| 374 | |||
| 375 | 567965 | av_assert0(codec->cb_type == FF_CODEC_CB_TYPE_ENCODE); | |
| 376 | |||
| 377 | #if CONFIG_FRAME_THREAD_ENCODER | ||
| 378 | 567965 | if (avci->frame_thread_encoder) | |
| 379 | /* This will unref frame. */ | ||
| 380 | 65862 | ret = ff_thread_video_encode_frame(avctx, avpkt, frame, &got_packet); | |
| 381 | else | ||
| 382 | #endif | ||
| 383 | 502103 | ret = ff_encode_encode_cb(avctx, avpkt, frame, &got_packet); | |
| 384 | |||
| 385 | 567965 | if (avci->draining && !got_packet) | |
| 386 | 2155 | avci->draining_done = 1; | |
| 387 | |||
| 388 | 567965 | return ret; | |
| 389 | } | ||
| 390 | |||
| 391 | 1136792 | static int encode_simple_receive_packet(AVCodecContext *avctx, AVPacket *avpkt) | |
| 392 | { | ||
| 393 | int ret; | ||
| 394 | |||
| 395 | 1704757 | while (!avpkt->data && !avpkt->side_data) { | |
| 396 | 1142380 | ret = encode_simple_internal(avctx, avpkt); | |
| 397 | 1142380 | if (ret < 0) | |
| 398 | 574415 | return ret; | |
| 399 | } | ||
| 400 | |||
| 401 | 562377 | return 0; | |
| 402 | } | ||
| 403 | |||
| 404 | 1143589 | static int encode_receive_packet_internal(AVCodecContext *avctx, AVPacket *avpkt) | |
| 405 | { | ||
| 406 | 1143589 | AVCodecInternal *avci = avctx->internal; | |
| 407 | int ret; | ||
| 408 | |||
| 409 | 1143589 | if (avci->draining_done) | |
| 410 | 6797 | return AVERROR_EOF; | |
| 411 | |||
| 412 | 1136792 | av_assert0(!avpkt->data && !avpkt->side_data); | |
| 413 | |||
| 414 | 1136792 | if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) { | |
| 415 | 316327 | if ((avctx->flags & AV_CODEC_FLAG_PASS1) && avctx->stats_out) | |
| 416 | 808 | avctx->stats_out[0] = '\0'; | |
| 417 | } | ||
| 418 | |||
| 419 | 1136792 | if (ffcodec(avctx->codec)->cb_type == FF_CODEC_CB_TYPE_RECEIVE_PACKET) { | |
| 420 | ✗ | ret = ffcodec(avctx->codec)->cb.receive_packet(avctx, avpkt); | |
| 421 | ✗ | if (ret < 0) | |
| 422 | ✗ | av_packet_unref(avpkt); | |
| 423 | else | ||
| 424 | // Encoders must always return ref-counted buffers. | ||
| 425 | // Side-data only packets have no data and can be not ref-counted. | ||
| 426 | ✗ | av_assert0(!avpkt->data || avpkt->buf); | |
| 427 | } else | ||
| 428 | 1136792 | ret = encode_simple_receive_packet(avctx, avpkt); | |
| 429 | 1136792 | if (ret >= 0) | |
| 430 | 562377 | avpkt->flags |= encode_ctx(avci)->intra_only_flag; | |
| 431 | |||
| 432 | 1136792 | if (ret == AVERROR_EOF) | |
| 433 | 8807 | avci->draining_done = 1; | |
| 434 | |||
| 435 | 1136792 | return ret; | |
| 436 | } | ||
| 437 | |||
| 438 | #if CONFIG_LCMS2 | ||
| 439 | static int encode_generate_icc_profile(AVCodecContext *avctx, AVFrame *frame) | ||
| 440 | { | ||
| 441 | enum AVColorTransferCharacteristic trc = frame->color_trc; | ||
| 442 | enum AVColorPrimaries prim = frame->color_primaries; | ||
| 443 | const FFCodec *const codec = ffcodec(avctx->codec); | ||
| 444 | AVCodecInternal *avci = avctx->internal; | ||
| 445 | cmsHPROFILE profile; | ||
| 446 | int ret; | ||
| 447 | |||
| 448 | /* don't generate ICC profiles if disabled or unsupported */ | ||
| 449 | if (!(avctx->flags2 & AV_CODEC_FLAG2_ICC_PROFILES)) | ||
| 450 | return 0; | ||
| 451 | if (!(codec->caps_internal & FF_CODEC_CAP_ICC_PROFILES)) | ||
| 452 | return 0; | ||
| 453 | |||
| 454 | if (trc == AVCOL_TRC_UNSPECIFIED) | ||
| 455 | trc = avctx->color_trc; | ||
| 456 | if (prim == AVCOL_PRI_UNSPECIFIED) | ||
| 457 | prim = avctx->color_primaries; | ||
| 458 | if (trc == AVCOL_TRC_UNSPECIFIED || prim == AVCOL_PRI_UNSPECIFIED) | ||
| 459 | return 0; /* can't generate ICC profile with missing csp tags */ | ||
| 460 | |||
| 461 | if (av_frame_get_side_data(frame, AV_FRAME_DATA_ICC_PROFILE)) | ||
| 462 | return 0; /* don't overwrite existing ICC profile */ | ||
| 463 | |||
| 464 | if (!avci->icc.avctx) { | ||
| 465 | ret = ff_icc_context_init(&avci->icc, avctx); | ||
| 466 | if (ret < 0) | ||
| 467 | return ret; | ||
| 468 | } | ||
| 469 | |||
| 470 | ret = ff_icc_profile_generate(&avci->icc, prim, trc, &profile); | ||
| 471 | if (ret < 0) | ||
| 472 | return ret; | ||
| 473 | |||
| 474 | ret = ff_icc_profile_attach(&avci->icc, profile, frame); | ||
| 475 | cmsCloseProfile(profile); | ||
| 476 | return ret; | ||
| 477 | } | ||
| 478 | #else /* !CONFIG_LCMS2 */ | ||
| 479 | 153001 | static int encode_generate_icc_profile(av_unused AVCodecContext *c, av_unused AVFrame *f) | |
| 480 | { | ||
| 481 | 153001 | return 0; | |
| 482 | } | ||
| 483 | #endif | ||
| 484 | |||
| 485 | 562246 | static int encode_send_frame_internal(AVCodecContext *avctx, const AVFrame *src) | |
| 486 | { | ||
| 487 | 562246 | AVCodecInternal *avci = avctx->internal; | |
| 488 | 562246 | EncodeContext *ec = encode_ctx(avci); | |
| 489 | 562246 | AVFrame *dst = avci->buffer_frame; | |
| 490 | int ret; | ||
| 491 | |||
| 492 | 562246 | if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) { | |
| 493 | /* extract audio service type metadata */ | ||
| 494 | 409245 | AVFrameSideData *sd = av_frame_get_side_data(src, AV_FRAME_DATA_AUDIO_SERVICE_TYPE); | |
| 495 | 409245 | if (sd && sd->size >= sizeof(enum AVAudioServiceType)) | |
| 496 | 642 | avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data; | |
| 497 | |||
| 498 | /* check for valid frame size */ | ||
| 499 | 409245 | if (avctx->frame_size) { | |
| 500 | /* if we already got an undersized frame, that must have been the last */ | ||
| 501 | 116239 | if (ec->last_audio_frame) { | |
| 502 | ✗ | av_log(avctx, AV_LOG_ERROR, "frame_size (%d) was not respected for a non-last frame\n", avctx->frame_size); | |
| 503 | ✗ | return AVERROR(EINVAL); | |
| 504 | } | ||
| 505 | 116239 | if (src->nb_samples > avctx->frame_size) { | |
| 506 | ✗ | av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) > frame_size (%d)\n", src->nb_samples, avctx->frame_size); | |
| 507 | ✗ | return AVERROR(EINVAL); | |
| 508 | } | ||
| 509 | 116239 | if (src->nb_samples < avctx->frame_size) { | |
| 510 | 333 | ec->last_audio_frame = 1; | |
| 511 | 333 | if (!(avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) || | |
| 512 | 157 | (avctx->flags2 & AV_CODEC_FLAG2_FIXED_FRAME_SIZE)) { | |
| 513 | 236 | int pad_samples = avci->pad_samples && !(avctx->flags2 & AV_CODEC_FLAG2_FIXED_FRAME_SIZE) ? | |
| 514 | 236 | avci->pad_samples : avctx->frame_size; | |
| 515 | 236 | int out_samples = (src->nb_samples + pad_samples - 1) / pad_samples * pad_samples; | |
| 516 | |||
| 517 | 236 | if (out_samples != src->nb_samples) { | |
| 518 | 226 | ret = pad_last_frame(avctx, dst, src, out_samples); | |
| 519 | 226 | if (ret < 0) | |
| 520 | ✗ | return ret; | |
| 521 | 226 | goto finish; | |
| 522 | } | ||
| 523 | } | ||
| 524 | } | ||
| 525 | } | ||
| 526 | 153001 | } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE)) { | |
| 527 | 33980 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(src->format); | |
| 528 | |||
| 529 | 33980 | if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL) { | |
| 530 | ✗ | if (src->hw_frames_ctx) { | |
| 531 | ✗ | const AVHWFramesContext *frames_ctx = (AVHWFramesContext *)src->hw_frames_ctx->data; | |
| 532 | |||
| 533 | ✗ | if (frames_ctx->width < avctx->width || frames_ctx->height < avctx->height) { | |
| 534 | ✗ | av_log(avctx, AV_LOG_ERROR, "Hardware frames are smaller than the encoder\n"); | |
| 535 | ✗ | return AVERROR(EINVAL); | |
| 536 | } | ||
| 537 | } | ||
| 538 | 33980 | } else if (src->format != avctx->pix_fmt || | |
| 539 | 33980 | src->width != avctx->width || src->height != avctx->height) { | |
| 540 | ✗ | av_log(avctx, AV_LOG_ERROR, "Frame size or pixel format does not match the encoder\n"); | |
| 541 | ✗ | return AVERROR(EINVAL); | |
| 542 | } | ||
| 543 | } | ||
| 544 | |||
| 545 | 562020 | ret = av_frame_ref(dst, src); | |
| 546 | 562020 | if (ret < 0) | |
| 547 | ✗ | return ret; | |
| 548 | |||
| 549 | 562020 | finish: | |
| 550 | |||
| 551 | 562246 | if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) { | |
| 552 | 153001 | ret = encode_generate_icc_profile(avctx, dst); | |
| 553 | 153001 | if (ret < 0) | |
| 554 | ✗ | return ret; | |
| 555 | } | ||
| 556 | |||
| 557 | // unset frame duration unless AV_CODEC_FLAG_FRAME_DURATION is set, | ||
| 558 | // since otherwise we cannot be sure that whatever value it has is in the | ||
| 559 | // right timebase, so we would produce an incorrect value, which is worse | ||
| 560 | // than none at all | ||
| 561 | 562246 | if (!(avctx->flags & AV_CODEC_FLAG_FRAME_DURATION)) | |
| 562 | 3309 | dst->duration = 0; | |
| 563 | |||
| 564 | 562246 | return 0; | |
| 565 | } | ||
| 566 | |||
| 567 | 571055 | int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame) | |
| 568 | { | ||
| 569 | 571055 | AVCodecInternal *avci = avctx->internal; | |
| 570 | int ret; | ||
| 571 | |||
| 572 | 571055 | if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec)) | |
| 573 | ✗ | return AVERROR(EINVAL); | |
| 574 | |||
| 575 | 571055 | if (avci->draining) | |
| 576 | 2 | return AVERROR_EOF; | |
| 577 | |||
| 578 | 571053 | if (avci->buffer_frame->buf[0]) | |
| 579 | ✗ | return AVERROR(EAGAIN); | |
| 580 | |||
| 581 | 571053 | if (!frame) { | |
| 582 | 8807 | avci->draining = 1; | |
| 583 | } else { | ||
| 584 | 562246 | ret = encode_send_frame_internal(avctx, frame); | |
| 585 | 562246 | if (ret < 0) | |
| 586 | ✗ | return ret; | |
| 587 | } | ||
| 588 | |||
| 589 | 571053 | if (!avci->buffer_pkt->data && !avci->buffer_pkt->side_data) { | |
| 590 | 571053 | ret = encode_receive_packet_internal(avctx, avci->buffer_pkt); | |
| 591 | 571053 | if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) | |
| 592 | ✗ | return ret; | |
| 593 | } | ||
| 594 | |||
| 595 | 571053 | avctx->frame_num++; | |
| 596 | |||
| 597 | 571053 | return 0; | |
| 598 | } | ||
| 599 | |||
| 600 | 1133359 | int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt) | |
| 601 | { | ||
| 602 | 1133359 | AVCodecInternal *avci = avctx->internal; | |
| 603 | int ret; | ||
| 604 | |||
| 605 | 1133359 | av_packet_unref(avpkt); | |
| 606 | |||
| 607 | 1133359 | if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec)) | |
| 608 | ✗ | return AVERROR(EINVAL); | |
| 609 | |||
| 610 | 1133359 | if (avci->buffer_pkt->data || avci->buffer_pkt->side_data) { | |
| 611 | 560823 | av_packet_move_ref(avpkt, avci->buffer_pkt); | |
| 612 | } else { | ||
| 613 | 572536 | ret = encode_receive_packet_internal(avctx, avpkt); | |
| 614 | 572536 | if (ret < 0) | |
| 615 | 570982 | return ret; | |
| 616 | } | ||
| 617 | |||
| 618 | 562377 | return 0; | |
| 619 | } | ||
| 620 | |||
| 621 | ✗ | av_cold int ff_encode_reconf_parse_dict(AVCodecContext *avctx, AVDictionary **dict) | |
| 622 | { | ||
| 623 | ✗ | const AVCodec *codec = avctx->codec; | |
| 624 | ✗ | const FFCodec *codec2 = ffcodec(codec); | |
| 625 | ✗ | const AVDictionaryEntry *t = NULL; | |
| 626 | ✗ | AVDictionary *copy = NULL; | |
| 627 | int ret; | ||
| 628 | |||
| 629 | ✗ | av_assert0(av_codec_is_encoder(codec) && (codec->capabilities & AV_CODEC_CAP_ENCODER_RECONF)); | |
| 630 | |||
| 631 | ✗ | ret = av_dict_copy(©, *dict, 0); | |
| 632 | ✗ | if (ret < 0) | |
| 633 | ✗ | goto end; | |
| 634 | |||
| 635 | // Remove the dictionary entries that would be applied to the private codec context | ||
| 636 | ✗ | if (codec->priv_class) { | |
| 637 | ✗ | while ((t = av_dict_iterate(*dict, t))) { | |
| 638 | ✗ | if (av_opt_find(avctx->priv_data, t->key, NULL, | |
| 639 | AV_OPT_FLAG_RUNTIME_PARAM, 0)) | ||
| 640 | ✗ | av_dict_set(dict, t->key, NULL, 0); | |
| 641 | } | ||
| 642 | } | ||
| 643 | |||
| 644 | // Ditto for global options | ||
| 645 | ✗ | if (codec2->defaults) { | |
| 646 | ✗ | const FFCodecDefault *d = codec2->defaults; | |
| 647 | ✗ | while (d->key) { | |
| 648 | ✗ | if (d->flags & AV_OPT_FLAG_RUNTIME_PARAM) | |
| 649 | ✗ | av_dict_set(dict, d->key, NULL, 0); | |
| 650 | ✗ | d++; | |
| 651 | } | ||
| 652 | } | ||
| 653 | |||
| 654 | // If any entry remains, then the requested option/s don't exist or are not settable. | ||
| 655 | ✗ | if (av_dict_count(*dict)) { | |
| 656 | ✗ | ret = AVERROR_OPTION_NOT_FOUND; | |
| 657 | ✗ | goto end; | |
| 658 | } | ||
| 659 | |||
| 660 | ✗ | ret = av_dict_copy(dict, copy, 0); | |
| 661 | ✗ | if (ret < 0) | |
| 662 | ✗ | goto end; | |
| 663 | |||
| 664 | // Do a dry run of applying the options, to ensure the encoder is unchanged in case | ||
| 665 | // one of them has an invalid value. | ||
| 666 | // This is done twice, once for avctx and once for the AVCodec, because using the | ||
| 667 | // search children flag in combination with the fake obj flag will iterate through | ||
| 668 | // the options from all compiled in codecs if you pass the avctx class. | ||
| 669 | ✗ | if (codec->priv_class) { | |
| 670 | ✗ | ret = av_opt_set_dict2((void *)&codec->priv_class, ©, AV_OPT_SEARCH_FAKE_OBJ); | |
| 671 | ✗ | if (ret < 0) | |
| 672 | ✗ | goto end; | |
| 673 | } | ||
| 674 | ✗ | ret = av_opt_set_dict2((void *)&avctx->av_class, ©, AV_OPT_SEARCH_FAKE_OBJ); | |
| 675 | ✗ | if (ret < 0) | |
| 676 | ✗ | goto end; | |
| 677 | |||
| 678 | // The dictionary should be empty. | ||
| 679 | ✗ | av_assert0(!av_dict_count(copy)); | |
| 680 | |||
| 681 | ✗ | ret = av_opt_set_dict2(avctx, dict, AV_OPT_SEARCH_CHILDREN); | |
| 682 | ✗ | if (ret < 0) | |
| 683 | ✗ | goto end; | |
| 684 | |||
| 685 | // The dictionary should be empty. | ||
| 686 | ✗ | av_assert0(!av_dict_count(*dict)); | |
| 687 | |||
| 688 | ✗ | ret = 0; | |
| 689 | ✗ | end: | |
| 690 | ✗ | av_dict_free(©); | |
| 691 | |||
| 692 | ✗ | return ret; | |
| 693 | } | ||
| 694 | |||
| 695 | ✗ | av_cold int avcodec_encode_reconfigure(AVCodecContext *avctx, AVDictionary **dict) | |
| 696 | { | ||
| 697 | ✗ | const FFCodec *codec = ffcodec(avctx->codec); | |
| 698 | ✗ | int ret = AVERROR_BUG; | |
| 699 | |||
| 700 | ✗ | if (!dict || !*dict || !avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec)) | |
| 701 | ✗ | return AVERROR(EINVAL); | |
| 702 | |||
| 703 | ✗ | if (!(avctx->codec->capabilities & AV_CODEC_CAP_ENCODER_RECONF)) { | |
| 704 | ✗ | av_log(avctx, AV_LOG_ERROR, "This encoder does not support reconfiguration\n"); | |
| 705 | ✗ | return AVERROR(ENOSYS); | |
| 706 | } | ||
| 707 | |||
| 708 | ✗ | if (codec->reconf) | |
| 709 | ✗ | ret = codec->reconf(avctx, dict); | |
| 710 | else | ||
| 711 | ✗ | ret = ff_encode_reconf_parse_dict(avctx, dict); | |
| 712 | ✗ | if (ret < 0) | |
| 713 | ✗ | return ret; | |
| 714 | |||
| 715 | ✗ | return 0; | |
| 716 | } | ||
| 717 | |||
| 718 | 21344 | static int encode_preinit_video(AVCodecContext *avctx) | |
| 719 | { | ||
| 720 | 21344 | const AVCodec *c = avctx->codec; | |
| 721 | 21344 | const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt); | |
| 722 | const enum AVPixelFormat *pix_fmts; | ||
| 723 | int ret, i, num_pix_fmts; | ||
| 724 | |||
| 725 | 21344 | if (!pixdesc) { | |
| 726 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid video pixel format: %d\n", | |
| 727 | ✗ | avctx->pix_fmt); | |
| 728 | ✗ | return AVERROR(EINVAL); | |
| 729 | } | ||
| 730 | |||
| 731 | 21344 | ret = avcodec_get_supported_config(avctx, NULL, AV_CODEC_CONFIG_PIX_FORMAT, | |
| 732 | 0, (const void **) &pix_fmts, &num_pix_fmts); | ||
| 733 | 21344 | if (ret < 0) | |
| 734 | ✗ | return ret; | |
| 735 | |||
| 736 | 21344 | if (pix_fmts) { | |
| 737 | 2989 | for (i = 0; i < num_pix_fmts; i++) | |
| 738 | 2989 | if (avctx->pix_fmt == pix_fmts[i]) | |
| 739 | 888 | break; | |
| 740 | 888 | if (i == num_pix_fmts) { | |
| 741 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 742 | "Specified pixel format %s is not supported by the %s encoder.\n", | ||
| 743 | ✗ | av_get_pix_fmt_name(avctx->pix_fmt), c->name); | |
| 744 | |||
| 745 | ✗ | av_log(avctx, AV_LOG_ERROR, "Supported pixel formats:\n"); | |
| 746 | ✗ | for (int p = 0; pix_fmts[p] != AV_PIX_FMT_NONE; p++) { | |
| 747 | ✗ | av_log(avctx, AV_LOG_ERROR, " %s\n", | |
| 748 | ✗ | av_get_pix_fmt_name(pix_fmts[p])); | |
| 749 | } | ||
| 750 | |||
| 751 | ✗ | return AVERROR(EINVAL); | |
| 752 | } | ||
| 753 | 888 | if (pix_fmts[i] == AV_PIX_FMT_YUVJ420P || | |
| 754 | 862 | pix_fmts[i] == AV_PIX_FMT_YUVJ411P || | |
| 755 | 862 | pix_fmts[i] == AV_PIX_FMT_YUVJ422P || | |
| 756 | 858 | pix_fmts[i] == AV_PIX_FMT_YUVJ440P || | |
| 757 | 858 | pix_fmts[i] == AV_PIX_FMT_YUVJ444P) | |
| 758 | 40 | avctx->color_range = AVCOL_RANGE_JPEG; | |
| 759 | } | ||
| 760 | |||
| 761 | 21344 | if (pixdesc->flags & AV_PIX_FMT_FLAG_ALPHA) { | |
| 762 | const enum AVAlphaMode *alpha_modes; | ||
| 763 | int num_alpha_modes; | ||
| 764 | 2684 | ret = avcodec_get_supported_config(avctx, NULL, AV_CODEC_CONFIG_ALPHA_MODE, | |
| 765 | 0, (const void **) &alpha_modes, &num_alpha_modes); | ||
| 766 | 2684 | if (ret < 0) | |
| 767 | ✗ | return ret; | |
| 768 | |||
| 769 | 2684 | if (avctx->alpha_mode != AVALPHA_MODE_UNSPECIFIED && alpha_modes) { | |
| 770 | 4 | for (i = 0; i < num_alpha_modes; i++) { | |
| 771 | 4 | if (avctx->alpha_mode == alpha_modes[i]) | |
| 772 | 4 | break; | |
| 773 | } | ||
| 774 | 4 | if (i == num_alpha_modes) { | |
| 775 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 776 | "Specified alpha mode '%s' is not supported by the %s encoder.\n", | ||
| 777 | ✗ | av_alpha_mode_name(avctx->alpha_mode), c->name); | |
| 778 | ✗ | av_log(avctx, AV_LOG_ERROR, "Supported alpha modes:\n"); | |
| 779 | ✗ | for (int p = 0; alpha_modes[p] != AVALPHA_MODE_UNSPECIFIED; p++) { | |
| 780 | ✗ | av_log(avctx, AV_LOG_ERROR, " %s\n", | |
| 781 | ✗ | av_alpha_mode_name(alpha_modes[p])); | |
| 782 | } | ||
| 783 | ✗ | return AVERROR(EINVAL); | |
| 784 | } | ||
| 785 | } | ||
| 786 | } | ||
| 787 | |||
| 788 | 21344 | if ( avctx->bits_per_raw_sample < 0 | |
| 789 | 21344 | || (avctx->bits_per_raw_sample > 8 && pixdesc->comp[0].depth <= 8)) { | |
| 790 | ✗ | av_log(avctx, AV_LOG_WARNING, "Specified bit depth %d not possible with the specified pixel formats depth %d\n", | |
| 791 | ✗ | avctx->bits_per_raw_sample, pixdesc->comp[0].depth); | |
| 792 | ✗ | avctx->bits_per_raw_sample = pixdesc->comp[0].depth; | |
| 793 | } | ||
| 794 | 21344 | if (avctx->width <= 0 || avctx->height <= 0) { | |
| 795 | ✗ | av_log(avctx, AV_LOG_ERROR, "dimensions not set\n"); | |
| 796 | ✗ | return AVERROR(EINVAL); | |
| 797 | } | ||
| 798 | |||
| 799 | 21344 | if (avctx->hw_frames_ctx) { | |
| 800 | ✗ | AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data; | |
| 801 | ✗ | if (frames_ctx->format != avctx->pix_fmt) { | |
| 802 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 803 | "Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format\n"); | ||
| 804 | ✗ | return AVERROR(EINVAL); | |
| 805 | } | ||
| 806 | ✗ | if (avctx->sw_pix_fmt != AV_PIX_FMT_NONE && | |
| 807 | ✗ | avctx->sw_pix_fmt != frames_ctx->sw_format) { | |
| 808 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 809 | "Mismatching AVCodecContext.sw_pix_fmt (%s) " | ||
| 810 | "and AVHWFramesContext.sw_format (%s)\n", | ||
| 811 | av_get_pix_fmt_name(avctx->sw_pix_fmt), | ||
| 812 | av_get_pix_fmt_name(frames_ctx->sw_format)); | ||
| 813 | ✗ | return AVERROR(EINVAL); | |
| 814 | } | ||
| 815 | ✗ | avctx->sw_pix_fmt = frames_ctx->sw_format; | |
| 816 | } | ||
| 817 | |||
| 818 | 21344 | return 0; | |
| 819 | } | ||
| 820 | |||
| 821 | 1713 | static int encode_preinit_audio(AVCodecContext *avctx) | |
| 822 | { | ||
| 823 | 1713 | const AVCodec *c = avctx->codec; | |
| 824 | const enum AVSampleFormat *sample_fmts; | ||
| 825 | const int *supported_samplerates; | ||
| 826 | const AVChannelLayout *ch_layouts; | ||
| 827 | int ret, i, num_sample_fmts, num_samplerates, num_ch_layouts; | ||
| 828 | |||
| 829 | 1713 | if (!av_get_sample_fmt_name(avctx->sample_fmt)) { | |
| 830 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid audio sample format: %d\n", | |
| 831 | ✗ | avctx->sample_fmt); | |
| 832 | ✗ | return AVERROR(EINVAL); | |
| 833 | } | ||
| 834 | |||
| 835 | 1713 | ret = avcodec_get_supported_config(avctx, NULL, AV_CODEC_CONFIG_SAMPLE_FORMAT, | |
| 836 | 0, (const void **) &sample_fmts, | ||
| 837 | &num_sample_fmts); | ||
| 838 | 1713 | if (ret < 0) | |
| 839 | ✗ | return ret; | |
| 840 | 1713 | if (sample_fmts) { | |
| 841 | 1741 | for (i = 0; i < num_sample_fmts; i++) { | |
| 842 | 1741 | if (avctx->sample_fmt == sample_fmts[i]) | |
| 843 | 1713 | break; | |
| 844 | 42 | if (avctx->ch_layout.nb_channels == 1 && | |
| 845 | 14 | av_get_planar_sample_fmt(avctx->sample_fmt) == | |
| 846 | 14 | av_get_planar_sample_fmt(sample_fmts[i])) { | |
| 847 | ✗ | avctx->sample_fmt = sample_fmts[i]; | |
| 848 | ✗ | break; | |
| 849 | } | ||
| 850 | } | ||
| 851 | 1713 | if (i == num_sample_fmts) { | |
| 852 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 853 | "Specified sample format %s is not supported by the %s encoder\n", | ||
| 854 | ✗ | av_get_sample_fmt_name(avctx->sample_fmt), c->name); | |
| 855 | |||
| 856 | ✗ | av_log(avctx, AV_LOG_ERROR, "Supported sample formats:\n"); | |
| 857 | ✗ | for (int p = 0; sample_fmts[p] != AV_SAMPLE_FMT_NONE; p++) { | |
| 858 | ✗ | av_log(avctx, AV_LOG_ERROR, " %s\n", | |
| 859 | ✗ | av_get_sample_fmt_name(sample_fmts[p])); | |
| 860 | } | ||
| 861 | |||
| 862 | ✗ | return AVERROR(EINVAL); | |
| 863 | } | ||
| 864 | } | ||
| 865 | |||
| 866 | 1713 | ret = avcodec_get_supported_config(avctx, NULL, AV_CODEC_CONFIG_SAMPLE_RATE, | |
| 867 | 0, (const void **) &supported_samplerates, | ||
| 868 | &num_samplerates); | ||
| 869 | 1713 | if (ret < 0) | |
| 870 | ✗ | return ret; | |
| 871 | 1713 | if (supported_samplerates) { | |
| 872 | 930 | for (i = 0; i < num_samplerates; i++) | |
| 873 | 930 | if (avctx->sample_rate == supported_samplerates[i]) | |
| 874 | 227 | break; | |
| 875 | 227 | if (i == num_samplerates) { | |
| 876 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 877 | "Specified sample rate %d is not supported by the %s encoder\n", | ||
| 878 | ✗ | avctx->sample_rate, c->name); | |
| 879 | |||
| 880 | ✗ | av_log(avctx, AV_LOG_ERROR, "Supported sample rates:\n"); | |
| 881 | ✗ | for (int p = 0; supported_samplerates[p]; p++) | |
| 882 | ✗ | av_log(avctx, AV_LOG_ERROR, " %d\n", supported_samplerates[p]); | |
| 883 | |||
| 884 | ✗ | return AVERROR(EINVAL); | |
| 885 | } | ||
| 886 | } | ||
| 887 | 1713 | ret = avcodec_get_supported_config(avctx, NULL, AV_CODEC_CONFIG_CHANNEL_LAYOUT, | |
| 888 | 0, (const void **) &ch_layouts, &num_ch_layouts); | ||
| 889 | 1713 | if (ret < 0) | |
| 890 | ✗ | return ret; | |
| 891 | 1713 | if (ch_layouts) { | |
| 892 | 350 | for (i = 0; i < num_ch_layouts; i++) { | |
| 893 | 350 | if (!av_channel_layout_compare(&avctx->ch_layout, &ch_layouts[i])) | |
| 894 | 229 | break; | |
| 895 | } | ||
| 896 | 229 | if (i == num_ch_layouts) { | |
| 897 | char buf[512]; | ||
| 898 | ✗ | int ret = av_channel_layout_describe(&avctx->ch_layout, buf, sizeof(buf)); | |
| 899 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 900 | "Specified channel layout '%s' is not supported by the %s encoder\n", | ||
| 901 | ✗ | ret > 0 ? buf : "?", c->name); | |
| 902 | |||
| 903 | ✗ | av_log(avctx, AV_LOG_ERROR, "Supported channel layouts:\n"); | |
| 904 | ✗ | for (int p = 0; ch_layouts[p].nb_channels; p++) { | |
| 905 | ✗ | ret = av_channel_layout_describe(&ch_layouts[p], buf, sizeof(buf)); | |
| 906 | ✗ | av_log(avctx, AV_LOG_ERROR, " %s\n", ret > 0 ? buf : "?"); | |
| 907 | } | ||
| 908 | ✗ | return AVERROR(EINVAL); | |
| 909 | } | ||
| 910 | } | ||
| 911 | |||
| 912 | 1713 | if (!avctx->bits_per_raw_sample) | |
| 913 | 1642 | avctx->bits_per_raw_sample = av_get_exact_bits_per_sample(avctx->codec_id); | |
| 914 | 1713 | if (!avctx->bits_per_raw_sample) | |
| 915 | 370 | avctx->bits_per_raw_sample = 8 * av_get_bytes_per_sample(avctx->sample_fmt); | |
| 916 | |||
| 917 | 1713 | return 0; | |
| 918 | } | ||
| 919 | |||
| 920 | 23104 | int ff_encode_preinit(AVCodecContext *avctx) | |
| 921 | { | ||
| 922 | 23104 | AVCodecInternal *avci = avctx->internal; | |
| 923 | 23104 | EncodeContext *ec = encode_ctx(avci); | |
| 924 | 23104 | int ret = 0; | |
| 925 | |||
| 926 | 23104 | if (avctx->time_base.num <= 0 || avctx->time_base.den <= 0) { | |
| 927 | ✗ | av_log(avctx, AV_LOG_ERROR, "The encoder timebase is not set.\n"); | |
| 928 | ✗ | return AVERROR(EINVAL); | |
| 929 | } | ||
| 930 | |||
| 931 | 23104 | if (avctx->bit_rate < 0) { | |
| 932 | ✗ | av_log(avctx, AV_LOG_ERROR, "The encoder bitrate is negative.\n"); | |
| 933 | ✗ | return AVERROR(EINVAL); | |
| 934 | } | ||
| 935 | |||
| 936 | 23104 | if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE && | |
| 937 | 22965 | !(avctx->codec->capabilities & AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE)) { | |
| 938 | ✗ | av_log(avctx, AV_LOG_ERROR, "The copy_opaque flag is set, but the " | |
| 939 | "encoder does not support it.\n"); | ||
| 940 | ✗ | return AVERROR(EINVAL); | |
| 941 | } | ||
| 942 | |||
| 943 | 23104 | switch (avctx->codec_type) { | |
| 944 | 21344 | case AVMEDIA_TYPE_VIDEO: ret = encode_preinit_video(avctx); break; | |
| 945 | 1713 | case AVMEDIA_TYPE_AUDIO: ret = encode_preinit_audio(avctx); break; | |
| 946 | } | ||
| 947 | 23104 | if (ret < 0) | |
| 948 | ✗ | return ret; | |
| 949 | |||
| 950 | 23104 | if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO) | |
| 951 | 23057 | && avctx->bit_rate>0 && avctx->bit_rate<1000) { | |
| 952 | 1 | av_log(avctx, AV_LOG_WARNING, "Bitrate %"PRId64" is extremely low, maybe you mean %"PRId64"k\n", avctx->bit_rate, avctx->bit_rate); | |
| 953 | } | ||
| 954 | |||
| 955 | 23104 | if (!avctx->rc_initial_buffer_occupancy) | |
| 956 | 23102 | avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3LL / 4; | |
| 957 | |||
| 958 | 23104 | if (avctx->codec_descriptor->props & AV_CODEC_PROP_INTRA_ONLY) | |
| 959 | 22616 | ec->intra_only_flag = AV_PKT_FLAG_KEY; | |
| 960 | |||
| 961 | 23104 | if (ffcodec(avctx->codec)->cb_type == FF_CODEC_CB_TYPE_ENCODE) { | |
| 962 | 23057 | avci->in_frame = av_frame_alloc(); | |
| 963 | 23057 | if (!avci->in_frame) | |
| 964 | ✗ | return AVERROR(ENOMEM); | |
| 965 | } | ||
| 966 | |||
| 967 | 23104 | if ((avctx->flags & AV_CODEC_FLAG_RECON_FRAME)) { | |
| 968 | 6 | if (!(avctx->codec->capabilities & AV_CODEC_CAP_ENCODER_RECON_FRAME)) { | |
| 969 | ✗ | av_log(avctx, AV_LOG_ERROR, "Reconstructed frame output requested " | |
| 970 | "from an encoder not supporting it\n"); | ||
| 971 | ✗ | return AVERROR(ENOSYS); | |
| 972 | } | ||
| 973 | |||
| 974 | 6 | avci->recon_frame = av_frame_alloc(); | |
| 975 | 6 | if (!avci->recon_frame) | |
| 976 | ✗ | return AVERROR(ENOMEM); | |
| 977 | } | ||
| 978 | |||
| 979 | 300352 | for (int i = 0; ff_sd_global_map[i].packet < AV_PKT_DATA_NB; i++) { | |
| 980 | 277248 | const enum AVPacketSideDataType type_packet = ff_sd_global_map[i].packet; | |
| 981 | 277248 | const enum AVFrameSideDataType type_frame = ff_sd_global_map[i].frame; | |
| 982 | const AVFrameSideData *sd_frame; | ||
| 983 | AVPacketSideData *sd_packet; | ||
| 984 | |||
| 985 | 277248 | sd_frame = av_frame_side_data_get(avctx->decoded_side_data, | |
| 986 | 277248 | avctx->nb_decoded_side_data, | |
| 987 | type_frame); | ||
| 988 | 277529 | if (!sd_frame || | |
| 989 | 281 | av_packet_side_data_get(avctx->coded_side_data, avctx->nb_coded_side_data, | |
| 990 | type_packet)) | ||
| 991 | |||
| 992 | 276967 | continue; | |
| 993 | |||
| 994 | 281 | sd_packet = av_packet_side_data_new(&avctx->coded_side_data, &avctx->nb_coded_side_data, | |
| 995 | 281 | type_packet, sd_frame->size, 0); | |
| 996 | 281 | if (!sd_packet) | |
| 997 | ✗ | return AVERROR(ENOMEM); | |
| 998 | |||
| 999 | 281 | memcpy(sd_packet->data, sd_frame->data, sd_frame->size); | |
| 1000 | } | ||
| 1001 | |||
| 1002 | #if CONFIG_FRAME_THREAD_ENCODER | ||
| 1003 | 23104 | ret = ff_frame_thread_encoder_init(avctx); | |
| 1004 | 23104 | if (ret < 0) | |
| 1005 | ✗ | return ret; | |
| 1006 | #endif | ||
| 1007 | |||
| 1008 | 23104 | return 0; | |
| 1009 | } | ||
| 1010 | |||
| 1011 | 19042 | int ff_encode_alloc_frame(AVCodecContext *avctx, AVFrame *frame) | |
| 1012 | { | ||
| 1013 | int ret; | ||
| 1014 | |||
| 1015 | av_assert1(avctx->codec_type == AVMEDIA_TYPE_VIDEO); | ||
| 1016 | |||
| 1017 | 19042 | frame->format = avctx->pix_fmt; | |
| 1018 | 19042 | if (frame->width <= 0 || frame->height <= 0) { | |
| 1019 | 16 | frame->width = avctx->width; | |
| 1020 | 16 | frame->height = avctx->height; | |
| 1021 | } | ||
| 1022 | |||
| 1023 | 19042 | ret = avcodec_default_get_buffer2(avctx, frame, 0); | |
| 1024 | 19042 | if (ret < 0) { | |
| 1025 | ✗ | av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); | |
| 1026 | ✗ | av_frame_unref(frame); | |
| 1027 | ✗ | return ret; | |
| 1028 | } | ||
| 1029 | |||
| 1030 | 19042 | return 0; | |
| 1031 | } | ||
| 1032 | |||
| 1033 | 104 | int ff_encode_receive_frame(AVCodecContext *avctx, AVFrame *frame) | |
| 1034 | { | ||
| 1035 | 104 | AVCodecInternal *avci = avctx->internal; | |
| 1036 | |||
| 1037 | 104 | if (!avci->recon_frame) | |
| 1038 | ✗ | return AVERROR(EINVAL); | |
| 1039 | 104 | if (!avci->recon_frame->buf[0]) | |
| 1040 | ✗ | return avci->draining_done ? AVERROR_EOF : AVERROR(EAGAIN); | |
| 1041 | |||
| 1042 | 104 | av_frame_move_ref(frame, avci->recon_frame); | |
| 1043 | 104 | return 0; | |
| 1044 | } | ||
| 1045 | |||
| 1046 | ✗ | void ff_encode_flush_buffers(AVCodecContext *avctx) | |
| 1047 | { | ||
| 1048 | ✗ | AVCodecInternal *avci = avctx->internal; | |
| 1049 | |||
| 1050 | ✗ | if (avci->in_frame) | |
| 1051 | ✗ | av_frame_unref(avci->in_frame); | |
| 1052 | ✗ | if (avci->recon_frame) | |
| 1053 | ✗ | av_frame_unref(avci->recon_frame); | |
| 1054 | ✗ | } | |
| 1055 | |||
| 1056 | 23104 | AVCodecInternal *ff_encode_internal_alloc(void) | |
| 1057 | { | ||
| 1058 | 23104 | return av_mallocz(sizeof(EncodeContext)); | |
| 1059 | } | ||
| 1060 | |||
| 1061 | 253 | AVCPBProperties *ff_encode_add_cpb_side_data(AVCodecContext *avctx) | |
| 1062 | { | ||
| 1063 | AVPacketSideData *tmp; | ||
| 1064 | AVCPBProperties *props; | ||
| 1065 | size_t size; | ||
| 1066 | int i; | ||
| 1067 | |||
| 1068 | 274 | for (i = 0; i < avctx->nb_coded_side_data; i++) | |
| 1069 | 21 | if (avctx->coded_side_data[i].type == AV_PKT_DATA_CPB_PROPERTIES) | |
| 1070 | ✗ | return (AVCPBProperties *)avctx->coded_side_data[i].data; | |
| 1071 | |||
| 1072 | 253 | props = av_cpb_properties_alloc(&size); | |
| 1073 | 253 | if (!props) | |
| 1074 | ✗ | return NULL; | |
| 1075 | |||
| 1076 | 253 | tmp = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*tmp)); | |
| 1077 | 253 | if (!tmp) { | |
| 1078 | ✗ | av_freep(&props); | |
| 1079 | ✗ | return NULL; | |
| 1080 | } | ||
| 1081 | |||
| 1082 | 253 | avctx->coded_side_data = tmp; | |
| 1083 | 253 | avctx->nb_coded_side_data++; | |
| 1084 | |||
| 1085 | 253 | avctx->coded_side_data[avctx->nb_coded_side_data - 1].type = AV_PKT_DATA_CPB_PROPERTIES; | |
| 1086 | 253 | avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props; | |
| 1087 | 253 | avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size; | |
| 1088 | |||
| 1089 | 253 | return props; | |
| 1090 | } | ||
| 1091 | |||
| 1092 | 19486 | int ff_encode_add_stats_side_data(AVPacket *pkt, int quality, const int64_t error[], | |
| 1093 | int error_count, enum AVPictureType pict_type) | ||
| 1094 | { | ||
| 1095 | uint8_t *side_data; | ||
| 1096 | size_t side_data_size; | ||
| 1097 | |||
| 1098 | 19486 | side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS, &side_data_size); | |
| 1099 | 19486 | if (!side_data) { | |
| 1100 | 19486 | side_data_size = 4+4+8*error_count; | |
| 1101 | 19486 | side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_QUALITY_STATS, | |
| 1102 | side_data_size); | ||
| 1103 | } | ||
| 1104 | |||
| 1105 | 19486 | if (!side_data || side_data_size < 4+4+8*error_count) | |
| 1106 | ✗ | return AVERROR(ENOMEM); | |
| 1107 | |||
| 1108 | 19486 | AV_WL32(side_data, quality); | |
| 1109 | 19486 | side_data[4] = pict_type; | |
| 1110 | 19486 | side_data[5] = error_count; | |
| 1111 | 19486 | for (int i = 0; i < error_count; ++i) | |
| 1112 | ✗ | AV_WL64(side_data+8 + 8*i , error[i]); | |
| 1113 | |||
| 1114 | 19486 | return 0; | |
| 1115 | } | ||
| 1116 | |||
| 1117 | 1858 | int ff_check_codec_matrices(AVCodecContext *avctx, unsigned types, uint16_t min, uint16_t max) | |
| 1118 | { | ||
| 1119 | 1858 | uint16_t *matrices[] = {avctx->intra_matrix, avctx->inter_matrix, avctx->chroma_intra_matrix}; | |
| 1120 | 1858 | const char *names[] = {"Intra", "Inter", "Chroma Intra"}; | |
| 1121 | static_assert(FF_ARRAY_ELEMS(matrices) == FF_ARRAY_ELEMS(names), "matrix count mismatch"); | ||
| 1122 | 7432 | for (int m = 0; m < FF_ARRAY_ELEMS(matrices); m++) { | |
| 1123 | 5574 | uint16_t *matrix = matrices[m]; | |
| 1124 | 5574 | if (matrix && (types & (1U << m))) { | |
| 1125 | ✗ | for (int i = 0; i < 64; i++) { | |
| 1126 | ✗ | if (matrix[i] < min || matrix[i] > max) { | |
| 1127 | ✗ | 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); | |
| 1128 | ✗ | return AVERROR(EINVAL); | |
| 1129 | } | ||
| 1130 | } | ||
| 1131 | } | ||
| 1132 | } | ||
| 1133 | 1858 | return 0; | |
| 1134 | } | ||
| 1135 |