]> git.sesse.net Git - vlc/blob - modules/demux/vorbis.h
Split FLAC picture parsing from flac.c
[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 input_attachment_t* ParseFlacPicture( const uint8_t *p_data, int i_data, int i_attachments, int *i_type )
27 {
28     int i_len;
29     char *psz_mime = NULL;
30     char psz_name[128];
31     char *psz_description = NULL;
32     input_attachment_t *p_attachment = NULL;
33
34     if( i_data < 4 + 3*4 )
35         return NULL;
36 #define RM(x) do { i_data -= (x); p_data += (x); } while(0)
37
38     *i_type = GetDWBE( p_data ); RM(4);
39     i_len = GetDWBE( p_data ); RM(4);
40
41     if( i_len < 0 || i_data < i_len + 4 )
42         goto error;
43     psz_mime = strndup( (const char*)p_data, i_len ); RM(i_len);
44     i_len = GetDWBE( p_data ); RM(4);
45     if( i_len < 0 || i_data < i_len + 4*4 + 4)
46         goto error;
47     psz_description = strndup( (const char*)p_data, i_len ); RM(i_len);
48     EnsureUTF8( psz_description );
49     RM(4*4);
50     i_len = GetDWBE( p_data ); RM(4);
51     if( i_len < 0 || i_len > i_data )
52         goto error;
53
54     /* printf( "Picture type=%d mime=%s description='%s' file length=%d\n",
55              *i_type, psz_mime, psz_description, i_len ); */
56
57     snprintf( psz_name, sizeof(psz_name), "picture%d", i_attachments );
58     if( !strcasecmp( psz_mime, "image/jpeg" ) )
59         strcat( psz_name, ".jpg" );
60     else if( !strcasecmp( psz_mime, "image/png" ) )
61         strcat( psz_name, ".png" );
62
63     p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
64             psz_description, p_data, i_data );
65
66 error:
67     free( psz_mime );
68     free( psz_description );
69     return p_attachment;
70 }
71
72 static inline void vorbis_ParseComment( vlc_meta_t **pp_meta, const uint8_t *p_data, int i_data )
73 {
74     int n;
75     int i_comment;
76     if( i_data < 8 )
77         return;
78
79     n = GetDWLE(p_data); RM(4);
80     if( n < 0 || n > i_data )
81         return;
82 #if 0
83     if( n > 0 )
84     {
85         /* TODO report vendor string ? */
86         char *psz_vendor = psz_vendor = strndup( p_data, n );
87         free( psz_vendor );
88     }
89 #endif
90     RM(n);
91
92     if( i_data < 4 )
93         return;
94
95     i_comment = GetDWLE(p_data); RM(4);
96     if( i_comment <= 0 )
97         return;
98
99     /* */
100     vlc_meta_t *p_meta = *pp_meta;
101     if( !p_meta )
102         *pp_meta = p_meta = vlc_meta_New();
103     if( !p_meta )
104         return;
105
106     bool hasTitle = false;
107     bool hasAlbum = false;
108     bool hasTrackNumber = false;
109     bool hasArtist = false;
110     bool hasCopyright = false;
111     bool hasDescription = false;
112     bool hasGenre = false;
113     bool hasDate = false;
114     bool hasPublisher = false;
115
116     for( ; i_comment > 0; i_comment-- )
117     {
118         char *psz;
119         if( i_data < 4 )
120             break;
121         n = GetDWLE(p_data); RM(4);
122         if( n > i_data )
123             break;
124         if( n <= 0 )
125             continue;
126
127         psz = strndup( (const char*)p_data, n );
128         RM(n);
129
130         EnsureUTF8( psz );
131
132 #define IF_EXTRACT(txt,var) \
133     if( !strncasecmp(psz, txt, strlen(txt)) ) \
134     { \
135         const char *oldval = vlc_meta_Get( p_meta, vlc_meta_ ## var ); \
136         if( oldval && has##var) \
137         { \
138             char * newval; \
139             if( asprintf( &newval, "%s,%s", oldval, &psz[strlen(txt)] ) == -1 ) \
140                 newval = NULL; \
141             vlc_meta_Set( p_meta, vlc_meta_ ## var, newval ); \
142             free( newval ); \
143         } \
144         else \
145             vlc_meta_Set( p_meta, vlc_meta_ ## var, &psz[strlen(txt)] ); \
146         has##var = true; \
147     }
148         IF_EXTRACT("TITLE=", Title )
149         else IF_EXTRACT("ALBUM=", Album )
150         else IF_EXTRACT("TRACKNUMBER=", TrackNumber )
151         else IF_EXTRACT("ARTIST=", Artist )
152         else IF_EXTRACT("COPYRIGHT=", Copyright )
153         else IF_EXTRACT("ORGANIZATION=", Publisher )
154         else IF_EXTRACT("DESCRIPTION=", Description )
155         else IF_EXTRACT("GENRE=", Genre )
156         else IF_EXTRACT("DATE=", Date )
157         else if( strchr( psz, '=' ) )
158         {
159             /* generic (PERFORMER/LICENSE/ORGANIZATION/LOCATION/CONTACT/ISRC,
160              * undocumented tags and replay gain ) */
161             char *p = strchr( psz, '=' );
162             *p++ = '\0';
163             vlc_meta_AddExtra( p_meta, psz, p );
164         }
165 #undef IF_EXTRACT
166         free( psz );
167     }
168 #undef RM
169 }
170