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