]> git.sesse.net Git - vlc/blob - modules/demux/vorbis.h
Parse meta data at ogg level.
[vlc] / modules / demux / vorbis.h
1 /*****************************************************************************
2  * vorbis.h: Vorbis Comment parser
3  *****************************************************************************
4  * Copyright (C) 2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 static inline void vorbis_ParseComment( vlc_meta_t **pp_meta, const uint8_t *p_data, int i_data )
25 {
26     int n;
27     int i_comment;
28     if( i_data < 8 )
29         return;
30
31 #define RM(x) do { i_data -= (x); p_data += (x); } while(0)
32     n = GetDWLE(p_data); RM(4);
33     if( n < 0 || n > i_data )
34         return;
35 #if 0
36     if( n > 0 )
37     {
38         /* TODO report vendor string ? */
39         char *psz_vendor = psz_vendor = strndup( p_data, n );
40         free( psz_vendor );
41     }
42 #endif
43     RM(n);
44
45     if( i_data < 4 )
46         return;
47
48     i_comment = GetDWLE(p_data); RM(4);
49     if( i_comment <= 0 )
50         return;
51
52     /* */
53     vlc_meta_t *p_meta = *pp_meta;
54     if( !p_meta )
55         *pp_meta = p_meta = vlc_meta_New();
56     if( !p_meta )
57         return;
58
59     for( ; i_comment > 0; i_comment-- )
60     {
61         char *psz;
62         if( i_data < 4 )
63             break;
64         n = GetDWLE(p_data); RM(4);
65         if( n > i_data )
66             break;
67         if( n <= 0 )
68             continue;
69
70         psz = strndup( (const char*)p_data, n );
71         RM(n);
72
73         EnsureUTF8( psz );
74
75 #define IF_EXTRACT(txt,var) \
76     if( !strncasecmp(psz, txt, strlen(txt)) ) \
77     { \
78         const char *oldval = vlc_meta_Get( p_meta, vlc_meta_ ## var ); \
79         if( oldval ) \
80         { \
81             char * newval; \
82             if( asprintf( &newval, "%s,%s", oldval, &psz[strlen(txt)] ) == -1 ) \
83                 newval = NULL; \
84             vlc_meta_Set( p_meta, vlc_meta_ ## var, newval ); \
85             free( newval ); \
86         } \
87         else \
88             vlc_meta_Set( p_meta, vlc_meta_ ## var, &psz[strlen(txt)] ); \
89     }
90         IF_EXTRACT("TITLE=", Title )
91         else IF_EXTRACT("ALBUM=", Album )
92         else IF_EXTRACT("TRACKNUMBER=", TrackNumber )
93         else IF_EXTRACT("ARTIST=", Artist )
94         else IF_EXTRACT("COPYRIGHT=", Copyright )
95         else IF_EXTRACT("DESCRIPTION=", Description )
96         else IF_EXTRACT("GENRE=", Genre )
97         else IF_EXTRACT("DATE=", Date )
98         else if( strchr( psz, '=' ) )
99         {
100             /* generic (PERFORMER/LICENSE/ORGANIZATION/LOCATION/CONTACT/ISRC,
101              * undocumented tags and replay gain ) */
102             char *p = strchr( psz, '=' );
103             *p++ = '\0';
104             vlc_meta_AddExtra( p_meta, psz, p );
105         }
106 #undef IF_EXTRACT
107         free( psz );
108     }
109 #undef RM
110 }
111