gstav: Use libavcodec util function for version check
[platform/upstream/gst-libav.git] / ext / libav / gstav.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library 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  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 /* First, include the header file for the plugin, to bring in the
21  * object definition and other useful things.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27 #include <stdio.h>
28 #include <string.h>
29 #include <gst/gst.h>
30
31 #include "gstav.h"
32 #include "gstavutils.h"
33 #include "gstavcfg.h"
34
35 #ifdef GST_LIBAV_ENABLE_GPL
36 #define LICENSE "GPL"
37 #else
38 #define LICENSE "LGPL"
39 #endif
40
41 GST_DEBUG_CATEGORY (ffmpeg_debug);
42
43 static GMutex gst_avcodec_mutex;
44
45 /*
46  * Check for FFmpeg-provided libavcodec/format
47  */
48 static inline gboolean
49 gst_ffmpeg_avcodec_is_ffmpeg (void)
50 {
51   guint av_version = avcodec_version ();
52
53   GST_DEBUG ("Using libavcodec version %d.%d.%d",
54       av_version >> 16, (av_version & 0x00ff00) >> 8, av_version & 0xff);
55
56   /* FFmpeg *_MICRO versions start at 100 and Libav's at 0 */
57   if ((av_version & 0xff) < 100)
58     return FALSE;
59
60   return TRUE;
61 }
62
63 int
64 gst_ffmpeg_avcodec_open (AVCodecContext * avctx, AVCodec * codec)
65 {
66   int ret;
67
68   g_mutex_lock (&gst_avcodec_mutex);
69   ret = avcodec_open2 (avctx, codec, NULL);
70   g_mutex_unlock (&gst_avcodec_mutex);
71
72   return ret;
73 }
74
75 int
76 gst_ffmpeg_avcodec_close (AVCodecContext * avctx)
77 {
78   int ret;
79
80   g_mutex_lock (&gst_avcodec_mutex);
81   ret = avcodec_close (avctx);
82   g_mutex_unlock (&gst_avcodec_mutex);
83
84   return ret;
85 }
86
87 int
88 gst_ffmpeg_av_find_stream_info (AVFormatContext * ic)
89 {
90   int ret;
91
92   g_mutex_lock (&gst_avcodec_mutex);
93   ret = avformat_find_stream_info (ic, NULL);
94   g_mutex_unlock (&gst_avcodec_mutex);
95
96   return ret;
97 }
98
99 #ifndef GST_DISABLE_GST_DEBUG
100 static void
101 gst_ffmpeg_log_callback (void *ptr, int level, const char *fmt, va_list vl)
102 {
103   GstDebugLevel gst_level;
104   gint len = strlen (fmt);
105   gchar *fmt2 = NULL;
106
107   switch (level) {
108     case AV_LOG_QUIET:
109       gst_level = GST_LEVEL_NONE;
110       break;
111     case AV_LOG_ERROR:
112       gst_level = GST_LEVEL_ERROR;
113       break;
114     case AV_LOG_INFO:
115       gst_level = GST_LEVEL_INFO;
116       break;
117     case AV_LOG_DEBUG:
118       gst_level = GST_LEVEL_DEBUG;
119       break;
120     default:
121       gst_level = GST_LEVEL_INFO;
122       break;
123   }
124
125   /* remove trailing newline as it gets already appended by the logger */
126   if (fmt[len - 1] == '\n') {
127     fmt2 = g_strdup (fmt);
128     fmt2[len - 1] = '\0';
129   }
130
131   gst_debug_log_valist (ffmpeg_debug, gst_level, "", "", 0, NULL,
132       fmt2 ? fmt2 : fmt, vl);
133
134   g_free (fmt2);
135 }
136 #endif
137
138 static gboolean
139 plugin_init (GstPlugin * plugin)
140 {
141   GST_DEBUG_CATEGORY_INIT (ffmpeg_debug, "libav", 0, "libav elements");
142
143   /* Bail if not FFmpeg. We can no longer ensure operation with Libav */
144   if (!gst_ffmpeg_avcodec_is_ffmpeg ()) {
145     GST_ERROR_OBJECT (plugin,
146         "Incompatible, non-FFmpeg libavcodec/format found");
147     return FALSE;
148   }
149 #ifndef GST_DISABLE_GST_DEBUG
150   av_log_set_callback (gst_ffmpeg_log_callback);
151 #endif
152
153   gst_ffmpeg_init_pix_fmt_info ();
154
155   /* build global ffmpeg param/property info */
156   gst_ffmpeg_cfg_init ();
157
158   gst_ffmpegaudenc_register (plugin);
159   gst_ffmpegvidenc_register (plugin);
160   gst_ffmpegauddec_register (plugin);
161   gst_ffmpegviddec_register (plugin);
162   gst_ffmpegdemux_register (plugin);
163   gst_ffmpegmux_register (plugin);
164   gst_ffmpegdeinterlace_register (plugin);
165
166   /* Now we can return the pointer to the newly created Plugin object. */
167   return TRUE;
168 }
169
170 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
171     GST_VERSION_MINOR,
172     libav,
173     "All libav codecs and formats (" LIBAV_SOURCE ")",
174     plugin_init, PACKAGE_VERSION, LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)