libav: switch from CODEC_ID to AV_CODEC_ID
[mplayer.git] / av_log.c
blob4e27e98d0f8b8fca4db38435b0ecf61feca23ddd
1 /*
2 * av_log to mp_msg converter
3 * Copyright (C) 2006 Michael Niedermayer
4 * Copyright (C) 2009 Uoti Urpala
6 * This file is part of MPlayer.
8 * MPlayer is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * MPlayer is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <stdlib.h>
24 #include <stdbool.h>
26 #include "av_log.h"
27 #include "config.h"
28 #include "mp_msg.h"
29 #include <libavutil/avutil.h>
30 #include <libavutil/log.h>
32 #include <libavcodec/avcodec.h>
33 #include <libavformat/avformat.h>
34 #include <libswscale/swscale.h>
36 static int av_log_level_to_mp_level(int av_level)
38 if (av_level > AV_LOG_VERBOSE)
39 return MSGL_DBG2;
40 if (av_level > AV_LOG_INFO)
41 return MSGL_V;
42 if (av_level > AV_LOG_WARNING)
43 return MSGL_INFO;
44 if (av_level > AV_LOG_ERROR)
45 return MSGL_WARN;
46 if (av_level > AV_LOG_FATAL)
47 return MSGL_ERR;
48 return MSGL_FATAL;
51 static int extract_msg_type_from_ctx(void *ptr)
53 if (!ptr)
54 return MSGT_FIXME;
56 AVClass *avc = *(AVClass **)ptr;
57 if (!avc) {
58 mp_msg(MSGT_FIXME, MSGL_WARN,
59 "av_log callback called with bad parameters (NULL AVClass).\n"
60 "This is a bug in one of Libav/FFmpeg libraries used.\n");
61 return MSGT_FIXME;
64 if (!strcmp(avc->class_name, "AVCodecContext")) {
65 AVCodecContext *s = ptr;
66 if (s->codec) {
67 if (s->codec->type == AVMEDIA_TYPE_AUDIO) {
68 if (s->codec->decode)
69 return MSGT_DECAUDIO;
70 } else if (s->codec->type == AVMEDIA_TYPE_VIDEO) {
71 if (s->codec->decode)
72 return MSGT_DECVIDEO;
74 // FIXME subtitles, encoders
75 // What msgt for them? There is nothing appropriate...
77 return MSGT_FIXME;
80 if (!strcmp(avc->class_name, "AVFormatContext")) {
81 AVFormatContext *s = ptr;
82 if (s->iformat)
83 return MSGT_DEMUXER;
84 else if (s->oformat)
85 return MSGT_MUXER;
86 return MSGT_FIXME;
89 return MSGT_FIXME;
92 static void mp_msg_av_log_callback(void *ptr, int level, const char *fmt,
93 va_list vl)
95 static bool print_prefix = 1;
96 AVClass *avc = ptr ? *(AVClass **)ptr : NULL;
97 int mp_level = av_log_level_to_mp_level(level);
98 int type = extract_msg_type_from_ctx(ptr);
100 if (!mp_msg_test(type, mp_level))
101 return;
103 if (print_prefix && avc)
104 mp_msg(type, mp_level, "[%s @ %p]", avc->item_name(ptr), avc);
105 print_prefix = fmt[strlen(fmt) - 1] == '\n';
107 mp_msg_va(type, mp_level, fmt, vl);
110 void init_libav(void)
112 av_log_set_callback(mp_msg_av_log_callback);
113 avcodec_register_all();
114 av_register_all();
115 avformat_network_init();
118 #define V(x) (x)>>16, (x)>>8 & 255, (x) & 255
119 static void print_version(char *name, unsigned buildv, unsigned runv)
122 if (buildv == runv)
123 mp_msg(MSGT_CPLAYER, MSGL_V, "Compiled against %s version %d.%d.%d\n",
124 name, V(buildv));
125 else
126 mp_msg(MSGT_CPLAYER, MSGL_V, "Compiled against %s version %d.%d.%d "
127 "(runtime %d.%d.%d)\n", name, V(buildv), V(runv));
129 #undef V
131 void print_libav_versions(void)
133 print_version("libavutil", LIBAVUTIL_VERSION_INT, avutil_version());
134 print_version("libavcodec", LIBAVCODEC_VERSION_INT, avcodec_version());
135 print_version("libavformat", LIBAVFORMAT_VERSION_INT, avformat_version());
136 print_version("libswscale", LIBSWSCALE_VERSION_INT, swscale_version());