id3tag.c
上传用户:riyaled888
上传日期:2009-03-27
资源大小:7338k
文件大小:8k
源码类别:

多媒体

开发平台:

MultiPlatform

  1. /*****************************************************************************
  2.  * id3tag.c: id3 tag parser/skipper based on libid3tag
  3.  *****************************************************************************
  4.  * Copyright (C) 2002-2004 VideoLAN
  5.  * $Id: id3tag.c 8332 2004-07-30 21:13:17Z sam $
  6.  *
  7.  * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
  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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  22.  *****************************************************************************/
  23. /*****************************************************************************
  24.  * Preamble
  25.  *****************************************************************************/
  26. #include <stdlib.h>                                      /* malloc(), free() */
  27. #include <string.h>
  28. #include <vlc/vlc.h>
  29. #include <vlc/intf.h>
  30. #include <vlc/input.h>
  31. #include <sys/types.h>
  32. #include "vlc_meta.h"
  33. #include <id3tag.h>
  34. #include "id3genres.h"
  35. /*****************************************************************************
  36.  * Local prototypes
  37.  *****************************************************************************/
  38. static int  ParseID3Tags ( vlc_object_t * );
  39. /*****************************************************************************
  40.  * Module descriptor
  41.  *****************************************************************************/
  42. vlc_module_begin();
  43. set_description( _("ID3 tag parser using libid3tag" ) );
  44. set_capability( "id3", 70 );
  45. set_callbacks( ParseID3Tags, NULL );
  46. vlc_module_end();
  47. /*****************************************************************************
  48.  * Definitions of structures  and functions used by this plugins
  49.  *****************************************************************************/
  50. /*****************************************************************************
  51.  * ParseID3Tag : parse an id3tag into the info structures
  52.  *****************************************************************************/
  53. static void ParseID3Tag( demux_t *p_demux, uint8_t *p_data, int i_size )
  54. {
  55.     struct id3_tag        *p_id3_tag;
  56.     struct id3_frame      *p_frame;
  57.     char                  *psz_temp;
  58.     vlc_value_t val;
  59.     int i;
  60.     input_thread_t *p_input;
  61.     p_input = vlc_object_find( p_demux, VLC_OBJECT_INPUT,
  62.                                 FIND_PARENT );
  63.     if( !p_input)
  64.     {
  65.         return;
  66.     }
  67.     var_Get( p_input, "demuxed-id3", &val );
  68.     if( val.b_bool )
  69.     {
  70.         msg_Dbg( p_demux, "the ID3 tag was already parsed" );
  71.         return;
  72.     }
  73.     val.b_bool = VLC_FALSE;
  74.     p_id3_tag = id3_tag_parse( p_data, i_size );
  75.     i = 0;
  76.     while ( ( p_frame = id3_tag_findframe( p_id3_tag , "T", i ) ) )
  77.     {
  78.         int i_strings;
  79.         i_strings = id3_field_getnstrings( &p_frame->fields[1] );
  80.         while ( i_strings > 0 )
  81.         {
  82.             psz_temp = id3_ucs4_utf8duplicate( id3_field_getstrings( &p_frame->fields[1], --i_strings ) );
  83.             if ( !strcmp(p_frame->id, ID3_FRAME_GENRE ) )
  84.             {
  85.                 int i_genre;
  86.                 char *psz_endptr;
  87.                 i_genre = strtol( psz_temp, &psz_endptr, 10 );
  88.                 if( psz_temp != psz_endptr && i_genre >= 0 &&
  89.                                               i_genre < NUM_GENRES )
  90.                 {
  91.                     vlc_meta_Add( (vlc_meta_t *)p_demux->p_private,
  92.                                   VLC_META_GENRE, ppsz_genres[atoi(psz_temp)]);
  93.                 }
  94.                 else
  95.                 {
  96.                     /* Unknown genre */
  97.                     vlc_meta_Add( (vlc_meta_t *)p_demux->p_private,
  98.                                    VLC_META_GENRE, psz_temp );
  99.                 }
  100.             }
  101.             else if ( !strcmp(p_frame->id, ID3_FRAME_TITLE ) )
  102.             {
  103.                 vlc_meta_Add( (vlc_meta_t *)p_demux->p_private,
  104.                                VLC_META_TITLE, psz_temp );
  105. //              input_Control( p_demux, INPUT_SET_NAME, psz_temp );
  106.             }
  107.             else if ( !strcmp(p_frame->id, ID3_FRAME_ARTIST ) )
  108.             {
  109.                 vlc_meta_Add( (vlc_meta_t *)p_demux->p_private,
  110.                                VLC_META_ARTIST, psz_temp );
  111.             }
  112.             else
  113.             {
  114.                 /* Unknown meta info */
  115.                 vlc_meta_Add( (vlc_meta_t *)p_demux->p_private,
  116.                                (char *)p_frame->description, psz_temp );
  117.             }
  118.             free( psz_temp );
  119.         }
  120.         i++;
  121.     }
  122.     id3_tag_delete( p_id3_tag );
  123.     val.b_bool = VLC_TRUE;
  124.     var_Change( p_demux, "demuxed-id3", VLC_VAR_SETVALUE, &val, NULL );
  125.     vlc_object_release( p_input );
  126. }
  127. /*****************************************************************************
  128.  * ParseID3Tags: check if ID3 tags at common locations. Parse them and skip it
  129.  * if it's at the start of the file
  130.  ****************************************************************************/
  131. static int ParseID3Tags( vlc_object_t *p_this )
  132. {
  133.     demux_t *p_demux = (demux_t *)p_this;
  134.     uint8_t *p_peek;
  135.     int i_size;
  136.     int i_size2;
  137.     vlc_bool_t b_seekable;
  138.     p_demux->p_private = (void *)vlc_meta_New();
  139.     msg_Dbg( p_demux, "checking for ID3 tag" );
  140.     stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
  141.     if( b_seekable )
  142.     {
  143.         int64_t i_init;
  144.         int64_t i_pos;
  145.         /*look for a ID3v1 tag at the end of the file*/
  146.         i_init = stream_Tell( p_demux->s );
  147.         i_pos = stream_Size( p_demux->s );
  148.         if ( i_pos >128 )
  149.         {
  150.             stream_Seek( p_demux->s, i_pos - 128 );
  151.             /* get 10 byte id3 header */
  152.             if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 )
  153.             {
  154.                 msg_Err( p_demux, "cannot peek()" );
  155.                 return( VLC_EGENERIC );
  156.             }
  157.             i_size2 = id3_tag_query( p_peek, 10 );
  158.             if ( i_size2 == 128 )
  159.             {
  160.                 /* peek the entire tag */
  161.                 if ( stream_Peek( p_demux->s, &p_peek, i_size2 ) < i_size2 )
  162.                 {
  163.                     msg_Err( p_demux, "cannot peek()" );
  164.                     return( VLC_EGENERIC );
  165.                 }
  166.                 msg_Dbg( p_demux, "found ID3v1 tag" );
  167.                 ParseID3Tag( p_demux, p_peek, i_size2 );
  168.             }
  169.             /* look for ID3v2.4 tag at end of file */
  170.             /* get 10 byte ID3 footer */
  171.             if( stream_Peek( p_demux->s, &p_peek, 128 ) < 128 )
  172.             {
  173.                 msg_Err( p_demux, "cannot peek()" );
  174.                 return( VLC_EGENERIC );
  175.             }
  176.             i_size2 = id3_tag_query( p_peek + 118, 10 );
  177.             if ( i_size2 < 0  && i_pos > -i_size2 )
  178.             {                                        /* id3v2.4 footer found */
  179.                 stream_Seek( p_demux->s , i_pos + i_size2 );
  180.                 /* peek the entire tag */
  181.                 if ( stream_Peek( p_demux->s, &p_peek, i_size2 ) < i_size2 )
  182.                 {
  183.                     msg_Err( p_demux, "cannot peek()" );
  184.                     return( VLC_EGENERIC );
  185.                 }
  186.                 msg_Dbg( p_demux, "found ID3v2 tag at end of file" );
  187.                 ParseID3Tag( p_demux, p_peek, i_size2 );
  188.             }
  189.         }
  190.         stream_Seek( p_demux->s, i_init );
  191.     }
  192.     /* get 10 byte id3 header */
  193.     if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 )
  194.     {
  195.         msg_Err( p_demux, "cannot peek()" );
  196.         return( VLC_EGENERIC );
  197.     }
  198.     i_size = id3_tag_query( p_peek, 10 );
  199.     if ( i_size <= 0 )
  200.     {
  201.         return( VLC_SUCCESS );
  202.     }
  203.     /* Read the entire tag */
  204.     p_peek = malloc( i_size );
  205.     if( !p_peek || stream_Read( p_demux->s, p_peek, i_size ) < i_size )
  206.     {
  207.         msg_Err( p_demux, "cannot read ID3 tag" );
  208.         if( p_peek ) free( p_peek );
  209.         return( VLC_EGENERIC );
  210.     }
  211.     ParseID3Tag( p_demux, p_peek, i_size );
  212.     msg_Dbg( p_demux, "found ID3v2 tag" );
  213.     free( p_peek );
  214.     return( VLC_SUCCESS );
  215. }