]> git.sesse.net Git - vlc/blobdiff - modules/demux/util/id3tag.c
all: updated my name and e-mail address
[vlc] / modules / demux / util / id3tag.c
index ea1dad7abb2d847cf5f54d12080245c04afdb6b6..ef171ce4e6e1f2de4ea6576df8adf377ed541422 100644 (file)
@@ -1,16 +1,16 @@
 /*****************************************************************************
  * id3tag.c: id3 tag parser/skipper based on libid3tag
  *****************************************************************************
- * Copyright (C) 2001 VideoLAN
- * $Id: id3tag.c,v 1.4 2003/02/22 14:11:38 sigmunau Exp $
+ * Copyright (C) 2002-2004 the VideoLAN team
+ * $Id$
+ *
+ * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
  *
- * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
- * 
  * 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
 #include <string.h>
 
 #include <vlc/vlc.h>
+#include <vlc/intf.h>
 #include <vlc/input.h>
 
 #include <sys/types.h>
 
+#include "vlc_meta.h"
+
 #include <id3tag.h>
+#include "id3genres.h"
 
 /*****************************************************************************
  * Local prototypes
@@ -43,38 +47,73 @@ static int  ParseID3Tags ( vlc_object_t * );
  * Module descriptor
  *****************************************************************************/
 vlc_module_begin();
-set_description( _("id3 tag parser using libid3tag" ) );
+set_description( _("ID3 tag parser using libid3tag" ) );
 set_capability( "id3", 70 );
 set_callbacks( ParseID3Tags, NULL );
 vlc_module_end();
 
 /*****************************************************************************
- * Definitions of structures  and functions used by this plugins 
+ * Definitions of structures  and functions used by this plugins
  *****************************************************************************/
 
 /*****************************************************************************
  * ParseID3Tag : parse an id3tag into the info structures
  *****************************************************************************/
-static void ParseID3Tag( input_thread_t *p_input, u8 *p_data, int i_size )
+static void ParseID3Tag( demux_t *p_demux, uint8_t *p_data, int i_size )
 {
-    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;
-    
+    struct id3_tag   *p_id3_tag;
+    struct id3_frame *p_frame;
+    int i = 0;
+
     p_id3_tag = id3_tag_parse( p_data, i_size );
-    p_category = input_InfoCategory( p_input, "ID3" );
-    i = 0;
-    while ( ( p_frame = id3_tag_findframe( p_id3_tag , "T", i ) ) )
+    if( !p_id3_tag ) return;
+
+    if( !p_demux->p_private ) p_demux->p_private = (void *)vlc_meta_New();
+
+    while( ( p_frame = id3_tag_findframe( p_id3_tag , "T", i ) ) )
     {
-        i_strings = id3_field_getnstrings( &p_frame->fields[1] );
-        while ( i_strings > 0 )
+        int i_strings = id3_field_getnstrings( &p_frame->fields[1] );
+
+        while( i_strings > 0 )
         {
-            psz_temp = id3_ucs4_latin1duplicate( id3_field_getstrings ( &p_frame->fields[1], --i_strings ) );
-            input_AddInfo( p_category, (char *)p_frame->description, psz_temp );
-            free( psz_temp ); 
+            char *psz_temp = id3_ucs4_utf8duplicate(
+                id3_field_getstrings( &p_frame->fields[1], --i_strings ) );
+
+            if( !strcmp( p_frame->id, ID3_FRAME_GENRE ) )
+            {
+                char *psz_endptr;
+                int i_genre = strtol( psz_temp, &psz_endptr, 10 );
+
+                if( psz_temp != psz_endptr &&
+                    i_genre >= 0 && i_genre < NUM_GENRES )
+                {
+                    vlc_meta_Add( (vlc_meta_t *)p_demux->p_private,
+                                  VLC_META_GENRE, ppsz_genres[atoi(psz_temp)]);
+                }
+                else
+                {
+                    /* Unknown genre */
+                    vlc_meta_Add( (vlc_meta_t *)p_demux->p_private,
+                                  VLC_META_GENRE, psz_temp );
+                }
+            }
+            else if( !strcmp(p_frame->id, ID3_FRAME_TITLE ) )
+            {
+                vlc_meta_Add( (vlc_meta_t *)p_demux->p_private,
+                              VLC_META_TITLE, psz_temp );
+            }
+            else if( !strcmp(p_frame->id, ID3_FRAME_ARTIST ) )
+            {
+                vlc_meta_Add( (vlc_meta_t *)p_demux->p_private,
+                              VLC_META_ARTIST, psz_temp );
+            }
+            else
+            {
+                /* Unknown meta info */
+                vlc_meta_Add( (vlc_meta_t *)p_demux->p_private,
+                              (char *)p_frame->description, psz_temp );
+            }
+            free( psz_temp );
         }
         i++;
     }
@@ -87,104 +126,76 @@ static void ParseID3Tag( input_thread_t *p_input, u8 *p_data, int i_size )
  ****************************************************************************/
 static int ParseID3Tags( vlc_object_t *p_this )
 {
-    input_thread_t *p_input;
-    u8  *p_peek;
+    demux_t *p_demux = (demux_t *)p_this;
+    uint8_t *p_peek;
+    vlc_bool_t b_seekable;
+    int64_t i_init, i_pos;
     int i_size;
-    int i_size2;
-    stream_position_t * p_pos;
 
-    if ( p_this->i_object_type != VLC_OBJECT_INPUT )
+    p_demux->p_private = NULL;
+
+    msg_Dbg( p_demux, "checking for ID3 tag" );
+
+    stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
+    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; */
     {
-        return( VLC_EGENERIC );
-    }
-    p_input = (input_thread_t *)p_this;
+        stream_Seek( p_demux->s, i_pos - 128 );
 
-    msg_Dbg( p_input, "Checking for ID3 tag" );
+        /* get 10 byte id3 header */
+        if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 ) break;
 
-    if ( p_input->stream.b_seekable )
-    {        
-        /*look for a id3v1 tag at the end of the file*/
-        p_pos = malloc( sizeof( stream_position_t ) );
-        if ( p_pos == 0 )
-        {
-            msg_Err( p_input, "no mem" );
-        }
-        input_Tell( p_input, p_pos );
-        if ( p_pos->i_size >128 )
+        i_size = id3_tag_query( p_peek, 10 );
+        if( i_size == 128 )
         {
-            input_AccessReinit( p_input );
-            p_input->pf_seek( p_input, p_pos->i_size - 128 );
-            
-            /* get 10 byte id3 header */    
-            if( input_Peek( p_input, &p_peek, 10 ) < 10 )
-            {
-                msg_Err( p_input, "cannot peek()" );
-                return( VLC_EGENERIC );
-            }
-            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 );
-            }
+            /* peek the entire tag */
+            if( stream_Peek( p_demux->s, &p_peek, i_size ) < i_size ) break;
+
+            msg_Dbg( p_demux, "found ID3v1 tag" );
+            ParseID3Tag( p_demux, p_peek, i_size );
         }
 
