FFmpeg
avcodec.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "libavutil/opt.h"
20 #include "libavcodec/codec.h"
21 #include "libavcodec/codec_desc.h"
23 
24 static const char *get_type_string(enum AVMediaType type)
25 {
26  const char *ret = av_get_media_type_string(type);
27  return ret ? ret : "unknown";
28 }
29 
30 #define AV_LOG(...) av_log(NULL, AV_LOG_FATAL, __VA_ARGS__)
31 #define ERR_INTERNAL(msg, ...) \
32 do { \
33  AV_LOG(msg, codec->name __VA_ARGS__); \
34  ret = 1; \
35 } while (0)
36 #define ERR(msg) ERR_INTERNAL(msg, )
37 #define ERR_EXT(msg, ...) ERR_INTERNAL(msg, , __VA_ARGS__)
38 
39 static int priv_data_size_wrong(const FFCodec *codec)
40 {
41  if (codec->priv_data_size < 0 ||
42  codec->p.priv_class && codec->priv_data_size < sizeof(AVClass*))
43  return 1;
44  if (!codec->p.priv_class || !codec->p.priv_class->option)
45  return 0;
46  for (const AVOption *opt = codec->p.priv_class->option; opt->name; opt++) {
47  if (opt->offset >= codec->priv_data_size ||
48  opt->type == AV_OPT_TYPE_CONST && opt->offset != 0 ||
49  opt->type != AV_OPT_TYPE_CONST && (opt->offset < sizeof(AVClass*) || opt->offset < 0)) {
50  AV_LOG("Option %s offset %d nonsensical\n",
51  opt->name, opt->offset);
52  return 1;
53  }
54  }
55  return 0;
56 }
57 
58 int main(void){
59  void *iter = NULL;
60  const AVCodec *codec = NULL;
61  int ret = 0;
62 
63  while (codec = av_codec_iterate(&iter)) {
64  const FFCodec *const codec2 = ffcodec(codec);
65  const AVCodecDescriptor *desc;
66  int is_decoder = 0, is_encoder = 0;
67 
68  if (!codec->name) {
69  AV_LOG("Codec for format %s has no name\n",
70  avcodec_get_name(codec->id));
71  ret = 1;
72  continue;
73  }
74  if (codec->type != AVMEDIA_TYPE_VIDEO &&
75  codec->type != AVMEDIA_TYPE_AUDIO &&
76  codec->type != AVMEDIA_TYPE_SUBTITLE)
77  ERR_EXT("Codec %s has unsupported type %s\n",
78  get_type_string(codec->type));
79  if (codec->type != AVMEDIA_TYPE_AUDIO) {
80  if (codec->ch_layouts || codec->sample_fmts ||
81  codec->supported_samplerates)
82  ERR("Non-audio codec %s has audio-only fields set\n");
86  ERR("Non-audio codec %s has audio-only capabilities set\n");
87  }
88  if (codec->type != AVMEDIA_TYPE_VIDEO) {
89  if (codec->pix_fmts || codec->supported_framerates)
90  ERR("Non-video codec %s has video-only fields set\n");
92  ERR("Non-video codec %s exports cropping\n");
93  }
96  ERR("Codec %s wants mainfunction despite not being "
97  "slice-threading capable");
102  ERR("Codec %s has private-only threading support\n");
103 
104  switch (codec2->cb_type) {
108  is_decoder = 1;
109  break;
113  is_encoder = 1;
114  break;
115  default:
116  ERR("Codec %s has unknown cb_type\n");
117  continue;
118  }
119  if (is_decoder != av_codec_is_decoder(codec) ||
120  is_encoder != av_codec_is_encoder(codec)) {
121  ERR("Codec %s cb_type and av_codec_is_(de|en)coder inconsistent.\n");
122  continue;
123  }
124 #define CHECK(TYPE, type) (codec2->cb_type == FF_CODEC_CB_TYPE_ ## TYPE && !codec2->cb.type)
125  if (CHECK(DECODE, decode) || CHECK(DECODE_SUB, decode_sub) ||
126  CHECK(RECEIVE_PACKET, receive_packet) ||
127  CHECK(ENCODE, encode) || CHECK(ENCODE_SUB, encode_sub) ||
128  CHECK(RECEIVE_FRAME, receive_frame)) {
129  ERR_EXT("Codec %s does not implement its %s callback.\n",
130  is_decoder ? "decoding" : "encoding");
131  }
132 #undef CHECK
133  if (is_encoder) {
134  if ((codec->type == AVMEDIA_TYPE_SUBTITLE) != (codec2->cb_type == FF_CODEC_CB_TYPE_ENCODE_SUB))
135  ERR("Encoder %s is both subtitle encoder and not subtitle encoder.");
136  if (codec2->update_thread_context || codec2->update_thread_context_for_user || codec2->bsfs)
137  ERR("Encoder %s has decoder-only thread functions or bsf.\n");
138  if (codec->type == AVMEDIA_TYPE_AUDIO) {
139  if (!codec->sample_fmts) {
140  av_log(NULL, AV_LOG_FATAL, "Encoder %s is missing the sample_fmts field\n", codec->name);
141  ret = 1;
142  }
143  }
152  ERR("Encoder %s has decoder-only capabilities set\n");
155  ERR("Frame-threaded encoder %s claims to support flushing\n");
158  ERR("Frame-threaded encoder %s claims to have delay\n");
159 
160  if (codec2->caps_internal & FF_CODEC_CAP_EOF_FLUSH &&
161  !(codec->capabilities & AV_CODEC_CAP_DELAY))
162  ERR("EOF_FLUSH encoder %s is not marked as having delay\n");
163  } else {
164  if ((codec->type == AVMEDIA_TYPE_SUBTITLE) != (codec2->cb_type == FF_CODEC_CB_TYPE_DECODE_SUB))
165  ERR("Subtitle decoder %s does not implement decode_sub callback\n");
166  if (codec->type == AVMEDIA_TYPE_SUBTITLE && codec2->bsfs)
167  ERR("Automatic bitstream filtering unsupported for subtitles; "
168  "yet decoder %s has it set\n");
173  ERR("Decoder %s has encoder-only capabilities\n");
174  if (codec2->cb_type != FF_CODEC_CB_TYPE_DECODE &&
176  ERR("Decoder %s is marked as setting pkt_dts when it doesn't have"
177  "any effect\n");
178  }
179  if (priv_data_size_wrong(codec2))
180  ERR_EXT("Private context of codec %s is impossibly-sized (size %d).",
181  codec2->priv_data_size);
182  if (!(desc = avcodec_descriptor_get(codec->id))) {
183  ERR("Codec %s lacks a corresponding descriptor\n");
184  } else if (desc->type != codec->type)
185  ERR_EXT("The type of AVCodec %s and its AVCodecDescriptor differ: "
186  "%s vs %s\n",
187  get_type_string(codec->type), get_type_string(desc->type));
188  }
189  return ret;
190 }
ERR
#define ERR(msg)
Definition: avcodec.c:36
FFCodec::update_thread_context
int(* update_thread_context)(struct AVCodecContext *dst, const struct AVCodecContext *src)
Copy necessary context variables from a previous thread context to the current one.
Definition: codec_internal.h:163
AVCodec
AVCodec.
Definition: codec.h:187
AVMEDIA_TYPE_SUBTITLE
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
FF_CODEC_CAP_SLICE_THREAD_HAS_MF
#define FF_CODEC_CAP_SLICE_THREAD_HAS_MF
Codec initializes slice-based threading with a main function.
Definition: codec_internal.h:65
opt.h
ENCODE
#define ENCODE(type, endian, src, dst, n, shift, offset)
Write PCM samples macro.
Definition: pcm.c:78
FF_CODEC_CB_TYPE_RECEIVE_PACKET
@ FF_CODEC_CB_TYPE_RECEIVE_PACKET
Definition: codec_internal.h:124
AVCodec::priv_class
const AVClass * priv_class
AVClass for the private context.
Definition: codec.h:221
FF_CODEC_CAP_EOF_FLUSH
#define FF_CODEC_CAP_EOF_FLUSH
The encoder has AV_CODEC_CAP_DELAY set, but does not actually have delay - it only wants to be flushe...
Definition: codec_internal.h:90
AVCodec::capabilities
int capabilities
Codec capabilities.
Definition: codec.h:206
AVOption
AVOption.
Definition: opt.h:429
FFCodec
Definition: codec_internal.h:127
FF_CODEC_CB_TYPE_ENCODE_SUB
@ FF_CODEC_CB_TYPE_ENCODE_SUB
Definition: codec_internal.h:121
AVCodec::supported_framerates
const attribute_deprecated AVRational * supported_framerates
Deprecated codec capabilities.
Definition: codec.h:213
FFCodec::priv_data_size
int priv_data_size
Definition: codec_internal.h:151
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
AVCodec::supported_samplerates
const attribute_deprecated int * supported_samplerates
Definition: codec.h:217
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
AV_CODEC_CAP_ENCODER_FLUSH
#define AV_CODEC_CAP_ENCODER_FLUSH
This encoder can be flushed using avcodec_flush_buffers().
Definition: codec.h:166
codec.h
FF_CODEC_CB_TYPE_DECODE
@ FF_CODEC_CB_TYPE_DECODE
Definition: codec_internal.h:109
FF_CODEC_CAP_USES_PROGRESSFRAMES
#define FF_CODEC_CAP_USES_PROGRESSFRAMES
The decoder might make use of the ProgressFrame API.
Definition: codec_internal.h:69
FFCodec::update_thread_context_for_user
int(* update_thread_context_for_user)(struct AVCodecContext *dst, const struct AVCodecContext *src)
Copy variables back to the user-facing context.
Definition: codec_internal.h:168
decode
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:72
AVCodecDescriptor
This struct describes the properties of a single codec described by an AVCodecID.
Definition: codec_desc.h:38
priv_data_size_wrong
static int priv_data_size_wrong(const FFCodec *codec)
Definition: avcodec.c:39
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AV_CODEC_CAP_OTHER_THREADS
#define AV_CODEC_CAP_OTHER_THREADS
Codec supports multithreading through a method other than slice- or frame-level multithreading.
Definition: codec.h:124
AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition: codec.h:159
FF_CODEC_CB_TYPE_ENCODE
@ FF_CODEC_CB_TYPE_ENCODE
Definition: codec_internal.h:118
receive_frame
static int receive_frame(AVFilterLink *inlink, AVFrame *out)
Definition: vf_lcevc.c:248
AV_CODEC_CAP_FRAME_THREADS
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: codec.h:110
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
NULL
#define NULL
Definition: coverity.c:32
AVCodec::type
enum AVMediaType type
Definition: codec.h:200
get_type_string
static const char * get_type_string(enum AVMediaType type)
Definition: avcodec.c:24
FF_CODEC_CB_TYPE_DECODE_SUB
@ FF_CODEC_CB_TYPE_DECODE_SUB
Definition: codec_internal.h:112
AV_CODEC_CAP_VARIABLE_FRAME_SIZE
#define AV_CODEC_CAP_VARIABLE_FRAME_SIZE
Audio encoder supports receiving a different number of samples in each call.
Definition: codec.h:128
AV_CODEC_CAP_CHANNEL_CONF
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition: codec.h:106
av_codec_is_decoder
int av_codec_is_decoder(const AVCodec *codec)
Definition: utils.c:86
main
int main(void)
Definition: avcodec.c:58
AVMediaType
AVMediaType
Definition: avutil.h:199
AVCodec::ch_layouts
const attribute_deprecated AVChannelLayout * ch_layouts
Array of supported channel layouts, terminated with a zeroed layout.
Definition: codec.h:241
codec_internal.h
AVCodec::pix_fmts
attribute_deprecated enum AVPixelFormat * pix_fmts
Definition: codec.h:215
FF_CODEC_CAP_SETS_FRAME_PROPS
#define FF_CODEC_CAP_SETS_FRAME_PROPS
Codec handles output frame properties internally instead of letting the internal logic derive them fr...
Definition: codec_internal.h:78
FF_CODEC_CAP_EXPORTS_CROPPING
#define FF_CODEC_CAP_EXPORTS_CROPPING
The decoder sets the cropping fields in the output frames manually.
Definition: codec_internal.h:61
FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM
#define FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM
The decoder extracts and fills its parameters even if the frame is skipped due to the skip_frame sett...
Definition: codec_internal.h:55
ffcodec
static const av_always_inline FFCodec * ffcodec(const AVCodec *codec)
Definition: codec_internal.h:330
DECODE
#define DECODE(size, endian, src, dst, n, shift, offset)
Read PCM samples macro.
Definition: pcm.c:305
AVOption::name
const char * name
Definition: opt.h:430
ERR_EXT
#define ERR_EXT(msg,...)
Definition: avcodec.c:37
AV_LOG
#define AV_LOG(...)
Definition: avcodec.c:30
encode
static void encode(AVCodecContext *ctx, AVFrame *frame, AVPacket *pkt, FILE *output)
Definition: encode_audio.c:94
AV_CODEC_CAP_SLICE_THREADS
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: codec.h:114
AVCodec::id
enum AVCodecID id
Definition: codec.h:201
av_codec_is_encoder
int av_codec_is_encoder(const AVCodec *codec)
Definition: utils.c:78
avcodec_get_name
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
Definition: utils.c:409
FFCodec::caps_internal
unsigned caps_internal
Internal codec capabilities FF_CODEC_CAP_*.
Definition: codec_internal.h:136
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
FF_CODEC_CAP_SETS_PKT_DTS
#define FF_CODEC_CAP_SETS_PKT_DTS
Decoders marked with FF_CODEC_CAP_SETS_PKT_DTS want to set AVFrame.pkt_dts manually.
Definition: codec_internal.h:50
AVCodec::sample_fmts
attribute_deprecated enum AVSampleFormat * sample_fmts
Definition: codec.h:219
av_codec_iterate
const AVCodec * av_codec_iterate(void **opaque)
Iterate over all registered codecs.
Definition: allcodecs.c:959
ret
ret
Definition: filter_design.txt:187
AV_LOG_FATAL
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
Definition: log.h:203
av_get_media_type_string
const char * av_get_media_type_string(enum AVMediaType media_type)
Return a string describing the media_type enum, NULL if media_type is unknown.
Definition: utils.c:28
AVClass::option
const struct AVOption * option
An array of options for the structure or NULL.
Definition: log.h:95
AV_CODEC_CAP_DELAY
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: codec.h:76
FF_CODEC_CB_TYPE_RECEIVE_FRAME
@ FF_CODEC_CB_TYPE_RECEIVE_FRAME
Definition: codec_internal.h:115
FFCodec::cb_type
unsigned cb_type
This field determines the type of the codec (decoder/encoder) and also the exact callback cb implemen...
Definition: codec_internal.h:149
FFCodec::bsfs
const char * bsfs
Decoding only, a comma-separated list of bitstream filters to apply to packets before decoding.
Definition: codec_internal.h:250
desc
const char * desc
Definition: libsvtav1.c:79
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
FF_CODEC_CAP_AUTO_THREADS
#define FF_CODEC_CAP_AUTO_THREADS
Codec handles avctx->thread_count == 0 (auto) internally.
Definition: codec_internal.h:73
CHECK
#define CHECK(TYPE, type)
AV_CODEC_CAP_DRAW_HORIZ_BAND
#define AV_CODEC_CAP_DRAW_HORIZ_BAND
Decoder can use draw_horiz_band callback.
Definition: codec.h:44
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AV_CODEC_CAP_AVOID_PROBING
#define AV_CODEC_CAP_AVOID_PROBING
Decoder is not a preferred choice for probing.
Definition: codec.h:138
avcodec_descriptor_get
const AVCodecDescriptor * avcodec_descriptor_get(enum AVCodecID id)
Definition: codec_desc.c:3748
AV_CODEC_CAP_SMALL_LAST_FRAME
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
Definition: codec.h:81
codec_desc.h
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition: opt.h:299