| 142 | } |
| 143 | |
| 144 | int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options) |
| 145 | { |
| 146 | int ret = 0; |
| 147 | AVCodecInternal *avci; |
| 148 | const FFCodec *codec2; |
| 149 | const AVDictionaryEntry *e; |
| 150 | |
| 151 | if (avcodec_is_open(avctx)) |
| 152 | return 0; |
| 153 | |
| 154 | if (!codec && !avctx->codec) { |
| 155 | av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n"); |
| 156 | return AVERROR(EINVAL); |
| 157 | } |
| 158 | if (codec && avctx->codec && codec != avctx->codec) { |
| 159 | av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, " |
| 160 | "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name); |
| 161 | return AVERROR(EINVAL); |
| 162 | } |
| 163 | if (!codec) |
| 164 | codec = avctx->codec; |
| 165 | codec2 = ffcodec(codec); |
| 166 | |
| 167 | if ((avctx->codec_type != AVMEDIA_TYPE_UNKNOWN && avctx->codec_type != codec->type) || |
| 168 | (avctx->codec_id != AV_CODEC_ID_NONE && avctx->codec_id != codec->id)) { |
| 169 | av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n"); |
| 170 | return AVERROR(EINVAL); |
| 171 | } |
| 172 | |
| 173 | avctx->codec_type = codec->type; |
| 174 | avctx->codec_id = codec->id; |
| 175 | avctx->codec = codec; |
| 176 | |
| 177 | if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE) |
| 178 | return AVERROR(EINVAL); |
| 179 | |
| 180 | // set the whitelist from provided options dict, |
| 181 | // so we can check it immediately |
| 182 | e = options ? av_dict_get(*options, "codec_whitelist", NULL, 0) : NULL; |
| 183 | if (e) { |
| 184 | ret = av_opt_set(avctx, e->key, e->value, 0); |
| 185 | if (ret < 0) |
| 186 | return ret; |
| 187 | } |
| 188 | |
| 189 | if (avctx->codec_whitelist && av_match_list(codec->name, avctx->codec_whitelist, ',') <= 0) { |
| 190 | av_log(avctx, AV_LOG_ERROR, "Codec (%s) not on whitelist \'%s\'\n", codec->name, avctx->codec_whitelist); |
| 191 | return AVERROR(EINVAL); |
| 192 | } |
| 193 | |
| 194 | avci = ff_codec_is_decoder(codec) ? |
| 195 | ff_decode_internal_alloc() : |
| 196 | ff_encode_internal_alloc(); |
| 197 | if (!avci) { |
| 198 | ret = AVERROR(ENOMEM); |
| 199 | goto end; |
| 200 | } |
| 201 | avctx->internal = avci; |