id3tag.c 8.56 KB
Newer Older
1
/*****************************************************************************
2
 * id3tag.c: id3 tag parser/skipper based on libid3tag
3
4
 *****************************************************************************
 * Copyright (C) 2001 VideoLAN
5
 * $Id: id3tag.c,v 1.11 2003/10/20 00:01:06 hartman Exp $
6
 *
7
 * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdlib.h>                                      /* malloc(), free() */
#include <string.h>

#include <vlc/vlc.h>
31
#include <vlc/intf.h>
32
33
34
35
36
#include <vlc/input.h>

#include <sys/types.h>

#include <id3tag.h>
37
#include "id3genres.h"
38
39
40
41
42
43
44
45
46
47

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  ParseID3Tags ( vlc_object_t * );

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
vlc_module_begin();
48
49
50
set_description( _("id3 tag parser using libid3tag" ) );
set_capability( "id3", 70 );
set_callbacks( ParseID3Tags, NULL );
51
52
53
54
55
56
vlc_module_end();

/*****************************************************************************
 * Definitions of structures  and functions used by this plugins 
 *****************************************************************************/

57
/*****************************************************************************
58
 * ParseID3Tag : parse an id3tag into the info structures
59
 *****************************************************************************/
60
61
static void ParseID3Tag( input_thread_t *p_input, u8 *p_data, int i_size )
{
62
    playlist_t * p_playlist;
63
64
65
66
67
68
69
70
71
72
    struct id3_tag * p_id3_tag;
    struct id3_frame * p_frame;
    input_info_category_t * p_category;
    int i_strings;
    char * psz_temp;
    int i;
    
    p_id3_tag = id3_tag_parse( p_data, i_size );
    p_category = input_InfoCategory( p_input, "ID3" );
    i = 0;
73
    
74
75
76
77
78
    while ( ( p_frame = id3_tag_findframe( p_id3_tag , "T", i ) ) )
    {
        i_strings = id3_field_getnstrings( &p_frame->fields[1] );
        while ( i_strings > 0 )
        {
79
            psz_temp = id3_ucs4_utf8duplicate( id3_field_getstrings( &p_frame->fields[1], --i_strings ) );
80
81
82
83
84
85
86
87
88
89
90
91
92
93
            if ( !strcmp(p_frame->id, ID3_FRAME_GENRE ) )
            {
                int i_genre;
                char *psz_endptr;
                i_genre = strtol( psz_temp, &psz_endptr, 10 );
                if( psz_temp != psz_endptr && i_genre >= 0 && i_genre < NUM_GENRES )
                {
                    input_AddInfo( p_category, (char *)p_frame->description, ppsz_genres[atoi(psz_temp)]);
                }
                else
                {
                    input_AddInfo( p_category, (char *)p_frame->description, psz_temp );
                }
            }
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
            else if ( !strcmp(p_frame->id, ID3_FRAME_TITLE ) )
            {
                p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_PARENT );
                if( p_playlist )
                {
                    p_playlist->pp_items[p_playlist->i_index]->psz_name = strdup( psz_temp );
                    vlc_object_release( p_playlist );
                }
                input_AddInfo( p_category, (char *)p_frame->description, psz_temp );
            }
            else if ( !strcmp(p_frame->id, ID3_FRAME_ARTIST ) )
            {
                p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_PARENT );
                if( p_playlist )
                {
                    p_playlist->pp_items[p_playlist->i_index]->psz_author = strdup( psz_temp );
                    vlc_object_release( p_playlist );
                }
                input_AddInfo( p_category, (char *)p_frame->description, psz_temp );
            }
114
115
116
117
            else
            {
                input_AddInfo( p_category, (char *)p_frame->description, psz_temp );
            }
118
119
120
121
122
123
124
            free( psz_temp ); 
        }
        i++;
    }
    id3_tag_delete( p_id3_tag );
}

125
126
127
128
/*****************************************************************************
 * ParseID3Tags: check if ID3 tags at common locations. Parse them and skip it
 * if it's at the start of the file
 ****************************************************************************/