-        /* look for id3v2.4 tag at end of file */
-        if ( p_pos->i_size > 10 )
+        /* 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;
+
+        i_size = id3_tag_query( p_peek + 118, 10 );
+        if( i_size < 0  && i_pos > -i_size )
         {
-            input_AccessReinit( p_input );
-            p_input->pf_seek( p_input, p_pos->i_size - 10 );
-            /* get 10 byte id3 footer */    
-            if( input_Peek( p_input, &p_peek, 10 ) < 10 )
-            {
-                msg_Err( p_input, "cannot peek()" );
-                return( VLC_EGENERIC );
-            }
-            i_size2 = id3_tag_query( p_peek, 10 );
-            if ( i_size2 < 0  && p_pos->i_size > -i_size2 )
-            {                                          /* id3v2.4 footer found */
-                input_AccessReinit( p_input );
-                p_input->pf_seek( p_input, p_pos->i_size + i_size2 );
-                /* 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 );
-            }
+            /* 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 );
         }
-        free( p_pos );
-        input_AccessReinit( p_input );    
-        p_input->pf_seek( p_input, 0 );
-    }
-    /* get 10 byte id3 header */    
-    if( input_Peek( p_input, &p_peek, 10 ) < 10 )
-    {
-        msg_Err( p_input, "cannot peek()" );
-        return( VLC_EGENERIC );
+        break;
     }
 
-    i_size = id3_tag_query( p_peek, 10 );
-    if ( i_size <= 0 )
-    {
-        return( VLC_SUCCESS );
-    }
+    /*
+     * Get 10 byte id3 header
+     */
+    stream_Seek( p_demux->s, 0 );
+    if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 ) goto end;
 
-    /* peek the entire tag */
-    if ( input_Peek( p_input, &p_peek, i_size ) < i_size )
-    {
-        msg_Err( p_input, "cannot peek()" );
-        return( VLC_EGENERIC );
-    }
+    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;
+
+    msg_Dbg( p_demux, "found ID3v2 tag" );
+    ParseID3Tag( p_demux, p_peek, i_size );
 
-    ParseID3Tag( p_input, p_peek, i_size );
-    msg_Dbg( p_input, "ID3 tag found, skiping %d bytes", i_size );
-    p_input->p_current_data += i_size; /* seek passed end of ID3 tag */
-    return( VLC_SUCCESS );
+ end:
+    stream_Seek( p_demux->s, i_init );
+    return VLC_SUCCESS;
 }