]> git.sesse.net Git - vlc/blob - modules/demux/vorbis.h
Qt: use a QStackedWidget for the artContainer in the playlist
[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 #include <vlc_charset.h>
25
26 static inline void vorbis_ParseComment( vlc_meta_t **pp_meta, const uint8_t *p_data, int i_data )
27 {
28     int n;
29     int i_comment;
30     if( i_data < 8 )
31         return;
32
33 #define RM(x) do { i_data -= (x); p_data += (x); } while(0)
34     n = GetDWLE(p_data); RM(4);
35     if( n < 0 || n > i_data )
36         return;
37 #if 0
38     if( n > 0 )
39     {
40         /* TODO report vendor string ? */
41         char *psz_vendor = psz_vendor = strndup( p_data, n );
42         free( psz_vendor );
43     }
44 #endif
45     RM(n);
46
47     if( i_data < 4 )
48         return;
49
50     i_comment = GetDWLE(p_data); RM(4);
51     if( i_comment <= 0 )
52         return;
53
54     /* */
55     vlc_meta_t *p_meta = *pp_meta;
56     if( !p_meta )
57         *pp_meta = p_meta = vlc_meta_New();
58     if( !p_meta )
59         return;
60
61     bool hasTitle = false;
62     bool hasAlbum = false;
63     bool hasTrackNumber = false;
64     bool hasArtist = false;
65     bool hasCopyright = false;
66     bool hasDescription = false;
67     bool hasGenre = false;
68     bool hasDate = false;
69
70     for( ; i_comment > 0; i_comment-- )
71     {
72         char *psz;
73         if( i_data < 4 )
74             break;
75         n = GetDWLE(p_data); RM(4);
76         if( n > i_data )
77             break;
78         if( n <= 0 )
79             continue;
80
81         psz = strndup( (const char*)p_data, n );
82         RM(n);
83
84         EnsureUTF8( psz );
85
86 #define IF_EXTRACT(txt,var) \
87     if( !strncasecmp(psz, txt, strlen(txt)) ) \
88     { \
89         const char *oldval = vlc_meta_Get( p_meta, vlc_meta_ ## var ); \
90         if( oldval && has##var) \
91         { \
92             char * newval; \
93             if( asprintf( &newval, "%s,%s", oldval, &psz[strlen(txt)] ) == -1 ) \
94                 newval = NULL; \
95             vlc_meta_Set( p_meta, vlc_meta_ ## var, newval ); \
96             free( newval ); \
97         } \
98         else \
99             vlc_meta_Set( p_meta, vlc_meta_ ## var, &psz[strlen(txt)] ); \
100         has##var = true; \
101     }
102         IF_EXTRACT("TITLE=", Title )
103         else IF_EXTRACT("ALBUM=", Album )
104         else IF_EXTRACT("TRACKNUMBER=", TrackNumber )
105         else IF_EXTRACT("ARTIST=", Artist )
106         else IF_EXTRACT("COPYRIGHT=", Copyright )
107         else IF_EXTRACT("DESCRIPTION=", Description )
108         else IF_EXTRACT("GENRE=", Genre )
109         else IF_EXTRACT("DATE=", Date )
110         else if( strchr( psz, '=' ) )
111         {
112             /* generic (PERFORMER/LICENSE/ORGANIZATION/LOCATION/CONTACT/ISRC,
113              * undocumented tags and replay gain ) */
114             char *p = strchr( psz, '=' );
115             *p++ = '\0';
116             vlc_meta_AddExtra( p_meta, psz, p );
117         }
118 #undef IF_EXTRACT
119         free( psz );
120     }
121 #undef RM
122 }
123