[FFmpeg-devel] [PATCH 1/8] lavc/avcodec: simplify codec id/type validity checking

Soft Works softworkz at hotmail.com
Sun Jun 5 08:23:18 EEST 2022



> -----Original Message-----
> From: ffmpeg-devel <ffmpeg-devel-bounces at ffmpeg.org> On Behalf Of Anton
> Khirnov
> Sent: Wednesday, March 23, 2022 4:57 PM
> To: ffmpeg-devel at ffmpeg.org
> Subject: [FFmpeg-devel] [PATCH 1/8] lavc/avcodec: simplify codec id/type
> validity checking
> 
> On entry to avcodec_open2(), the type and id either have to be
> UNKNOWN/NONE or have to match the codec to be used.
> ---
>  libavcodec/avcodec.c | 14 ++++++--------
>  1 file changed, 6 insertions(+), 8 deletions(-)
> 
> diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c
> index fbe4a5e413..dbaa9f78a2 100644
> --- a/libavcodec/avcodec.c
> +++ b/libavcodec/avcodec.c
> @@ -158,17 +158,15 @@ int attribute_align_arg avcodec_open2(AVCodecContext
> *avctx, const AVCodec *code
>          codec = avctx->codec;
>      codec2 = ffcodec(codec);
> 
> -    if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type ==
> codec->type) &&
> -        avctx->codec_id == AV_CODEC_ID_NONE) {
> -        avctx->codec_type = codec->type;
> -        avctx->codec_id   = codec->id;
> -    }
> -    if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type &&
> -                                         avctx->codec_type !=
> AVMEDIA_TYPE_ATTACHMENT)) {
> +    if ((avctx->codec_type != AVMEDIA_TYPE_UNKNOWN && avctx->codec_type !=
> codec->type) ||
> +        (avctx->codec_id   != AV_CODEC_ID_NONE     && avctx->codec_id   !=
> codec->id)) {
>          av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
>          return AVERROR(EINVAL);
>      }
> -    avctx->codec = codec;
> +
> +    avctx->codec_type = codec->type;
> +    avctx->codec_id   = codec->id;
> +    avctx->codec      = codec;
> 
>      if (avctx->extradata_size < 0 || avctx->extradata_size >=
> FF_MAX_EXTRADATA_SIZE)
>          return AVERROR(EINVAL);
> --

This is causing a regression in ffprobe. 

The commit removes the special-case check for AVMEDIA_TYPE_ATTACHMENT which 
was required for ffprobe and had been added with e83c716e16c52fa56a78274408f7628e5dc719da.

The demand from the commit message is not yet guaranteed to be fulfilled:

> On entry to avcodec_open2(), the type and id either have to be
> UNKNOWN/NONE or have to match the codec to be used.

I have one verified example (maybe a second will follow), which is an MKV with
an attachment "stream" of type "text".
The found codec will be textdec of type 'subtitle' even though the stream type
is attachment. Without the special condition for attachment streams, this 
is now causing ffprobe to error out with non-zero exit code and incomplete 
output.


------------------------------------------------------------------------
Example:
  
  [...]
  Stream #0:9: Attachment: text
    Metadata:
      filename        : textfile.text
      mimetype        : text/plain
[text @ 000001AC32310340] Codec type or id mismatches
Could not open codec for input stream 9
------------------------------------------------------------------------

Regards,
softworkz


More information about the ffmpeg-devel mailing list