]> git.sesse.net Git - vlc/blob - modules/meta_engine/id3tag.c
ec7c36bbf8d6b9cb88aea975f91784276574d639
[vlc] / modules / meta_engine / id3tag.c
1 /*****************************************************************************
2  * id3tag.c: id3 tag parser/skipper based on libid3tag
3  *****************************************************************************
4  * Copyright (C) 2002-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Sigmund Augdal Helberg <dnumgis@videolan.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 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <config.h>
28
29 #include <stdlib.h>                                      /* malloc(), free() */
30 #include <string.h>
31
32 #include <vlc/vlc.h>
33 #include <vlc_interface.h>
34 #include <vlc_demux.h>
35 #include <vlc_playlist.h>
36
37 #include <sys/types.h>
38
39 #include <vlc_meta.h>
40
41 #include <id3tag.h>
42 #include "id3genres.h"
43
44 /*****************************************************************************
45  * Local prototypes
46  *****************************************************************************/
47 static int  ParseID3Tags ( vlc_object_t * );
48
49 /*****************************************************************************
50  * Module descriptor
51  *****************************************************************************/
52 vlc_module_begin();
53     set_description( _("ID3 tags parser" ) );
54     set_capability( "meta reader", 70 );
55     set_callbacks( ParseID3Tags, NULL );
56 vlc_module_end();
57
58 /*****************************************************************************
59  * Definitions of structures  and functions used by this plugins
60  *****************************************************************************/
61
62 /*****************************************************************************
63  * ParseID3Tag : parse an id3tag into the info structures
64  *****************************************************************************/
65 static void ParseID3Tag( demux_t *p_demux, uint8_t *p_data, int i_size )
66 {
67     struct id3_tag   *p_id3_tag;
68     struct id3_frame *p_frame;
69     int i = 0;
70
71     p_id3_tag = id3_tag_parse( p_data, i_size );
72     if( !p_id3_tag ) return;
73
74     if( !p_demux->p_private ) p_demux->p_private = (void *)vlc_meta_New();
75
76     vlc_meta_t *p_meta = (vlc_meta_t *)(p_demux->p_private);
77 #define ID_IS( a ) (!strcmp(  p_frame->id, a ))
78 #define DESCR_IS( a) strstr( (char*)p_frame->description, a )
79
80     while ( ( p_frame = id3_tag_findframe( p_id3_tag, "UFID", i ) ) )
81     {
82         char *psz_owner = id3_field_getlatin1( &p_frame->fields[0] );
83
84         if( !strncmp( psz_owner, "http://musicbrainz.org", 22 ) )
85         {
86             id3_byte_t const * p_ufid;
87             id3_length_t i_ufidlen;
88
89             p_ufid = (id3_byte_t const *)
90                         id3_field_getbinarydata(
91                                 &p_frame->fields[1],
92                                 &i_ufidlen );
93             char *psz_ufid = strndup( p_ufid, i_ufidlen );
94
95             vlc_meta_SetTrackID( p_meta, psz_ufid );
96             free( psz_ufid );
97         }
98         i++;
99     }
100 #if 0 //not used
101     i = 0;
102
103     while( ( p_frame = id3_tag_findframe( p_id3_tag, "TXXX", i ) ) )
104     {
105         char *psz_desc = id3_ucs4_latin1duplicate(
106                 id3_field_getstring( &p_frame->fields[1] ) );
107
108         if ( ! strncmp( psz_desc, "MusicBrainz Artist Id", 21 ) )
109         {
110             char *psz_artistid = id3_ucs4_latin1duplicate(
111                     id3_field_getstring( &p_frame->fields[2] ) );
112             vlc_meta_SetArtistID( p_meta, psz_artistid );
113             free( psz_artistid );
114         }
115
116         if ( ! strncmp( psz_desc, "MusicBrainz Album Id", 20 ) )
117         {
118             char *psz_albumid = id3_ucs4_latin1duplicate(
119                     id3_field_getstring( &p_frame->fields[2] ) );
120             vlc_meta_SetAlbumID( p_meta, psz_albumid );
121             free( psz_albumid );
122         }
123
124         free( psz_desc );
125         i++;
126     }
127 #endif
128     i = 0;
129
130     while( ( p_frame = id3_tag_findframe( p_id3_tag , "T", i ) ) )
131     {
132         int i_strings = id3_field_getnstrings( &p_frame->fields[1] );
133
134         while( i_strings > 0 )
135         {
136             char *psz_temp = id3_ucs4_utf8duplicate(
137                 id3_field_getstrings( &p_frame->fields[1], --i_strings ) );
138
139             if( ID_IS( ID3_FRAME_GENRE ) )
140             {
141                 char *psz_endptr;
142                 int i_genre = strtol( psz_temp, &psz_endptr, 10 );
143
144                 if( psz_temp != psz_endptr &&
145                     i_genre >= 0 && i_genre < NUM_GENRES )
146                 {
147                     vlc_meta_SetGenre( p_meta, ppsz_genres[atoi(psz_temp)]);
148                 }
149                 else
150                 {
151                     /* Unknown genre */
152                     vlc_meta_SetGenre( p_meta,psz_temp );
153                 }
154             }
155             else if( ID_IS( ID3_FRAME_TITLE ) )
156             {
157                 vlc_meta_SetTitle( p_meta, psz_temp );
158             }
159             else if( ID_IS( ID3_FRAME_ARTIST ) )
160             {
161                 vlc_meta_SetArtist( p_meta, psz_temp );
162             }
163             else if( ID_IS( ID3_FRAME_YEAR ) )
164             {
165                 vlc_meta_SetDate( p_meta, psz_temp );
166             }
167             else if( ID_IS( ID3_FRAME_COMMENT ) )
168             {
169                 vlc_meta_SetDescription( p_meta, psz_temp );
170             }
171             else if( DESCR_IS( "Copyright" ) )
172             {
173                 vlc_meta_SetCopyright( p_meta, psz_temp );
174             }
175             else if( DESCR_IS( "Publisher" ) )
176             {
177                 vlc_meta_SetPublisher( p_meta, psz_temp );
178             }
179             else if( DESCR_IS( "Track number/position in set" ) )
180             {
181                 vlc_meta_SetTracknum( p_meta, psz_temp );
182             }
183             else if( DESCR_IS( "Album/movie/show title" ) )
184             {
185                 vlc_meta_SetAlbum( p_meta, psz_temp );
186             }
187             else if( DESCR_IS( "Encoded by" ) )
188             {
189                 vlc_meta_SetEncodedBy( p_meta, psz_temp );
190             }
191             else if( ID_IS ( "APIC" ) )
192             {
193                 msg_Dbg( p_demux, "** Has APIC **" );
194             }
195             else if( p_frame->description )
196             { /* Unhandled meta*/
197                 msg_Warn( p_demux, "Fixme: unhandled ID3 metatag, %s", p_frame->description );
198             }
199             free( psz_temp );
200         }
201         i++;
202     }
203     id3_tag_delete( p_id3_tag );
204 }
205
206 /*****************************************************************************
207  * ParseID3Tags: check if ID3 tags at common locations. Parse them and skip it
208  * if it's at the start of the file
209  ****************************************************************************/
210 static int ParseID3Tags( vlc_object_t *p_this )
211 {
212     demux_t *p_demux = (demux_t *)p_this;
213     uint8_t *p_peek;
214     vlc_bool_t b_seekable;
215     int64_t i_init, i_pos;
216     int i_size;
217
218     p_demux->p_private = NULL;
219
220     msg_Dbg( p_demux, "checking for ID3 tag" );
221
222     stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
223     if( !b_seekable ) return VLC_SUCCESS;
224
225     i_init = stream_Tell( p_demux->s );
226
227     /*
228      * Look for a ID3v1 tag at the end of the file
229      */
230     i_init = stream_Tell( p_demux->s );
231     i_pos = stream_Size( p_demux->s );
232
233     while( i_pos > 128 ) /* while used so we can break; */
234     {
235         stream_Seek( p_demux->s, i_pos - 128 );
236
237         /* get 10 byte id3 header */
238         if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 ) break;
239
240         i_size = id3_tag_query( p_peek, 10 );
241         if( i_size == 128 )
242         {
243             /* peek the entire tag */
244             if( stream_Peek( p_demux->s, &p_peek, i_size ) < i_size ) break;
245
246             msg_Dbg( p_demux, "found ID3v1 tag" );
247             ParseID3Tag( p_demux, p_peek, i_size );
248         }
249
250         /* look for ID3v2.4 tag at end of file */
251         /* get 10 byte ID3 footer */
252         if( stream_Peek( p_demux->s, &p_peek, 128 ) < 128 ) break;
253
254         i_size = id3_tag_query( p_peek + 118, 10 );
255         if( i_size < 0  && i_pos > -i_size )
256         {
257             /* id3v2.4 footer found */
258             stream_Seek( p_demux->s , i_pos + i_size );
259             /* peek the entire tag */
260             if( stream_Peek( p_demux->s, &p_peek, i_size ) < i_size ) break;
261
262             msg_Dbg( p_demux, "found ID3v2 tag at end of file" );
263             ParseID3Tag( p_demux, p_peek, i_size );
264         }
265         break;
266     }
267
268     /*
269      * Get 10 byte id3 header
270      */
271     stream_Seek( p_demux->s, 0 );
272     if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 ) goto end;
273
274     if( (i_size = id3_tag_query( p_peek, 10 )) <= 0 ) goto end;
275
276     if( stream_Peek( p_demux->s, &p_peek, i_size ) < i_size ) goto end;
277
278     msg_Dbg( p_demux, "found ID3v2 tag" );
279     ParseID3Tag( p_demux, p_peek, i_size );
280
281  end:
282     stream_Seek( p_demux->s, i_init );
283     return VLC_SUCCESS;
284 }