]> git.sesse.net Git - vlc/blob - modules/demux/util/id3tag.c
* Show meta-information separately
[vlc] / modules / demux / util / 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 <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc/intf.h>
32 #include <vlc/input.h>
33
34 #include <sys/types.h>
35
36 #include "vlc_meta.h"
37
38 #include <id3tag.h>
39 #include "id3genres.h"
40
41 /*****************************************************************************
42  * Local prototypes
43  *****************************************************************************/
44 static int  ParseID3Tags ( vlc_object_t * );
45
46 /*****************************************************************************
47  * Module descriptor
48  *****************************************************************************/
49 vlc_module_begin();
50 set_description( _("ID3 tag parser using libid3tag" ) );
51 set_capability( "id3", 70 );
52 set_callbacks( ParseID3Tags, NULL );
53 vlc_module_end();
54
55 /*****************************************************************************
56  * Definitions of structures  and functions used by this plugins
57  *****************************************************************************/
58
59 /*****************************************************************************
60  * ParseID3Tag : parse an id3tag into the info structures
61  *****************************************************************************/
62 static void ParseID3Tag( demux_t *p_demux, uint8_t *p_data, int i_size )
63 {
64     struct id3_tag   *p_id3_tag;
65     struct id3_frame *p_frame;
66     int i = 0;
67
68     p_id3_tag = id3_tag_parse( p_data, i_size );
69     if( !p_id3_tag ) return;
70
71     if( !p_demux->p_private ) p_demux->p_private = (void *)vlc_meta_New();
72
73     while( ( p_frame = id3_tag_findframe( p_id3_tag , "T", i ) ) )
74     {
75         int i_strings = id3_field_getnstrings( &p_frame->fields[1] );
76
77         while( i_strings > 0 )
78         {
79             char *psz_temp = id3_ucs4_utf8duplicate(
80                 id3_field_getstrings( &p_frame->fields[1], --i_strings ) );
81
82             if( !strcmp( p_frame->id, ID3_FRAME_GENRE ) )
83             {
84                 char *psz_endptr;
85                 int i_genre = strtol( psz_temp, &psz_endptr, 10 );
86
87                 if( psz_temp != psz_endptr &&
88                     i_genre >= 0 && i_genre < NUM_GENRES )
89                 {
90                     vlc_meta_Add( (vlc_meta_t *)p_demux->p_private,
91                                   VLC_META_GENRE, ppsz_genres[atoi(psz_temp)]);
92                 }
93                 else
94                 {
95                     /* Unknown genre */
96                     vlc_meta_Add( (vlc_meta_t *)p_demux->p_private,
97                                   VLC_META_GENRE, psz_temp );
98                 }
99             }
100             else if( !strcmp(p_frame->id, ID3_FRAME_TITLE ) )
101             {
102                 vlc_meta_Add( (vlc_meta_t *)p_demux->p_private,
103                               VLC_META_TITLE, psz_temp );
104             }
105             else if( !strcmp(p_frame->id, ID3_FRAME_ARTIST ) )
106             {
107                 vlc_meta_Add( (vlc_meta_t *)p_demux->p_private,
108                               VLC_META_ARTIST, psz_temp );
109             }
110             else if( !strcmp(p_frame->id, ID3_FRAME_YEAR ) )
111             {
112                 vlc_meta_Add( (vlc_meta_t *)p_demux->p_private,
113                               VLC_META_DATE, psz_temp );
114             }
115             else if( !strcmp(p_frame->id, ID3_FRAME_COMMENT ) )
116             {
117                 vlc_meta_Add( (vlc_meta_t *)p_demux->p_private,
118                               VLC_META_DESCRIPTION, psz_temp );
119             }
120             else if( strstr( (char*)p_frame->description, "Copyright" ) )
121             {
122                 vlc_meta_Add( (vlc_meta_t *)p_demux->p_private,
123                               VLC_META_COPYRIGHT, psz_temp );
124             }
125             else if( strstr( (char*)p_frame->description, "Publisher" ) )
126             {
127                 vlc_meta_Add( (vlc_meta_t *)p_demux->p_private,
128                               VLC_META_PUBLISHER, psz_temp );
129             }
130             else
131             {
132                 /* Unknown meta info */
133                 vlc_meta_Add( (vlc_meta_t *)p_demux->p_private,
134                               (char *)p_frame->description, psz_temp );
135             }
136             free( psz_temp );
137         }
138         i++;
139     }
140     id3_tag_delete( p_id3_tag );
141 }
142
143 /*****************************************************************************
144  * ParseID3Tags: check if ID3 tags at common locations. Parse them and skip it
145  * if it's at the start of the file
146  ****************************************************************************/
147 static int ParseID3Tags( vlc_object_t *p_this )
148 {
149     demux_t *p_demux = (demux_t *)p_this;
150     uint8_t *p_peek;
151     vlc_bool_t b_seekable;
152     int64_t i_init, i_pos;
153     int i_size;
154
155     p_demux->p_private = NULL;
156
157     msg_Dbg( p_demux, "checking for ID3 tag" );
158
159     stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
160     if( !b_seekable ) return VLC_SUCCESS;
161
162     i_init = stream_Tell( p_demux->s );
163
164     /*
165      * Look for a ID3v1 tag at the end of the file
166      */
167     i_init = stream_Tell( p_demux->s );
168     i_pos = stream_Size( p_demux->s );
169
170     while( i_pos > 128 ) /* while used so we can break; */
171     {
172         stream_Seek( p_demux->s, i_pos - 128 );
173
174         /* get 10 byte id3 header */
175         if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 ) break;
176
177         i_size = id3_tag_query( p_peek, 10 );
178         if( i_size == 128 )
179         {
180             /* peek the entire tag */
181             if( stream_Peek( p_demux->s, &p_peek, i_size ) < i_size ) break;
182
183             msg_Dbg( p_demux, "found ID3v1 tag" );
184             ParseID3Tag( p_demux, p_peek, i_size );
185         }
186
187         /* look for ID3v2.4 tag at end of file */
188         /* get 10 byte ID3 footer */
189         if( stream_Peek( p_demux->s, &p_peek, 128 ) < 128 ) break;
190
191         i_size = id3_tag_query( p_peek + 118, 10 );
192         if( i_size < 0  && i_pos > -i_size )
193         {
194             /* id3v2.4 footer found */
195             stream_Seek( p_demux->s , i_pos + i_size );
196             /* peek the entire tag */
197             if( stream_Peek( p_demux->s, &p_peek, i_size ) < i_size ) break;
198
199             msg_Dbg( p_demux, "found ID3v2 tag at end of file" );
200             ParseID3Tag( p_demux, p_peek, i_size );
201         }
202         break;
203     }
204
205     /*
206      * Get 10 byte id3 header
207      */
208     stream_Seek( p_demux->s, 0 );
209     if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 ) goto end;
210
211     if( (i_size = id3_tag_query( p_peek, 10 )) <= 0 ) goto end;
212
213     if( stream_Peek( p_demux->s, &p_peek, i_size ) < i_size ) goto end;
214
215     msg_Dbg( p_demux, "found ID3v2 tag" );
216     ParseID3Tag( p_demux, p_peek, i_size );
217
218  end:
219     stream_Seek( p_demux->s, i_init );
220     return VLC_SUCCESS;
221 }