id3tag.c 7.67 KB
Newer Older
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>
Sam Hocevar's avatar
Sam Hocevar committed
8
 *
9
10
11
12
 * 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.
Sam Hocevar's avatar
Sam Hocevar committed
13
 *
14
15
16
17
18
19
20
 * 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
dionoea's avatar
dionoea committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22
23
24
25
26
27
28
29
30
 *****************************************************************************/

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

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

#include <sys/types.h>

zorglub's avatar
zorglub committed
36
37
#include "vlc_meta.h"

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

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

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
vlc_module_begin();
zorglub's avatar
zorglub committed
50
51
52
    set_description( _("ID3 tags parser" ) );
    set_capability( "id3", 70 );
    set_callbacks( ParseID3Tags, NULL );
53
54
55
vlc_module_end();

/*****************************************************************************
Sam Hocevar's avatar
Sam Hocevar committed
56
 * Definitions of structures  and functions used by this plugins
57
58
 *****************************************************************************/

59
/*****************************************************************************
60
 * ParseID3Tag : parse an id3tag into the info structures
61
 *****************************************************************************/
zorglub's avatar
zorglub committed
62
static void ParseID3Tag( demux_t *p_demux, uint8_t *p_data, int i_size )
63
{
64
65
66
    struct id3_tag   *p_id3_tag;
    struct id3_frame *p_frame;
    int i = 0;
zorglub's avatar
zorglub committed
67

68
    p_id3_tag = id3_tag_parse( p_data, i_size );
69
    if( !p_id3_tag ) return;
Sam Hocevar's avatar
Sam Hocevar committed
70

71
72
    if( !p_demux->p_private ) p_demux->p_private = (void *)vlc_meta_New();

zorglub's avatar
zorglub committed
73
74
75
#define ID_IS( a ) (!strcmp(  p_frame->id, a ))
#define DESCR_IS( a) strstr( (char*)p_frame->description, a )

76
    while( ( p_frame = id3_tag_findframe( p_id3_tag , "T", i ) ) )
77
    {
78
        int i_strings = id3_field_getnstrings( &p_frame->fields[1] );
79

80
        while( i_strings > 0 )
81
        {
82
83
            vlc_meta_t *p_meta = (vlc_meta_t *)(p_demux->p_private);

84
85
86
            char *psz_temp = id3_ucs4_utf8duplicate(
                id3_field_getstrings( &p_frame->fields[1], --i_strings ) );

zorglub's avatar
zorglub committed
87
            if( ID_IS( ID3_FRAME_GENRE ) )
88
89
            {
                char *psz_endptr;
90
91
92
93
                int i_genre = strtol( psz_temp, &psz_endptr, 10 );

                if( psz_temp != psz_endptr &&
                    i_genre >= 0 && i_genre < NUM_GENRES )
94
                {
95
                    vlc_meta_SetGenre( p_meta, ppsz_genres[atoi(psz_temp)]);
96
97
98
                }
                else
                {
zorglub's avatar
zorglub committed
99
                    /* Unknown genre */
100
                    vlc_meta_SetGenre( p_meta,psz_temp );
101
102
                }
            }
zorglub's avatar
zorglub committed
103
            else if( ID_IS( ID3_FRAME_TITLE ) )
104
            {
105
                vlc_meta_SetTitle( p_meta, psz_temp );
106
            }
zorglub's avatar
zorglub committed
107
            else if( ID_IS( ID3_FRAME_ARTIST ) )
108
            {
109
                vlc_meta_SetArtist( p_meta, psz_temp );
110
            }
zorglub's avatar
zorglub committed
111
            else if( ID_IS( ID3_FRAME_YEAR ) )
zorglub's avatar
zorglub committed
112
            {
113
                vlc_meta_SetDate( p_meta, psz_temp );
zorglub's avatar
zorglub committed
114
            }
zorglub's avatar
zorglub committed
115
            else if( ID_IS( ID3_FRAME_COMMENT ) )
zorglub's avatar
zorglub committed
116
            {
117
                vlc_meta_SetDescription( p_meta, psz_temp );
zorglub's avatar
zorglub committed
118
            }
zorglub's avatar
zorglub committed
119
            else if( DESCR_IS( "Copyright" ) )
zorglub's avatar
zorglub committed
120
            {
121
                vlc_meta_SetCopyright( p_meta, psz_temp );
zorglub's avatar
zorglub committed
122
            }
zorglub's avatar
zorglub committed
123
            else if( DESCR_IS( "Publisher" ) )
zorglub's avatar
zorglub committed
124
            {
125
                vlc_meta_SetPublisher( p_meta, psz_temp );
zorglub's avatar
zorglub committed
126
            }
zorglub's avatar
zorglub committed
127
128
129
130
131
            else if( DESCR_IS( "Track number/position in set" ) )
            {
                vlc_meta_SetTracknum( p_meta, psz_temp );
            }
            else if( DESCR_IS( "Album/movie/show title" ) )
132
            {
zorglub's avatar
zorglub committed
133
                vlc_meta_SetAlbum( p_meta, psz_temp );
134
            }
hartman's avatar
hartman committed
135
136
137
138
139
140
141
142
            else if( DESCR_IS, "Encoded by" ) )
            {
                vlc_meta_SetEncodedBy( p_meta, psz_temp );
            }
            else if( p_frame->description )
            { /* Unhandled meta*/
                msg_Warn( p_demux, "Fixme: unhandled ID3 metatag, %s", p_frame->description );
            }
Sam Hocevar's avatar
Sam Hocevar committed
143
            free( psz_temp );
144
145
146
147
148
149
        }
        i++;
    }
    id3_tag_delete( p_id3_tag );
}

