7884a516eec102075d946f2243cf6bfc50b1471a
[platform/upstream/gstreamer.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 <libavcodec/avcodec.h>
32 #include <libavformat/avformat.h>
33
34 #include "gstav.h"
35 #include "gstavutils.h"
36
37 GST_DEBUG_CATEGORY (ffmpeg_debug);
38
39 static GStaticMutex gst_avcodec_mutex = G_STATIC_MUTEX_INIT;
40
41
42 int
43 gst_ffmpeg_avcodec_open (AVCodecContext * avctx, AVCodec * codec)
44 {
45   int ret;
46
47   g_static_mutex_lock (&gst_avcodec_mutex);
48   ret = avcodec_open2 (avctx, codec, NULL);
49   g_static_mutex_unlock (&gst_avcodec_mutex);
50
51   return ret;
52 }
53
54 int
55 gst_ffmpeg_avcodec_close (AVCodecContext * avctx)
56 {
57   int ret;
58
59   g_static_mutex_lock (&gst_avcodec_mutex);
60   ret = avcodec_close (avctx);
61   g_static_mutex_unlock (&gst_avcodec_mutex);
62
63   return ret;
64 }
65
66 int
67 gst_ffmpeg_av_find_stream_info (AVFormatContext * ic)
68 {
69   int ret;
70
71   g_static_mutex_lock (&gst_avcodec_mutex);
72   ret = avformat_find_stream_info (ic, NULL);
73   g_static_mutex_unlock (&gst_avcodec_mutex);
74
75   return ret;
76 }
77
78 #ifndef GST_DISABLE_GST_DEBUG
79 static void
80 gst_ffmpeg_log_callback (void *ptr, int level, const char *fmt, va_list vl)
81 {
82   GstDebugLevel gst_level;
83   gint len = strlen (fmt);
84   gchar *fmt2 = NULL;
85
86   switch (level) {
87     case AV_LOG_QUIET:
88       gst_level = GST_LEVEL_NONE;
89       break;
90     case AV_LOG_ERROR:
91       gst_level = GST_LEVEL_ERROR;
92       break;
93     case AV_LOG_INFO:
94       gst_level = GST_LEVEL_INFO;
95       break;
96     case AV_LOG_DEBUG:
97       gst_level = GST_LEVEL_DEBUG;
98       break;
99     default:
100       gst_level = GST_LEVEL_INFO;
101       break;
102   }
103
104   /* remove trailing newline as it gets already appended by the logger */
105   if (fmt[len - 1] == '\n') {
106     fmt2 = g_strdup (fmt);
107     fmt2[len - 1] = '\0';
108   }
109
110   gst_debug_log_valist (ffmpeg_debug, gst_level, "", "", 0, NULL,
111       fmt2 ? fmt2 : fmt, vl);
112
113   g_free (fmt2);
114 }
115 #endif
116
117 static gboolean
118 plugin_init (GstPlugin * plugin)
119 {
120   GST_DEBUG_CATEGORY_INIT (ffmpeg_debug, "libav", 0, "libav elements");
121 #ifndef GST_DISABLE_GST_DEBUG
122
123   av_log_set_callback (gst_ffmpeg_log_callback);
124 #endif
125
126   gst_ffmpeg_init_pix_fmt_info ();
127
128   av_register_all ();
129
130   gst_ffmpegaudenc_register (plugin);
131   gst_ffmpegvidenc_register (plugin);
132   gst_ffmpegauddec_register (plugin);
133   gst_ffmpegviddec_register (plugin);
134   gst_ffmpegdemux_register (plugin);
135   gst_ffmpegmux_register (plugin);
136   gst_ffmpegdeinterlace_register (plugin);
137 #if 0
138   gst_ffmpegscale_register (plugin);
139 #endif
140 #if 0
141   gst_ffmpegaudioresample_register (plugin);
142 #endif
143
144   /* Now we can return the pointer to the newly created Plugin object. */
145   return TRUE;
146 }
147
148 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
149     GST_VERSION_MINOR,
150     libav,
151     "All libav codecs and formats (" LIBAV_SOURCE ")",
152     plugin_init, PACKAGE_VERSION,
153 #ifdef GST_LIBAV_ENABLE_LGPL
154     "LGPL",
155 #else
156     "GPL",
157 #endif
158     "libav", "http://www.libav.org")