54#include <libavutil/channel_layout.h>
58#define MOD_NAME "[lavc_common] "
67 { AV_CODEC_ID_H264,
H264 },
68 { AV_CODEC_ID_HEVC,
H265 },
69 { AV_CODEC_ID_MJPEG,
MJPG },
70 { AV_CODEC_ID_JPEG2000,
J2K },
71 { AV_CODEC_ID_VP8,
VP8 },
72 { AV_CODEC_ID_VP9,
VP9 },
73 { AV_CODEC_ID_HUFFYUV,
HFYU },
74 { AV_CODEC_ID_FFV1,
FFV1 },
75 { AV_CODEC_ID_AV1,
AV1 },
76 { AV_CODEC_ID_PRORES,
PRORES },
96 return AV_CODEC_ID_NONE;
104 av_strerror(rc, errbuf,
sizeof(errbuf));
128 if (level <= AV_LOG_PANIC) {
129 level += AV_TO_UV_MULT;
132 if (level >= AV_LOG_INFO) {
133 level += AV_TO_UV_MULT;
135 return level / AV_TO_UV_MULT;
144 if (ff_module_name && strcmp(ff_module_name,
"mjpeg") == 0 && strstr(
fmt,
"APP") != NULL) {
156 AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
157 const char *ff_module_name = avc ? avc->item_name(avcl) : NULL;
161 static _Thread_local
char buf[
STR_LEN];
162 if (strlen(buf) == 0) {
163 snprintf(buf + strlen(buf),
sizeof buf - strlen(buf),
"[lavc");
164 if (ff_module_name) {
165 snprintf(buf + strlen(buf),
sizeof buf - strlen(buf),
166 " %s @ %p", ff_module_name, avcl);
168 snprintf(buf + strlen(buf),
sizeof buf - strlen(buf),
"] ");
170 vsnprintf(buf + strlen(buf),
sizeof buf - strlen(buf),
fmt, vl);
171 if (buf[strlen(buf) - 1] !=
'\n' && strlen(buf) <
sizeof buf - 1) {
174 if (strlen(buf) ==
sizeof buf - 1 && buf[strlen(buf) - 1] !=
'\n') {
175 MSG(WARNING,
"logger buffer full! flushing output:\n");
183 "* lavc-log-level=<num>\n"
184 " Set libavcodec log level (FFmpeg range semantics, bypasses UG logger)\n"
185 " - 'D' - use FFmpeg default log handler\n");
189 const char *
param = NULL;
198 const long av_log_level = strtol(
param, &endptr, 10);
199 if (av_log_level < AV_LOG_QUIET || av_log_level > AV_LOG_TRACE ||
201 MSG(ERROR,
"Wrong log level for FFmpeg '%s'\n",
param);
203 av_log_set_level((
int) av_log_level);
208 const struct AVPixFmtDescriptor *pd = av_pix_fmt_desc_get(
fmt);
209 if (pd->log2_chroma_w == 0 && pd->log2_chroma_h == 0) {
212 if (pd->log2_chroma_w == 1 && pd->log2_chroma_h == 0) {
215 if (pd->log2_chroma_w == 1 && pd->log2_chroma_h == 1) {
223 const struct AVPixFmtDescriptor *avd = av_pix_fmt_desc_get(pixfmt);
224 ret.
depth = avd->comp[0].depth;
225 ret.
rgb = avd->flags & AV_PIX_FMT_FLAG_RGB;
232#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 37, 100)
234 char errbuf[AV_ERROR_MAX_STRING_SIZE];
235 ret = avcodec_send_packet(codec_ctx, NULL);
237 av_strerror(ret, errbuf,
sizeof errbuf);
240 "lavd_flush send - unexpected return value: %s (%d)\n",
243 AVFrame *
frame = av_frame_alloc();
245 ret = avcodec_receive_frame(codec_ctx,
frame);
246 }
while (ret >= 0 && ret != AVERROR_EOF && ret != AVERROR(EAGAIN));
247 if (ret < 0 && ret != AVERROR_EOF && ret != AVERROR(EAGAIN)) {
248 av_strerror(ret, errbuf,
sizeof errbuf);
251 "lavd_flush recv - unexpected return value: %s (%d)\n",
254 av_frame_free(&
frame);
266 "%sNo frame returned - needs more input data.\n",
274 av_strerror(rc, buf,
sizeof buf);
276 "%sError while decoding frame (rc == %d): %s.\n",
283 static _Thread_local
char buf[128];
284 memset(buf, 0,
sizeof buf);
285 if ((thread_type & FF_THREAD_FRAME) != 0) {
286 strncpy(buf,
"thread",
sizeof buf - 1);
288 if ((thread_type & FF_THREAD_SLICE) != 0) {
289 if (strlen(buf) > 0) {
290 strncpy(buf,
", ",
sizeof buf - 1);
292 strncpy(buf,
"slice",
sizeof buf - 1);
294 if (strlen(buf) == 0) {
304 desc.bps = av_get_bytes_per_sample(frm->format);
305 #if FF_API_NEW_CHANNEL_LAYOUT
306 desc.ch_count = frm->ch_layout.nb_channels;
308 desc.ch_count = av_get_channel_layout_nb_channels(frm->channel_layout);
311 desc.sample_rate = frm->sample_rate;
321 return planar ? AV_SAMPLE_FMT_U8P : AV_SAMPLE_FMT_U8;
323 return planar ? AV_SAMPLE_FMT_S16P : AV_SAMPLE_FMT_S16;
327 return planar ? AV_SAMPLE_FMT_S32P : AV_SAMPLE_FMT_S32;
341 _Thread_local
static char buf[
STR_LEN];
342 if (pixfmts == NULL || *pixfmts == AV_PIX_FMT_NONE) {
343 snprintf(buf,
sizeof buf,
"(none)");
347 const enum AVPixelFormat *it = pixfmts;
348 while (*it != AV_PIX_FMT_NONE) {
349 snprintf(buf + strlen(buf),
sizeof buf - strlen(buf),
"%s%s",
350 it != pixfmts ?
" " :
"", av_get_pix_fmt_name(*it));
struct video_frame * frame
void log_msg(int level, const char *format,...)
#define LOG_LEVEL_ERROR
general errors
#define LOG_LEVEL_VERBOSE
#define LOG_LEVEL_WARNING
less severe errors
const char * get_commandline_param(const char *key)
#define ADD_TO_PARAM(param, doc)
codec_t get_av_to_ug_codec(enum AVCodecID av_codec)
enum AVSampleFormat audio_bps_to_av_sample_fmt(int bps, bool planar)
const char * lavc_thread_type_to_str(int thread_type)
struct audio_desc audio_desc_from_av_frame(const AVFrame *frm)
static _Bool av_log_filter(const char *ff_module_name, const char *fmt)
void print_decoder_error(const char *mod_name, int rc)
struct pixfmt_desc av_pixfmt_get_desc(enum AVPixelFormat pixfmt)
enum AVCodecID get_ug_to_av_codec(codec_t ug_codec)
void lavd_flush(AVCodecContext *codec_ctx)
const char * get_avpixfmts_names(const enum AVPixelFormat *pixfmts)
void ug_set_av_logging()
Sets specified log level either given explicitly or from UG-wide log_level.
int av_pixfmt_get_subsampling(enum AVPixelFormat fmt)
void printf_libav_error(int verbosity, int rc, const char *msg,...)
bool libav_codec_has_extradata(codec_t codec)
static int av_to_uv_log(int level)
static const struct @21 av_to_uv_map[]
void print_libav_error(int verbosity, const char *msg, int rc)
static void av_log_ug_callback(void *avcl, int av_level, const char *fmt, va_list vl)
@ STR_LEN
"standard" string length placeholder
int depth
bit depth; 0 means that whole struct is undefined
int subsampling
in 'JabA' format, eg. '4444'
@ VIDEO_CODEC_NONE
dummy color spec
@ PRORES
abstract Apple ProRes, must not be used in transmit
@ MJPG
JPEG image, without restart intervals.
This is an umbrella header for video functions.
int vsnprintf(char *s, size_t buf_size, const char *format, va_list ap)