129
130
131
static int ParseID3Tags( vlc_object_t *p_this )
{
    input_thread_t *p_input;
132
    vlc_value_t val;
133
134
135
136
137
138
139
140
141
142
    u8  *p_peek;
    int i_size;
    int i_size2;

    if ( p_this->i_object_type != VLC_OBJECT_INPUT )
    {
        return( VLC_EGENERIC );
    }
    p_input = (input_thread_t *)p_this;

143
144
145
146
147
148
149
150
151
152
153
    var_Get( p_input, "demuxed-id3", &val );

    if( val.b_bool )
    {
        msg_Dbg( p_input, "The ID3 tag was already parsed" );
        return VLC_EGENERIC;
    }
    else
    {
        msg_Dbg( p_input, "Checking for ID3 tag" );
    }
154

155
156
    if ( p_input->stream.b_seekable &&
         p_input->stream.i_method != INPUT_METHOD_NETWORK )
157
    {        
158
159
        stream_position_t pos;

160
        /*look for a id3v1 tag at the end of the file*/
161
162
        input_Tell( p_input, &pos );
        if ( pos.i_size >128 )
163
        {
164
            input_AccessReinit( p_input );
165
            p_input->pf_seek( p_input, pos.i_size - 128 );
166
167
168
            
            /* get 10 byte id3 header */    
            if( input_Peek( p_input, &p_peek, 10 ) < 10 )
169
170
171
172
            {
                msg_Err( p_input, "cannot peek()" );
                return( VLC_EGENERIC );
            }
173
174
175
176
177
178
179
180
181
182
183
            i_size2 = id3_tag_query( p_peek, 10 );
            if ( i_size2 == 128 )
            {
                /* peek the entire tag */
                if ( input_Peek( p_input, &p_peek, i_size2 ) < i_size2 )
                {
                    msg_Err( p_input, "cannot peek()" );
                    return( VLC_EGENERIC );
                }
                ParseID3Tag( p_input, p_peek, i_size2 );
            }
184

185
            /* look for id3v2.4 tag at end of file */
186
            /* get 10 byte id3 footer */    
187
            if( input_Peek( p_input, &p_peek, 128 ) < 128 )
188
189
190
191
            {
                msg_Err( p_input, "cannot peek()" );
                return( VLC_EGENERIC );
            }
192
            i_size2 = id3_tag_query( p_peek + 118, 10 );
193
194
            if ( i_size2 < 0  && pos.i_size > -i_size2 )
            {                                        /* id3v2.4 footer found */
195
                input_AccessReinit( p_input );
196
                p_input->pf_seek( p_input, pos.i_size + i_size2 );
197
198
199
200
201
202
203
204
                /* peek the entire tag */
                if ( input_Peek( p_input, &p_peek, i_size2 ) < i_size2 )
                {
                    msg_Err( p_input, "cannot peek()" );
                    return( VLC_EGENERIC );
                }
                ParseID3Tag( p_input, p_peek, i_size2 );
            }
205
206
        }
        input_AccessReinit( p_input );    
207
        p_input->pf_seek( p_input, 0 );
208
    }
209
210
211
212
213
214
215
216
    /* get 10 byte id3 header */    
    if( input_Peek( p_input, &p_peek, 10 ) < 10 )
    {
        msg_Err( p_input, "cannot peek()" );
        return( VLC_EGENERIC );
    }

    i_size = id3_tag_query( p_peek, 10 );
217
218
    if ( i_size <= 0 )
    {
219
220
        val.b_bool = VLC_TRUE;
        var_Change( p_input, "demuxed-id3", VLC_VAR_SETVALUE, &val, NULL );
221
222
223
224
225
226
227
228
229
230
231
232
        return( VLC_SUCCESS );
    }

    /* peek the entire tag */
    if ( input_Peek( p_input, &p_peek, i_size ) < i_size )
    {
        msg_Err( p_input, "cannot peek()" );
        return( VLC_EGENERIC );
    }

    ParseID3Tag( p_input, p_peek, i_size );
    msg_Dbg( p_input, "ID3 tag found, skiping %d bytes", i_size );
233
    p_input->p_current_data += i_size; /* seek passed end of ID3 tag */
234
235
236

    val.b_bool = VLC_TRUE;
    var_Change( p_input, "demuxed-id3", VLC_VAR_SETVALUE, &val, NULL );
237
238
    return( VLC_SUCCESS );
}