vorbis.h

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * vorbis.h: Vorbis Comment parser
00003  *****************************************************************************
00004  * Copyright (C) 2008 the VideoLAN team
00005  * $Id: 21fbb353b98df72b0029c047f94490fb8a6787c4 $
00006  *
00007  * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
00022  *****************************************************************************/
00023 
00024 #include <vlc_charset.h>
00025 
00026 static inline void vorbis_ParseComment( vlc_meta_t **pp_meta, const uint8_t *p_data, int i_data )
00027 {
00028     int n;
00029     int i_comment;
00030     if( i_data < 8 )
00031         return;
00032 
00033 #define RM(x) do { i_data -= (x); p_data += (x); } while(0)
00034     n = GetDWLE(p_data); RM(4);
00035     if( n < 0 || n > i_data )
00036         return;
00037 #if 0
00038     if( n > 0 )
00039     {
00040         /* TODO report vendor string ? */
00041         char *psz_vendor = psz_vendor = strndup( p_data, n );
00042         free( psz_vendor );
00043     }
00044 #endif
00045     RM(n);
00046 
00047     if( i_data < 4 )
00048         return;
00049 
00050     i_comment = GetDWLE(p_data); RM(4);
00051     if( i_comment <= 0 )
00052         return;
00053 
00054     /* */
00055     vlc_meta_t *p_meta = *pp_meta;
00056     if( !p_meta )
00057         *pp_meta = p_meta = vlc_meta_New();
00058     if( !p_meta )
00059         return;
00060 
00061     for( ; i_comment > 0; i_comment-- )
00062     {
00063         char *psz;
00064         if( i_data < 4 )
00065             break;
00066         n = GetDWLE(p_data); RM(4);
00067         if( n > i_data )
00068             break;
00069         if( n <= 0 )
00070             continue;
00071 
00072         psz = strndup( (const char*)p_data, n );
00073         RM(n);
00074 
00075         EnsureUTF8( psz );
00076 
00077 #define IF_EXTRACT(txt,var) \
00078     if( !strncasecmp(psz, txt, strlen(txt)) ) \
00079     { \
00080         const char *oldval = vlc_meta_Get( p_meta, vlc_meta_ ## var ); \
00081         if( oldval ) \
00082         { \
00083             char * newval; \
00084             if( asprintf( &newval, "%s,%s", oldval, &psz[strlen(txt)] ) == -1 ) \
00085                 newval = NULL; \
00086             vlc_meta_Set( p_meta, vlc_meta_ ## var, newval ); \
00087             free( newval ); \
00088         } \
00089         else \
00090             vlc_meta_Set( p_meta, vlc_meta_ ## var, &psz[strlen(txt)] ); \
00091     }
00092         IF_EXTRACT("TITLE=", Title )
00093         else IF_EXTRACT("ALBUM=", Album )
00094         else IF_EXTRACT("TRACKNUMBER=", TrackNumber )
00095         else IF_EXTRACT("ARTIST=", Artist )
00096         else IF_EXTRACT("COPYRIGHT=", Copyright )
00097         else IF_EXTRACT("DESCRIPTION=", Description )
00098         else IF_EXTRACT("GENRE=", Genre )
00099         else IF_EXTRACT("DATE=", Date )
00100         else if( strchr( psz, '=' ) )
00101         {
00102             /* generic (PERFORMER/LICENSE/ORGANIZATION/LOCATION/CONTACT/ISRC,
00103              * undocumented tags and replay gain ) */
00104             char *p = strchr( psz, '=' );
00105             *p++ = '\0';
00106             vlc_meta_AddExtra( p_meta, psz, p );
00107         }
00108 #undef IF_EXTRACT
00109         free( psz );
00110     }
00111 #undef RM
00112 }
00113 

Generated on Tue May 25 08:04:56 2010 for VLC by  doxygen 1.5.6