]> git.sesse.net Git - vlc/blob - modules/codec/ffmpeg/avformat.c
Some more seperation of the different ffmpeg based modules. They're now completely...
[vlc] / modules / codec / ffmpeg / avformat.c
1 /*****************************************************************************
2  * avformat.c: demuxer and muxer using libavformat library
3  *****************************************************************************
4  * Copyright (C) 1999-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_codec.h>
35
36 /* ffmpeg header */
37 #define HAVE_MMX 1
38 #ifdef HAVE_LIBAVCODEC_AVCODEC_H
39 #   include <libavcodec/avcodec.h>
40 #elif defined(HAVE_FFMPEG_AVCODEC_H)
41 #   include <ffmpeg/avcodec.h>
42 #else
43 #   include <avcodec.h>
44 #endif
45
46 #if LIBAVCODEC_BUILD < 5000
47 #   error You must have a libavcodec >= 5000 (get CVS)
48 #endif
49
50 #include "avformat.h"
51
52 /*****************************************************************************
53  * Module descriptor
54  *****************************************************************************/
55 vlc_module_begin();
56     add_shortcut( "ffmpeg" );
57     set_category( CAT_INPUT );
58     set_subcategory( SUBCAT_INPUT_SCODEC );
59     set_description( N_("FFmpeg demuxer" ) );
60     set_capability( "demux", 2 );
61     set_callbacks( OpenDemux, CloseDemux );
62
63 #ifdef ENABLE_SOUT
64     /* mux submodule */
65     add_submodule();
66     set_description( N_("FFmpeg muxer" ) );
67     set_capability( "sout mux", 2 );
68     add_string( "ffmpeg-mux", NULL, NULL, MUX_TEXT,
69                 MUX_LONGTEXT, true );
70     set_callbacks( OpenMux, CloseMux );
71 #endif
72 vlc_module_end();
73
74 /*****************************************************************************
75  *
76  *****************************************************************************/
77 void LibavcodecCallback( void *p_opaque, int i_level,
78                              const char *psz_format, va_list va )
79 {
80     int i_vlc_level;
81     AVCodecContext *p_avctx = (AVCodecContext *)p_opaque;
82     AVClass *p_avc;
83     vlc_object_t *p_this;
84     char *psz_new_format;
85     const char *psz_item_name;
86
87     p_avc = p_avctx ? p_avctx->av_class : 0;
88
89 #define cln p_avc->class_name
90     /* Make sure we can get p_this back */
91     if( !p_avctx || !p_avc || !cln ||
92         cln[0]!='A' || cln[1]!='V' || cln[2]!='C' || cln[3]!='o' ||
93         cln[4]!='d' || cln[5]!='e' || cln[6]!='c' )
94     {
95         if( i_level == AV_LOG_ERROR ) vfprintf( stderr, psz_format, va );
96         return;
97     }
98 #undef cln
99
100     p_this = (vlc_object_t *)p_avctx->opaque;
101
102     switch( i_level )
103     {
104     case AV_LOG_QUIET:
105         i_vlc_level = VLC_MSG_ERR;
106         break;
107     case AV_LOG_ERROR:
108         i_vlc_level = VLC_MSG_WARN;
109         break;
110     case AV_LOG_INFO:
111         i_vlc_level = VLC_MSG_DBG;
112         break;
113     case AV_LOG_DEBUG:
114         /* Print debug messages if they were requested */
115         if( p_avctx->debug ) vfprintf( stderr, psz_format, va );
116         return;
117     default:
118         return;
119     }
120
121     psz_item_name = p_avc->item_name(p_opaque);
122     psz_new_format = malloc( strlen(psz_format) + strlen(psz_item_name)
123                               + 18 + 5 );
124     snprintf( psz_new_format, strlen(psz_format) + strlen(psz_item_name)
125               + 18 + 5, "%s (%s@%p)", psz_format, p_avc->item_name(p_opaque), p_opaque );
126     msg_GenericVa( p_this, i_vlc_level,
127                     MODULE_STRING, psz_new_format, va );
128     free( psz_new_format );
129 }
130
131 void InitLibavcodec( vlc_object_t *p_object )
132 {
133     static int b_ffmpeginit = 0;
134     vlc_mutex_t *lock = var_AcquireMutex( "avcodec" );
135
136     /* *** init ffmpeg library (libavcodec) *** */
137     if( !b_ffmpeginit )
138     {
139         avcodec_init();
140         avcodec_register_all();
141         av_log_set_callback( LibavcodecCallback );
142         b_ffmpeginit = 1;
143
144         msg_Dbg( p_object, "libavcodec initialized (interface %d )",
145                  LIBAVCODEC_VERSION_INT );
146     }
147     else
148     {
149         msg_Dbg( p_object, "libavcodec already initialized" );
150     }
151
152     vlc_mutex_unlock( lock );
153 }