libavcodec/avcodec.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * AVCodecContext functions for libavcodec | ||
| 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 | /** | ||
| 22 | * @file | ||
| 23 | * AVCodecContext functions for libavcodec | ||
| 24 | */ | ||
| 25 | |||
| 26 | #include <assert.h> | ||
| 27 | |||
| 28 | #include "config.h" | ||
| 29 | #include "libavutil/avassert.h" | ||
| 30 | #include "libavutil/avstring.h" | ||
| 31 | #include "libavutil/bprint.h" | ||
| 32 | #include "libavutil/channel_layout.h" | ||
| 33 | #include "libavutil/common.h" | ||
| 34 | #include "libavutil/imgutils.h" | ||
| 35 | #include "libavutil/mem.h" | ||
| 36 | #include "libavutil/opt.h" | ||
| 37 | #include "libavutil/thread.h" | ||
| 38 | #include "avcodec.h" | ||
| 39 | #include "avcodec_internal.h" | ||
| 40 | #include "bsf.h" | ||
| 41 | #include "codec_desc.h" | ||
| 42 | #include "codec_internal.h" | ||
| 43 | #include "decode.h" | ||
| 44 | #include "frame_thread_encoder.h" | ||
| 45 | #include "hwconfig.h" | ||
| 46 | #include "internal.h" | ||
| 47 | #include "libavutil/refstruct.h" | ||
| 48 | |||
| 49 | /** | ||
| 50 | * Maximum size in bytes of extradata. | ||
| 51 | * This value was chosen such that every bit of the buffer is | ||
| 52 | * addressable by a 32-bit signed integer as used by get_bits. | ||
| 53 | */ | ||
| 54 | #define FF_MAX_EXTRADATA_SIZE ((1 << 28) - AV_INPUT_BUFFER_PADDING_SIZE) | ||
| 55 | |||
| 56 | const SideDataMap ff_sd_global_map[] = { | ||
| 57 | { AV_PKT_DATA_REPLAYGAIN , AV_FRAME_DATA_REPLAYGAIN }, | ||
| 58 | { AV_PKT_DATA_DISPLAYMATRIX, AV_FRAME_DATA_DISPLAYMATRIX }, | ||
| 59 | { AV_PKT_DATA_SPHERICAL, AV_FRAME_DATA_SPHERICAL }, | ||
| 60 | { AV_PKT_DATA_STEREO3D, AV_FRAME_DATA_STEREO3D }, | ||
| 61 | { AV_PKT_DATA_AUDIO_SERVICE_TYPE, AV_FRAME_DATA_AUDIO_SERVICE_TYPE }, | ||
| 62 | { AV_PKT_DATA_MASTERING_DISPLAY_METADATA, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA }, | ||
| 63 | { AV_PKT_DATA_CONTENT_LIGHT_LEVEL, AV_FRAME_DATA_CONTENT_LIGHT_LEVEL }, | ||
| 64 | { AV_PKT_DATA_ICC_PROFILE, AV_FRAME_DATA_ICC_PROFILE }, | ||
| 65 | { AV_PKT_DATA_AMBIENT_VIEWING_ENVIRONMENT,AV_FRAME_DATA_AMBIENT_VIEWING_ENVIRONMENT }, | ||
| 66 | { AV_PKT_DATA_3D_REFERENCE_DISPLAYS, AV_FRAME_DATA_3D_REFERENCE_DISPLAYS }, | ||
| 67 | { AV_PKT_DATA_EXIF, AV_FRAME_DATA_EXIF }, | ||
| 68 | { AV_PKT_DATA_GAIN_MAP_PARAMS, AV_FRAME_DATA_GAIN_MAP_PARAMS }, | ||
| 69 | { AV_PKT_DATA_NB }, | ||
| 70 | }; | ||
| 71 | |||
| 72 | |||
| 73 | 28942 | int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size) | |
| 74 | { | ||
| 75 | size_t i; | ||
| 76 | |||
| 77 | 1420904 | for (i = 0; i < count; i++) { | |
| 78 | 1391962 | size_t offset = i * size; | |
| 79 | 1391962 | int r = func(c, FF_PTR_ADD((char *)arg, offset)); | |
| 80 | 1391962 | if (ret) | |
| 81 | 930 | ret[i] = r; | |
| 82 | } | ||
| 83 | 28942 | return 0; | |
| 84 | } | ||
| 85 | |||
| 86 | 8681 | int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count) | |
| 87 | { | ||
| 88 | int i; | ||
| 89 | |||
| 90 | 241480 | for (i = 0; i < count; i++) { | |
| 91 | 232799 | int r = func(c, arg, i, 0); | |
| 92 | 232799 | if (ret) | |
| 93 | 50 | ret[i] = r; | |
| 94 | } | ||
| 95 | 8681 | return 0; | |
| 96 | } | ||
| 97 | |||
| 98 | static AVMutex codec_mutex = AV_MUTEX_INITIALIZER; | ||
| 99 | |||
| 100 | 73551 | static void lock_avcodec(const FFCodec *codec) | |
| 101 | { | ||
| 102 | 73551 | if (codec->caps_internal & FF_CODEC_CAP_NOT_INIT_THREADSAFE && codec->init) | |
| 103 | ✗ | ff_mutex_lock(&codec_mutex); | |
| 104 | 73551 | } | |
| 105 | |||
| 106 | 73551 | static void unlock_avcodec(const FFCodec *codec) | |
| 107 | { | ||
| 108 | 73551 | if (codec->caps_internal & FF_CODEC_CAP_NOT_INIT_THREADSAFE && codec->init) | |
| 109 | ✗ | ff_mutex_unlock(&codec_mutex); | |
| 110 | 73551 | } | |
| 111 | |||
| 112 | 32230 | static int64_t get_bit_rate(AVCodecContext *ctx) | |
| 113 | { | ||
| 114 | int64_t bit_rate; | ||
| 115 | int bits_per_sample; | ||
| 116 | |||
| 117 | 32230 | switch (ctx->codec_type) { | |
| 118 | 25330 | case AVMEDIA_TYPE_VIDEO: | |
| 119 | case AVMEDIA_TYPE_DATA: | ||
| 120 | case AVMEDIA_TYPE_SUBTITLE: | ||
| 121 | case AVMEDIA_TYPE_ATTACHMENT: | ||
| 122 | 25330 | bit_rate = ctx->bit_rate; | |
| 123 | 25330 | break; | |
| 124 | 6900 | case AVMEDIA_TYPE_AUDIO: | |
| 125 | 6900 | bits_per_sample = av_get_bits_per_sample(ctx->codec_id); | |
| 126 | 6900 | if (bits_per_sample) { | |
| 127 | 3078 | bit_rate = ctx->sample_rate * (int64_t)ctx->ch_layout.nb_channels; | |
| 128 | 3078 | if (bit_rate > INT64_MAX / bits_per_sample) { | |
| 129 | ✗ | bit_rate = 0; | |
| 130 | } else | ||
| 131 | 3078 | bit_rate *= bits_per_sample; | |
| 132 | } else | ||
| 133 | 3822 | bit_rate = ctx->bit_rate; | |
| 134 | 6900 | break; | |
| 135 | ✗ | default: | |
| 136 | ✗ | bit_rate = 0; | |
| 137 | ✗ | break; | |
| 138 | } | ||
| 139 | 32230 | return bit_rate; | |
| 140 | } | ||
| 141 | |||
| 142 | 41091 | int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options) | |
| 143 | { | ||
| 144 | 41091 | int ret = 0; | |
| 145 | AVCodecInternal *avci; | ||
| 146 | const FFCodec *codec2; | ||
| 147 | const AVDictionaryEntry *e; | ||
| 148 | |||
| 149 | 41091 | if (avcodec_is_open(avctx)) | |
| 150 | ✗ | return 0; | |
| 151 | |||
| 152 | 41091 | if (!codec && !avctx->codec) { | |
| 153 | ✗ | av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n"); | |
| 154 | ✗ | return AVERROR(EINVAL); | |
| 155 | } | ||
| 156 | 41091 | if (codec && avctx->codec && codec != avctx->codec) { | |
| 157 | ✗ | av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, " | |
| 158 | ✗ | "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name); | |
| 159 | ✗ | return AVERROR(EINVAL); | |
| 160 | } | ||
| 161 | 41091 | if (!codec) | |
| 162 | 49 | codec = avctx->codec; | |
| 163 | 41091 | codec2 = ffcodec(codec); | |
| 164 | |||
| 165 | 41091 | if ((avctx->codec_type != AVMEDIA_TYPE_UNKNOWN && avctx->codec_type != codec->type) || | |
| 166 | 41091 | (avctx->codec_id != AV_CODEC_ID_NONE && avctx->codec_id != codec->id)) { | |
| 167 | ✗ | av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n"); | |
| 168 | ✗ | return AVERROR(EINVAL); | |
| 169 | } | ||
| 170 | |||
| 171 | 41091 | avctx->codec_type = codec->type; | |
| 172 | 41091 | avctx->codec_id = codec->id; | |
| 173 | 41091 | avctx->codec = codec; | |
| 174 | |||
| 175 | 41091 | if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE) | |
| 176 | ✗ | return AVERROR(EINVAL); | |
| 177 | |||
| 178 | // set the whitelist from provided options dict, | ||
| 179 | // so we can check it immediately | ||
| 180 | 41091 | e = options ? av_dict_get(*options, "codec_whitelist", NULL, 0) : NULL; | |
| 181 | 41091 | if (e) { | |
| 182 | ✗ | ret = av_opt_set(avctx, e->key, e->value, 0); | |
| 183 | ✗ | if (ret < 0) | |
| 184 | ✗ | return ret; | |
| 185 | } | ||
| 186 | |||
| 187 | 41091 | if (avctx->codec_whitelist && av_match_list(codec->name, avctx->codec_whitelist, ',') <= 0) { | |
| 188 | ✗ | av_log(avctx, AV_LOG_ERROR, "Codec (%s) not on whitelist \'%s\'\n", codec->name, avctx->codec_whitelist); | |
| 189 | ✗ | return AVERROR(EINVAL); | |
| 190 | } | ||
| 191 | |||
| 192 | 41091 | avci = ff_codec_is_decoder(codec) ? | |
| 193 | 17975 | ff_decode_internal_alloc() : | |
| 194 | 23116 | ff_encode_internal_alloc(); | |
| 195 | 41091 | if (!avci) { | |
| 196 | ✗ | ret = AVERROR(ENOMEM); | |
| 197 | ✗ | goto end; | |
| 198 | } | ||
| 199 | 41091 | avctx->internal = avci; | |
| 200 | |||
| 201 | 41091 | avci->buffer_frame = av_frame_alloc(); | |
| 202 | 41091 | avci->buffer_pkt = av_packet_alloc(); | |
| 203 | 41091 | if (!avci->buffer_frame || !avci->buffer_pkt) { | |
| 204 | ✗ | ret = AVERROR(ENOMEM); | |
| 205 | ✗ | goto free_and_end; | |
| 206 | } | ||
| 207 | |||
| 208 | 41091 | if (codec2->priv_data_size > 0) { | |
| 209 | 18867 | if (!avctx->priv_data) { | |
| 210 | 9607 | avctx->priv_data = av_mallocz(codec2->priv_data_size); | |
| 211 | 9607 | if (!avctx->priv_data) { | |
| 212 | ✗ | ret = AVERROR(ENOMEM); | |
| 213 | ✗ | goto free_and_end; | |
| 214 | } | ||
| 215 | 9607 | if (codec->priv_class) { | |
| 216 | 3044 | *(const AVClass **)avctx->priv_data = codec->priv_class; | |
| 217 | 3044 | av_opt_set_defaults(avctx->priv_data); | |
| 218 | } | ||
| 219 | } | ||
| 220 | } else { | ||
| 221 | 22224 | avctx->priv_data = NULL; | |
| 222 | } | ||
| 223 | |||
| 224 | 41091 | ret = av_opt_set_dict2(avctx, options, AV_OPT_SEARCH_CHILDREN); | |
| 225 | 41091 | if (ret < 0) | |
| 226 | ✗ | goto free_and_end; | |
| 227 | |||
| 228 | // only call ff_set_dimensions() for non H.264/VP6F/DXV codecs so as not to overwrite previously setup dimensions | ||
| 229 | 41091 | if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && | |
| 230 | 6 | (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F || avctx->codec_id == AV_CODEC_ID_DXV))) { | |
| 231 | 41091 | if (avctx->coded_width && avctx->coded_height) | |
| 232 | 41 | ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height); | |
| 233 | 41050 | else if (avctx->width && avctx->height) | |
| 234 | 29960 | ret = ff_set_dimensions(avctx, avctx->width, avctx->height); | |
| 235 | 41091 | if (ret < 0) | |
| 236 | ✗ | goto free_and_end; | |
| 237 | } | ||
| 238 | |||
| 239 | 41091 | if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height) | |
| 240 | 30001 | && ( av_image_check_size2(avctx->coded_width, avctx->coded_height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0 | |
| 241 | 30001 | || av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0)) { | |
| 242 | ✗ | av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n"); | |
| 243 | ✗ | ff_set_dimensions(avctx, 0, 0); | |
| 244 | } | ||
| 245 | |||
| 246 | 41091 | if (avctx->width > 0 && avctx->height > 0) { | |
| 247 | 30001 | if (av_image_check_sar(avctx->width, avctx->height, | |
| 248 | avctx->sample_aspect_ratio) < 0) { | ||
| 249 | 2 | av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n", | |
| 250 | avctx->sample_aspect_ratio.num, | ||
| 251 | avctx->sample_aspect_ratio.den); | ||
| 252 | 2 | avctx->sample_aspect_ratio = (AVRational){ 0, 1 }; | |
| 253 | } | ||
| 254 | } | ||
| 255 | |||
| 256 | /* AV_CODEC_CAP_CHANNEL_CONF is a decoder-only flag; so the code below | ||
| 257 | * in particular checks that sample_rate is set for all audio encoders. */ | ||
| 258 | 41091 | if (avctx->sample_rate < 0 || | |
| 259 | 41091 | avctx->sample_rate == 0 && avctx->codec_type == AVMEDIA_TYPE_AUDIO && | |
| 260 | 421 | !(codec->capabilities & AV_CODEC_CAP_CHANNEL_CONF)) { | |
| 261 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid sample rate: %d\n", avctx->sample_rate); | |
| 262 | ✗ | ret = AVERROR(EINVAL); | |
| 263 | ✗ | goto free_and_end; | |
| 264 | } | ||
| 265 | 41091 | if (avctx->block_align < 0) { | |
| 266 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid block align: %d\n", avctx->block_align); | |
| 267 | ✗ | ret = AVERROR(EINVAL); | |
| 268 | ✗ | goto free_and_end; | |
| 269 | } | ||
| 270 | |||
| 271 | /* AV_CODEC_CAP_CHANNEL_CONF is a decoder-only flag; so the code below | ||
| 272 | * in particular checks that nb_channels is set for all audio encoders. */ | ||
| 273 | 41091 | if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && !avctx->ch_layout.nb_channels | |
| 274 | 424 | && !(codec->capabilities & AV_CODEC_CAP_CHANNEL_CONF)) { | |
| 275 | 1 | av_log(avctx, AV_LOG_ERROR, "%s requires channel layout to be set\n", | |
| 276 | 1 | ff_codec_is_decoder(codec) ? "Decoder" : "Encoder"); | |
| 277 | 1 | ret = AVERROR(EINVAL); | |
| 278 | 1 | goto free_and_end; | |
| 279 | } | ||
| 280 | 41090 | if (avctx->ch_layout.nb_channels && !av_channel_layout_check(&avctx->ch_layout)) { | |
| 281 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid channel layout\n"); | |
| 282 | ✗ | ret = AVERROR(EINVAL); | |
| 283 | ✗ | goto free_and_end; | |
| 284 | } | ||
| 285 | 41090 | if (avctx->ch_layout.nb_channels > FF_SANE_NB_CHANNELS) { | |
| 286 | ✗ | av_log(avctx, AV_LOG_ERROR, "Too many channels: %d\n", avctx->ch_layout.nb_channels); | |
| 287 | ✗ | ret = AVERROR(EINVAL); | |
| 288 | ✗ | goto free_and_end; | |
| 289 | } | ||
| 290 | |||
| 291 | 41090 | avctx->frame_num = 0; | |
| 292 | 41090 | avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id); | |
| 293 | |||
| 294 | 41090 | if ((avctx->codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) && | |
| 295 | 17 | avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) { | |
| 296 | ✗ | const char *codec_string = ff_codec_is_encoder(codec) ? "encoder" : "decoder"; | |
| 297 | const AVCodec *codec2; | ||
| 298 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 299 | "The %s '%s' is experimental but experimental codecs are not enabled, " | ||
| 300 | "add '-strict %d' if you want to use it.\n", | ||
| 301 | ✗ | codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL); | |
| 302 | ✗ | codec2 = ff_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id); | |
| 303 | ✗ | if (!(codec2->capabilities & AV_CODEC_CAP_EXPERIMENTAL)) | |
| 304 | ✗ | av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n", | |
| 305 | ✗ | codec_string, codec2->name); | |
| 306 | ✗ | ret = AVERROR_EXPERIMENTAL; | |
| 307 | ✗ | goto free_and_end; | |
| 308 | } | ||
| 309 | |||
| 310 | 41090 | if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && | |
| 311 | 6385 | (!avctx->time_base.num || !avctx->time_base.den)) { | |
| 312 | 4687 | avctx->time_base.num = 1; | |
| 313 | 4687 | avctx->time_base.den = avctx->sample_rate; | |
| 314 | } | ||
| 315 | |||
| 316 | 41090 | if (ff_codec_is_encoder(avctx->codec)) | |
| 317 | 23116 | ret = ff_encode_preinit(avctx); | |
| 318 | else | ||
| 319 | 17974 | ret = ff_decode_preinit(avctx); | |
| 320 | 41090 | if (ret < 0) | |
| 321 | ✗ | goto free_and_end; | |
| 322 | |||
| 323 | 41090 | if (HAVE_THREADS && !avci->frame_thread_encoder) { | |
| 324 | /* Frame-threaded decoders call FFCodec.init for their child contexts. */ | ||
| 325 | 39309 | lock_avcodec(codec2); | |
| 326 | 39309 | ret = ff_thread_init(avctx); | |
| 327 | 39309 | unlock_avcodec(codec2); | |
| 328 | 39309 | if (ret < 0) { | |
| 329 | ✗ | goto free_and_end; | |
| 330 | } | ||
| 331 | } | ||
| 332 | if (!HAVE_THREADS && !(codec2->caps_internal & FF_CODEC_CAP_AUTO_THREADS)) | ||
| 333 | avctx->thread_count = 1; | ||
| 334 | |||
| 335 | 41090 | if (!(avctx->active_thread_type & FF_THREAD_FRAME) || | |
| 336 | 1797 | avci->frame_thread_encoder) { | |
| 337 | 41074 | if (codec2->init) { | |
| 338 | 34242 | lock_avcodec(codec2); | |
| 339 | 34242 | ret = codec2->init(avctx); | |
| 340 | 34242 | unlock_avcodec(codec2); | |
| 341 | 34242 | if (ret < 0) { | |
| 342 | 9 | avci->needs_close = codec2->caps_internal & FF_CODEC_CAP_INIT_CLEANUP; | |
| 343 | 9 | goto free_and_end; | |
| 344 | } | ||
| 345 | } | ||
| 346 | 41065 | avci->needs_close = 1; | |
| 347 | } | ||
| 348 | |||
| 349 | 41081 | ret=0; | |
| 350 | |||
| 351 | 41081 | if (ff_codec_is_decoder(avctx->codec)) { | |
| 352 | 17965 | if (!avctx->bit_rate) | |
| 353 | 12429 | avctx->bit_rate = get_bit_rate(avctx); | |
| 354 | |||
| 355 | |||
| 356 | 17965 | if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && | |
| 357 | 4668 | !avctx->frame_size && (avctx->flags2 & AV_CODEC_FLAG2_FIXED_FRAME_SIZE)) { | |
| 358 | ✗ | av_log(avctx, AV_LOG_ERROR, "Fixed frame size requested but no frame_size value set\n"); | |
| 359 | ✗ | ret = AVERROR(EINVAL); | |
| 360 | ✗ | goto free_and_end; | |
| 361 | } | ||
| 362 | |||
| 363 | 17965 | avci->skip_samples = avctx->delay; | |
| 364 | |||
| 365 | /* validate channel layout from the decoder */ | ||
| 366 | 17965 | if ((avctx->ch_layout.nb_channels && !av_channel_layout_check(&avctx->ch_layout)) || | |
| 367 | 17965 | avctx->ch_layout.nb_channels > FF_SANE_NB_CHANNELS) { | |
| 368 | ✗ | ret = AVERROR(EINVAL); | |
| 369 | ✗ | goto free_and_end; | |
| 370 | } | ||
| 371 | 17965 | if (avctx->bits_per_coded_sample < 0) { | |
| 372 | ✗ | ret = AVERROR(EINVAL); | |
| 373 | ✗ | goto free_and_end; | |
| 374 | } | ||
| 375 | } | ||
| 376 | 41081 | if (codec->priv_class) | |
| 377 | 6400 | av_assert0(*(const AVClass **)avctx->priv_data == codec->priv_class); | |
| 378 | |||
| 379 | 41081 | end: | |
| 380 | |||
| 381 | 41091 | return ret; | |
| 382 | 10 | free_and_end: | |
| 383 | 10 | ff_codec_close(avctx); | |
| 384 | 10 | goto end; | |
| 385 | } | ||
| 386 | |||
| 387 | 41 | void avcodec_flush_buffers(AVCodecContext *avctx) | |
| 388 | { | ||
| 389 | 41 | AVCodecInternal *avci = avctx->internal; | |
| 390 | |||
| 391 | 41 | if (av_codec_is_encoder(avctx->codec)) { | |
| 392 | ✗ | int caps = avctx->codec->capabilities; | |
| 393 | |||
| 394 | ✗ | if (!(caps & AV_CODEC_CAP_ENCODER_FLUSH)) { | |
| 395 | // Only encoders that explicitly declare support for it can be | ||
| 396 | // flushed. Otherwise, this is a no-op. | ||
| 397 | ✗ | av_log(avctx, AV_LOG_WARNING, "Ignoring attempt to flush encoder " | |
| 398 | "that doesn't support it\n"); | ||
| 399 | ✗ | return; | |
| 400 | } | ||
| 401 | ✗ | ff_encode_flush_buffers(avctx); | |
| 402 | } else | ||
| 403 | 41 | ff_decode_flush_buffers(avctx); | |
| 404 | |||
| 405 | 41 | avci->draining = 0; | |
| 406 | 41 | avci->draining_done = 0; | |
| 407 | 41 | if (avci->buffer_frame) | |
| 408 | 41 | av_frame_unref(avci->buffer_frame); | |
| 409 | 41 | if (avci->buffer_pkt) | |
| 410 | 41 | av_packet_unref(avci->buffer_pkt); | |
| 411 | |||
| 412 | 41 | if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME && | |
| 413 | ✗ | !avci->is_frame_mt) | |
| 414 | ✗ | ff_thread_flush(avctx); | |
| 415 | 41 | else if (ffcodec(avctx->codec)->flush) | |
| 416 | 12 | ffcodec(avctx->codec)->flush(avctx); | |
| 417 | } | ||
| 418 | |||
| 419 | 1129 | void avsubtitle_free(AVSubtitle *sub) | |
| 420 | { | ||
| 421 | int i; | ||
| 422 | |||
| 423 | 2224 | for (i = 0; i < sub->num_rects; i++) { | |
| 424 | 1095 | AVSubtitleRect *const rect = sub->rects[i]; | |
| 425 | |||
| 426 | 1095 | av_freep(&rect->data[0]); | |
| 427 | 1095 | av_freep(&rect->data[1]); | |
| 428 | 1095 | av_freep(&rect->data[2]); | |
| 429 | 1095 | av_freep(&rect->data[3]); | |
| 430 | 1095 | av_freep(&rect->text); | |
| 431 | 1095 | av_freep(&rect->ass); | |
| 432 | |||
| 433 | 1095 | av_freep(&sub->rects[i]); | |
| 434 | } | ||
| 435 | |||
| 436 | 1129 | av_freep(&sub->rects); | |
| 437 | |||
| 438 | 1129 | memset(sub, 0, sizeof(*sub)); | |
| 439 | 1129 | } | |
| 440 | |||
| 441 | 72078 | av_cold void ff_codec_close(AVCodecContext *avctx) | |
| 442 | { | ||
| 443 | int i; | ||
| 444 | |||
| 445 | 72078 | if (avcodec_is_open(avctx)) { | |
| 446 | 41091 | AVCodecInternal *avci = avctx->internal; | |
| 447 | |||
| 448 | #if CONFIG_FRAME_THREAD_ENCODER | ||
| 449 | 41091 | if (avci->frame_thread_encoder && avctx->thread_count > 1) { | |
| 450 | 1781 | ff_frame_thread_encoder_free(avctx); | |
| 451 | } | ||
| 452 | #endif | ||
| 453 | 41091 | if (HAVE_THREADS && avci->thread_ctx) | |
| 454 | 65 | ff_thread_free(avctx); | |
| 455 | 41091 | if (avci->needs_close && ffcodec(avctx->codec)->close) | |
| 456 | 8864 | ffcodec(avctx->codec)->close(avctx); | |
| 457 | 41091 | avci->byte_buffer_size = 0; | |
| 458 | 41091 | av_freep(&avci->byte_buffer); | |
| 459 | 41091 | av_frame_free(&avci->buffer_frame); | |
| 460 | 41091 | av_packet_free(&avci->buffer_pkt); | |
| 461 | 41091 | av_packet_free(&avci->last_pkt_props); | |
| 462 | |||
| 463 | 41091 | av_packet_free(&avci->in_pkt); | |
| 464 | 41091 | av_frame_free(&avci->in_frame); | |
| 465 | 41091 | av_frame_free(&avci->recon_frame); | |
| 466 | |||
| 467 | 41091 | av_refstruct_unref(&avci->pool); | |
| 468 | 41091 | av_refstruct_pool_uninit(&avci->progress_frame_pool); | |
| 469 | 41091 | if (av_codec_is_decoder(avctx->codec)) | |
| 470 | 17975 | ff_decode_internal_uninit(avctx); | |
| 471 | |||
| 472 | 41091 | ff_hwaccel_uninit(avctx); | |
| 473 | |||
| 474 | 41091 | av_bsf_free(&avci->bsf); | |
| 475 | |||
| 476 | #if CONFIG_LCMS2 | ||
| 477 | ff_icc_context_uninit(&avci->icc); | ||
| 478 | #endif | ||
| 479 | |||
| 480 | 41091 | av_freep(&avctx->internal); | |
| 481 | } | ||
| 482 | |||
| 483 | 77173 | for (i = 0; i < avctx->nb_coded_side_data; i++) | |
| 484 | 5095 | av_freep(&avctx->coded_side_data[i].data); | |
| 485 | 72078 | av_freep(&avctx->coded_side_data); | |
| 486 | 72078 | avctx->nb_coded_side_data = 0; | |
| 487 | 72078 | av_frame_side_data_free(&avctx->decoded_side_data, | |
| 488 | &avctx->nb_decoded_side_data); | ||
| 489 | |||
| 490 | 72078 | av_buffer_unref(&avctx->hw_frames_ctx); | |
| 491 | 72078 | av_buffer_unref(&avctx->hw_device_ctx); | |
| 492 | |||
| 493 | 72078 | if (avctx->priv_data && avctx->codec && avctx->codec->priv_class) | |
| 494 | 9444 | av_opt_free(avctx->priv_data); | |
| 495 | 72078 | av_opt_free(avctx); | |
| 496 | 72078 | av_freep(&avctx->priv_data); | |
| 497 | 72078 | if (av_codec_is_encoder(avctx->codec)) { | |
| 498 | 23120 | av_freep(&avctx->extradata); | |
| 499 | 23120 | avctx->extradata_size = 0; | |
| 500 | 48958 | } else if (av_codec_is_decoder(avctx->codec)) | |
| 501 | 37358 | av_freep(&avctx->subtitle_header); | |
| 502 | |||
| 503 | 72078 | avctx->codec = NULL; | |
| 504 | 72078 | avctx->active_thread_type = 0; | |
| 505 | 72078 | } | |
| 506 | |||
| 507 | 24546 | static const char *unknown_if_null(const char *str) | |
| 508 | { | ||
| 509 | 24546 | return str ? str : "unknown"; | |
| 510 | } | ||
| 511 | |||
| 512 | 19830 | void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode) | |
| 513 | { | ||
| 514 | const char *codec_type; | ||
| 515 | const char *codec_name; | ||
| 516 | 19830 | const char *profile = NULL; | |
| 517 | AVBPrint bprint; | ||
| 518 | int64_t bitrate; | ||
| 519 | 19830 | int new_line = 0; | |
| 520 | AVRational display_aspect_ratio; | ||
| 521 | 19830 | const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", "; | |
| 522 | const char *str; | ||
| 523 | |||
| 524 | 19830 | if (!buf || buf_size <= 0) | |
| 525 | 29 | return; | |
| 526 | 19830 | av_bprint_init_for_buffer(&bprint, buf, buf_size); | |
| 527 | 19830 | codec_type = av_get_media_type_string(enc->codec_type); | |
| 528 | 19830 | codec_name = avcodec_get_name(enc->codec_id); | |
| 529 | 19830 | profile = avcodec_profile_name(enc->codec_id, enc->profile); | |
| 530 | |||
| 531 | 19830 | av_bprintf(&bprint, "%s: %s", codec_type ? codec_type : "unknown", | |
| 532 | codec_name); | ||
| 533 | 19830 | buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */ | |
| 534 | |||
| 535 | 19830 | if (enc->codec && strcmp(enc->codec->name, codec_name)) | |
| 536 | 532 | av_bprintf(&bprint, " (%s)", enc->codec->name); | |
| 537 | |||
| 538 | 19830 | if (profile) | |
| 539 | 2739 | av_bprintf(&bprint, " (%s)", profile); | |
| 540 | 19830 | if ( enc->codec_type == AVMEDIA_TYPE_VIDEO | |
| 541 | 14459 | && av_log_get_level() >= AV_LOG_VERBOSE | |
| 542 | ✗ | && enc->refs) | |
| 543 | ✗ | av_bprintf(&bprint, ", %d reference frame%s", | |
| 544 | ✗ | enc->refs, enc->refs > 1 ? "s" : ""); | |
| 545 | |||
| 546 | 19830 | if (enc->codec_tag) | |
| 547 | 11041 | av_bprintf(&bprint, " (%s / 0x%04X)", | |
| 548 | 11041 | av_fourcc2str(enc->codec_tag), enc->codec_tag); | |
| 549 | |||
| 550 | 19830 | switch (enc->codec_type) { | |
| 551 | 14459 | case AVMEDIA_TYPE_VIDEO: | |
| 552 | { | ||
| 553 | unsigned len; | ||
| 554 | |||
| 555 | 14459 | av_bprintf(&bprint, "%s%s", separator, | |
| 556 | 14459 | enc->pix_fmt == AV_PIX_FMT_NONE ? "none" : | |
| 557 | 14397 | unknown_if_null(av_get_pix_fmt_name(enc->pix_fmt))); | |
| 558 | |||
| 559 | 14459 | av_bprint_chars(&bprint, '(', 1); | |
| 560 | 14459 | len = bprint.len; | |
| 561 | |||
| 562 | /* The following check ensures that '(' has been written | ||
| 563 | * and therefore allows us to erase it if it turns out | ||
| 564 | * to be unnecessary. */ | ||
| 565 | 14459 | if (!av_bprint_is_complete(&bprint)) | |
| 566 | ✗ | return; | |
| 567 | |||
| 568 | 14459 | if (enc->bits_per_raw_sample && enc->pix_fmt != AV_PIX_FMT_NONE && | |
| 569 | 1357 | enc->bits_per_raw_sample < av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth) | |
| 570 | 27 | av_bprintf(&bprint, "%d bpc, ", enc->bits_per_raw_sample); | |
| 571 | 26176 | if (enc->color_range != AVCOL_RANGE_UNSPECIFIED && | |
| 572 | 11717 | (str = av_color_range_name(enc->color_range))) | |
| 573 | 11717 | av_bprintf(&bprint, "%s, ", str); | |
| 574 | |||
| 575 | 14459 | if (enc->colorspace != AVCOL_SPC_UNSPECIFIED || | |
| 576 | 11238 | enc->color_primaries != AVCOL_PRI_UNSPECIFIED || | |
| 577 | 11234 | enc->color_trc != AVCOL_TRC_UNSPECIFIED) { | |
| 578 | 3383 | const char *col = unknown_if_null(av_color_space_name(enc->colorspace)); | |
| 579 | 3383 | const char *pri = unknown_if_null(av_color_primaries_name(enc->color_primaries)); | |
| 580 | 3383 | const char *trc = unknown_if_null(av_color_transfer_name(enc->color_trc)); | |
| 581 | 3383 | if (strcmp(col, pri) || strcmp(col, trc)) { | |
| 582 | 3204 | new_line = 1; | |
| 583 | 3204 | av_bprintf(&bprint, "%s/%s/%s, ", col, pri, trc); | |
| 584 | } else | ||
| 585 | 179 | av_bprintf(&bprint, "%s, ", col); | |
| 586 | } | ||
| 587 | |||
| 588 | 14459 | if (enc->field_order != AV_FIELD_UNKNOWN) { | |
| 589 | 7989 | const char *field_order = "progressive"; | |
| 590 | 7989 | if (enc->field_order == AV_FIELD_TT) | |
| 591 | 174 | field_order = "top first"; | |
| 592 | 7815 | else if (enc->field_order == AV_FIELD_BB) | |
| 593 | 52 | field_order = "bottom first"; | |
| 594 | 7763 | else if (enc->field_order == AV_FIELD_TB) | |
| 595 | 370 | field_order = "top coded first (swapped)"; | |
| 596 | 7393 | else if (enc->field_order == AV_FIELD_BT) | |
| 597 | 149 | field_order = "bottom coded first (swapped)"; | |
| 598 | |||
| 599 | 7989 | av_bprintf(&bprint, "%s, ", field_order); | |
| 600 | } | ||
| 601 | |||
| 602 | 14459 | if (av_log_get_level() >= AV_LOG_VERBOSE && | |
| 603 | ✗ | enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED && | |
| 604 | ✗ | (str = av_chroma_location_name(enc->chroma_sample_location))) | |
| 605 | ✗ | av_bprintf(&bprint, "%s, ", str); | |
| 606 | |||
| 607 | 14459 | if (len == bprint.len) { | |
| 608 | 1276 | bprint.str[len - 1] = '\0'; | |
| 609 | 1276 | bprint.len--; | |
| 610 | } else { | ||
| 611 | 13183 | if (bprint.len - 2 < bprint.size) { | |
| 612 | /* Erase the last ", " */ | ||
| 613 | 13183 | bprint.len -= 2; | |
| 614 | 13183 | bprint.str[bprint.len] = '\0'; | |
| 615 | } | ||
| 616 | 13183 | av_bprint_chars(&bprint, ')', 1); | |
| 617 | } | ||
| 618 | } | ||
| 619 | |||
| 620 | 14459 | if (enc->width) { | |
| 621 | 14430 | av_bprintf(&bprint, "%s%dx%d", new_line ? separator : ", ", | |
| 622 | enc->width, enc->height); | ||
| 623 | |||
| 624 | 14430 | if (av_log_get_level() >= AV_LOG_VERBOSE && | |
| 625 | ✗ | enc->coded_width && enc->coded_height && | |
| 626 | ✗ | (enc->width != enc->coded_width || | |
| 627 | ✗ | enc->height != enc->coded_height)) | |
| 628 | ✗ | av_bprintf(&bprint, " (%dx%d)", | |
| 629 | enc->coded_width, enc->coded_height); | ||
| 630 | |||
| 631 | 14430 | if (enc->sample_aspect_ratio.num) { | |
| 632 | 3016 | av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den, | |
| 633 | 3016 | enc->width * (int64_t)enc->sample_aspect_ratio.num, | |
| 634 | 3016 | enc->height * (int64_t)enc->sample_aspect_ratio.den, | |
| 635 | 1024 * 1024); | ||
| 636 | 3016 | av_bprintf(&bprint, " [SAR %d:%d DAR %d:%d]", | |
| 637 | enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den, | ||
| 638 | display_aspect_ratio.num, display_aspect_ratio.den); | ||
| 639 | } | ||
| 640 | 14430 | if (av_log_get_level() >= AV_LOG_DEBUG) { | |
| 641 | ✗ | int g = av_gcd(enc->time_base.num, enc->time_base.den); | |
| 642 | ✗ | av_bprintf(&bprint, ", %d/%d", | |
| 643 | ✗ | enc->time_base.num / g, enc->time_base.den / g); | |
| 644 | } | ||
| 645 | } | ||
| 646 | 14459 | if (encode) { | |
| 647 | 7629 | av_bprintf(&bprint, ", q=%d-%d", enc->qmin, enc->qmax); | |
| 648 | } | ||
| 649 | 14459 | break; | |
| 650 | 5012 | case AVMEDIA_TYPE_AUDIO: | |
| 651 | 5012 | av_bprintf(&bprint, "%s", separator); | |
| 652 | |||
| 653 | 5012 | if (enc->sample_rate) { | |
| 654 | 4990 | av_bprintf(&bprint, "%d Hz, ", enc->sample_rate); | |
| 655 | } | ||
| 656 | 5012 | av_channel_layout_describe_bprint(&enc->ch_layout, &bprint); | |
| 657 | 10013 | if (enc->sample_fmt != AV_SAMPLE_FMT_NONE && | |
| 658 | 5001 | (str = av_get_sample_fmt_name(enc->sample_fmt))) { | |
| 659 | 5001 | av_bprintf(&bprint, ", %s", str); | |
| 660 | } | ||
| 661 | 5012 | if ( enc->bits_per_raw_sample > 0 | |
| 662 | 2451 | && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8) | |
| 663 | 277 | av_bprintf(&bprint, " (%d bit)", enc->bits_per_raw_sample); | |
| 664 | 5012 | if (av_log_get_level() >= AV_LOG_VERBOSE) { | |
| 665 | 6 | if (enc->initial_padding) | |
| 666 | 3 | av_bprintf(&bprint, ", delay %d", enc->initial_padding); | |
| 667 | 6 | if (enc->trailing_padding) | |
| 668 | ✗ | av_bprintf(&bprint, ", padding %d", enc->trailing_padding); | |
| 669 | } | ||
| 670 | 5012 | break; | |
| 671 | 110 | case AVMEDIA_TYPE_DATA: | |
| 672 | 110 | if (av_log_get_level() >= AV_LOG_DEBUG) { | |
| 673 | ✗ | int g = av_gcd(enc->time_base.num, enc->time_base.den); | |
| 674 | ✗ | if (g) | |
| 675 | ✗ | av_bprintf(&bprint, ", %d/%d", | |
| 676 | ✗ | enc->time_base.num / g, enc->time_base.den / g); | |
| 677 | } | ||
| 678 | 110 | break; | |
| 679 | 220 | case AVMEDIA_TYPE_SUBTITLE: | |
| 680 | 220 | if (enc->width) | |
| 681 | 33 | av_bprintf(&bprint, ", %dx%d", enc->width, enc->height); | |
| 682 | 220 | break; | |
| 683 | 29 | default: | |
| 684 | 29 | return; | |
| 685 | } | ||
| 686 | 19801 | if (encode) { | |
| 687 | 9978 | if (enc->flags & AV_CODEC_FLAG_PASS1) | |
| 688 | ✗ | av_bprintf(&bprint, ", pass 1"); | |
| 689 | 9978 | if (enc->flags & AV_CODEC_FLAG_PASS2) | |
| 690 | ✗ | av_bprintf(&bprint, ", pass 2"); | |
| 691 | } | ||
| 692 | 19801 | bitrate = get_bit_rate(enc); | |
| 693 | 19801 | if (bitrate != 0) { | |
| 694 | 12804 | av_bprintf(&bprint, ", %"PRId64" kb/s", bitrate / 1000); | |
| 695 | 6997 | } else if (enc->rc_max_rate > 0) { | |
| 696 | ✗ | av_bprintf(&bprint, ", max. %"PRId64" kb/s", enc->rc_max_rate / 1000); | |
| 697 | } | ||
| 698 | } | ||
| 699 | |||
| 700 | 4536671 | int avcodec_is_open(AVCodecContext *s) | |
| 701 | { | ||
| 702 | 4536671 | return !!s->internal; | |
| 703 | } | ||
| 704 | |||
| 705 | 1649510 | int attribute_align_arg avcodec_receive_frame_flags(AVCodecContext *avctx, | |
| 706 | AVFrame *frame, unsigned flags) | ||
| 707 | { | ||
| 708 | 1649510 | av_frame_unref(frame); | |
| 709 | |||
| 710 | 1649510 | if (!avcodec_is_open(avctx) || !avctx->codec) | |
| 711 | ✗ | return AVERROR(EINVAL); | |
| 712 | |||
| 713 | 1649510 | if (ff_codec_is_decoder(avctx->codec)) | |
| 714 | 1649406 | return ff_decode_receive_frame(avctx, frame, flags); | |
| 715 | 104 | return ff_encode_receive_frame(avctx, frame); | |
| 716 | } | ||
| 717 | |||
| 718 | 30137 | int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame) | |
| 719 | { | ||
| 720 | 30137 | return avcodec_receive_frame_flags(avctx, frame, 0); | |
| 721 | } | ||
| 722 | |||
| 723 | #define WRAP_CONFIG(allowed_type, field, var, field_type, sentinel_check) \ | ||
| 724 | do { \ | ||
| 725 | if (codec->type != (allowed_type)) \ | ||
| 726 | return AVERROR(EINVAL); \ | ||
| 727 | const field_type *ptr = codec2->field; \ | ||
| 728 | *out_configs = ptr; \ | ||
| 729 | if (ptr) { \ | ||
| 730 | for (int i = 0;; i++) { \ | ||
| 731 | const field_type var = ptr[i]; \ | ||
| 732 | if (sentinel_check) { \ | ||
| 733 | *out_num_configs = i; \ | ||
| 734 | break; \ | ||
| 735 | } \ | ||
| 736 | } \ | ||
| 737 | } else \ | ||
| 738 | *out_num_configs = 0; \ | ||
| 739 | return 0; \ | ||
| 740 | } while (0) | ||
| 741 | |||
| 742 | static const enum AVColorRange color_range_tab[] = { | ||
| 743 | AVCOL_RANGE_MPEG, AVCOL_RANGE_JPEG, AVCOL_RANGE_UNSPECIFIED, | ||
| 744 | AVCOL_RANGE_MPEG, AVCOL_RANGE_UNSPECIFIED, | ||
| 745 | }; | ||
| 746 | |||
| 747 | static const enum AVAlphaMode alpha_mode_tab[] = { | ||
| 748 | AVALPHA_MODE_PREMULTIPLIED, AVALPHA_MODE_STRAIGHT, AVALPHA_MODE_UNSPECIFIED, | ||
| 749 | AVALPHA_MODE_PREMULTIPLIED, AVALPHA_MODE_UNSPECIFIED | ||
| 750 | }; | ||
| 751 | |||
| 752 | static_assert((int)AVCOL_RANGE_MPEG == (int)AVALPHA_MODE_PREMULTIPLIED, "unexpected enum values"); | ||
| 753 | static_assert((int)AVCOL_RANGE_JPEG == (int)AVALPHA_MODE_STRAIGHT, "unexpected enum values"); | ||
| 754 | static_assert(AVCOL_RANGE_UNSPECIFIED == 0 && AVALPHA_MODE_UNSPECIFIED == 0, "unexpected enum values"); | ||
| 755 | static_assert(AVCOL_RANGE_NB == 3 && AVALPHA_MODE_NB == 3, "unexpected enum values"); | ||
| 756 | |||
| 757 | static const uint8_t offset_tab[] = { | ||
| 758 | [AVCOL_RANGE_MPEG] = 3, | ||
| 759 | [AVCOL_RANGE_JPEG] = 1, | ||
| 760 | [AVCOL_RANGE_MPEG | AVCOL_RANGE_JPEG] = 0, | ||
| 761 | }; | ||
| 762 | |||
| 763 | 82323 | int ff_default_get_supported_config(const AVCodecContext *avctx, | |
| 764 | const AVCodec *codec, | ||
| 765 | enum AVCodecConfig config, | ||
| 766 | unsigned flags, | ||
| 767 | const void **out_configs, | ||
| 768 | int *out_num_configs) | ||
| 769 | { | ||
| 770 | 82323 | const FFCodec *codec2 = ffcodec(codec); | |
| 771 | |||
| 772 | 82323 | switch (config) { | |
| 773 | 33942 | case AV_CODEC_CONFIG_PIX_FORMAT: | |
| 774 | 56762 | WRAP_CONFIG(AVMEDIA_TYPE_VIDEO, pix_fmts, pix_fmt, enum AVPixelFormat, pix_fmt == AV_PIX_FMT_NONE); | |
| 775 | 7109 | case AV_CODEC_CONFIG_FRAME_RATE: | |
| 776 | 11406 | WRAP_CONFIG(AVMEDIA_TYPE_VIDEO, supported_framerates, framerate, AVRational, framerate.num == 0); | |
| 777 | 3416 | case AV_CODEC_CONFIG_SAMPLE_RATE: | |
| 778 | 6684 | WRAP_CONFIG(AVMEDIA_TYPE_AUDIO, supported_samplerates, samplerate, int, samplerate == 0); | |
| 779 | 3418 | case AV_CODEC_CONFIG_SAMPLE_FORMAT: | |
| 780 | 10556 | WRAP_CONFIG(AVMEDIA_TYPE_AUDIO, sample_fmts, sample_fmt, enum AVSampleFormat, sample_fmt == AV_SAMPLE_FMT_NONE); | |
| 781 | 3416 | case AV_CODEC_CONFIG_CHANNEL_LAYOUT: | |
| 782 | 5056 | WRAP_CONFIG(AVMEDIA_TYPE_AUDIO, ch_layouts, ch_layout, AVChannelLayout, ch_layout.nb_channels == 0); | |
| 783 | |||
| 784 | 7076 | case AV_CODEC_CONFIG_COLOR_RANGE: | |
| 785 | 7076 | if (codec->type != AVMEDIA_TYPE_VIDEO) | |
| 786 | ✗ | return AVERROR(EINVAL); | |
| 787 | 7076 | unsigned color_ranges = codec2->color_ranges; | |
| 788 | 7076 | if (color_ranges) | |
| 789 | 518 | *out_configs = color_range_tab + offset_tab[color_ranges]; | |
| 790 | else | ||
| 791 | 6558 | *out_configs = NULL; | |
| 792 | 7076 | *out_num_configs = av_popcount(color_ranges); | |
| 793 | 7076 | return 0; | |
| 794 | |||
| 795 | 7110 | case AV_CODEC_CONFIG_COLOR_SPACE: | |
| 796 | 7110 | *out_configs = NULL; | |
| 797 | 7110 | *out_num_configs = 0; | |
| 798 | 7110 | return 0; | |
| 799 | |||
| 800 | 9794 | case AV_CODEC_CONFIG_ALPHA_MODE: | |
| 801 | 9794 | if (codec->type != AVMEDIA_TYPE_VIDEO) | |
| 802 | ✗ | return AVERROR(EINVAL); | |
| 803 | 9794 | unsigned alpha_modes = codec2->alpha_modes; | |
| 804 | 9794 | if (alpha_modes) | |
| 805 | 34 | *out_configs = alpha_mode_tab + offset_tab[alpha_modes]; | |
| 806 | else | ||
| 807 | 9760 | *out_configs = NULL; | |
| 808 | 9794 | *out_num_configs = av_popcount(alpha_modes); | |
| 809 | 9794 | return 0; | |
| 810 | |||
| 811 | 7042 | case AV_CODEC_CONFIG_CHROMA_LOCATION: | |
| 812 | 7268 | WRAP_CONFIG(AVMEDIA_TYPE_VIDEO, chroma_locations, loc, | |
| 813 | enum AVChromaLocation, loc == AVCHROMA_LOC_UNSPECIFIED); | ||
| 814 | |||
| 815 | ✗ | default: | |
| 816 | ✗ | return AVERROR(EINVAL); | |
| 817 | } | ||
| 818 | } | ||
| 819 | |||
| 820 | 82425 | int avcodec_get_supported_config(const AVCodecContext *avctx, const AVCodec *codec, | |
| 821 | enum AVCodecConfig config, unsigned flags, | ||
| 822 | const void **out, int *out_num) | ||
| 823 | { | ||
| 824 | const FFCodec *codec2; | ||
| 825 | 82425 | int dummy_num = 0; | |
| 826 | 82425 | if (!codec) | |
| 827 | 82423 | codec = avctx->codec; | |
| 828 | 82425 | if (!out_num) | |
| 829 | 53238 | out_num = &dummy_num; | |
| 830 | |||
| 831 | 82425 | codec2 = ffcodec(codec); | |
| 832 | 82425 | if (codec2->get_supported_config) { | |
| 833 | 749 | return codec2->get_supported_config(avctx, codec, config, flags, out, out_num); | |
| 834 | } else { | ||
| 835 | 81676 | return ff_default_get_supported_config(avctx, codec, config, flags, out, out_num); | |
| 836 | } | ||
| 837 | } | ||
| 838 | |||
| 839 | ✗ | int av_packet_side_data_from_frame(AVPacketSideData **psd, int *pnb_sd, | |
| 840 | const AVFrameSideData *src, unsigned int flags) | ||
| 841 | { | ||
| 842 | ✗ | AVPacketSideData *sd = NULL; | |
| 843 | |||
| 844 | ✗ | for (unsigned j = 0; ff_sd_global_map[j].packet < AV_PKT_DATA_NB; j++) { | |
| 845 | ✗ | if (ff_sd_global_map[j].frame != src->type) | |
| 846 | ✗ | continue; | |
| 847 | |||
| 848 | ✗ | sd = av_packet_side_data_new(psd, pnb_sd, ff_sd_global_map[j].packet, | |
| 849 | ✗ | src->size, 0); | |
| 850 | |||
| 851 | ✗ | if (!sd) | |
| 852 | ✗ | return AVERROR(ENOMEM); | |
| 853 | |||
| 854 | ✗ | memcpy(sd->data, src->data, src->size); | |
| 855 | ✗ | break; | |
| 856 | } | ||
| 857 | |||
| 858 | ✗ | if (!sd) | |
| 859 | ✗ | return AVERROR(EINVAL); | |
| 860 | |||
| 861 | ✗ | return 0; | |
| 862 | } | ||
| 863 | |||
| 864 | 2 | int av_packet_side_data_to_frame(AVFrameSideData ***psd, int *pnb_sd, | |
| 865 | const AVPacketSideData *src, unsigned int flags) | ||
| 866 | { | ||
| 867 | 2 | AVFrameSideData *sd = NULL; | |
| 868 | |||
| 869 | 19 | for (unsigned j = 0; ff_sd_global_map[j].packet < AV_PKT_DATA_NB; j++) { | |
| 870 | 19 | if (ff_sd_global_map[j].packet != src->type) | |
| 871 | 17 | continue; | |
| 872 | |||
| 873 | 2 | sd = av_frame_side_data_new(psd, pnb_sd, ff_sd_global_map[j].frame, | |
| 874 | 2 | src->size, flags); | |
| 875 | |||
| 876 | 2 | if (!sd) | |
| 877 | ✗ | return AVERROR(ENOMEM); | |
| 878 | |||
| 879 | 2 | memcpy(sd->data, src->data, src->size); | |
| 880 | 2 | break; | |
| 881 | } | ||
| 882 | |||
| 883 | 2 | if (!sd) | |
| 884 | ✗ | return AVERROR(EINVAL); | |
| 885 | |||
| 886 | 2 | return 0; | |
| 887 | } | ||
| 888 |