150
151
152
153
/*****************************************************************************
 * ParseID3Tags: check if ID3 tags at common locations. Parse them and skip it
 * if it's at the start of the file
 ****************************************************************************/
154
155
static int ParseID3Tags( vlc_object_t *p_this )
{
zorglub's avatar
zorglub committed
156
    demux_t *p_demux = (demux_t *)p_this;
Sam Hocevar's avatar
Sam Hocevar committed
157
    uint8_t *p_peek;
zorglub's avatar
zorglub committed
158
    vlc_bool_t b_seekable;
159
160
    int64_t i_init, i_pos;
    int i_size;
161

162
    p_demux->p_private = NULL;
163

zorglub's avatar
zorglub committed
164
    msg_Dbg( p_demux, "checking for ID3 tag" );
165

zorglub's avatar
zorglub committed
166
    stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
167
168
169
170
171
172
173
174
175
176
177
    if( !b_seekable ) return VLC_SUCCESS;

    i_init = stream_Tell( p_demux->s );

    /*
     * Look for a ID3v1 tag at the end of the file
     */
    i_init = stream_Tell( p_demux->s );
    i_pos = stream_Size( p_demux->s );

    while( i_pos > 128 ) /* while used so we can break; */
Sam Hocevar's avatar
Sam Hocevar committed
178
    {
179
        stream_Seek( p_demux->s, i_pos - 128 );
180

181
182
        /* get 10 byte id3 header */
        if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 ) break;
183

184
185
        i_size = id3_tag_query( p_peek, 10 );
        if( i_size == 128 )
186
        {
187
188
            /* peek the entire tag */
            if( stream_Peek( p_demux->s, &p_peek, i_size ) < i_size ) break;
Sam Hocevar's avatar
Sam Hocevar committed
189

190
191
192
            msg_Dbg( p_demux, "found ID3v1 tag" );
            ParseID3Tag( p_demux, p_peek, i_size );
        }
193

194
195
196
        /* look for ID3v2.4 tag at end of file */
        /* get 10 byte ID3 footer */
        if( stream_Peek( p_demux->s, &p_peek, 128 ) < 128 ) break;
197

198
199
200
201
202
203
204
205
206
207
        i_size = id3_tag_query( p_peek + 118, 10 );
        if( i_size < 0  && i_pos > -i_size )
        {
            /* id3v2.4 footer found */
            stream_Seek( p_demux->s , i_pos + i_size );
            /* peek the entire tag */
            if( stream_Peek( p_demux->s, &p_peek, i_size ) < i_size ) break;

            msg_Dbg( p_demux, "found ID3v2 tag at end of file" );
            ParseID3Tag( p_demux, p_peek, i_size );
208
        }
209
        break;
210
211
    }

212
213
214
215
216
    /*
     * Get 10 byte id3 header
     */
    stream_Seek( p_demux->s, 0 );
    if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 ) goto end;
217

218
219
220
    if( (i_size = id3_tag_query( p_peek, 10 )) <= 0 ) goto end;

    if( stream_Peek( p_demux->s, &p_peek, i_size ) < i_size ) goto end;
221

zorglub's avatar
zorglub committed
222
    msg_Dbg( p_demux, "found ID3v2 tag" );
223
    ParseID3Tag( p_demux, p_peek, i_size );
gbazin's avatar
   
gbazin committed
224

225
226
 end:
    stream_Seek( p_demux->s, i_init );
227
    return VLC_SUCCESS;
228
}