demux: hls: add adaptative based hls demuxer
[vlc.git] / modules / demux / xiph_metadata.c
index 16e060d..e4ef767 100644 (file)
@@ -120,6 +120,7 @@ typedef struct chapters_array_t
 
 static seekpoint_t * getChapterEntry( unsigned int i_index, chapters_array_t *p_array )
 {
+    if ( i_index > 4096 ) return NULL;
     if ( i_index >= p_array->i_size )
     {
         unsigned int i_newsize = p_array->i_size;
@@ -144,11 +145,13 @@ static seekpoint_t * getChapterEntry( unsigned int i_index, chapters_array_t *p_
     return p_array->pp_chapters[i_index];
 }
 
-void vorbis_ParseComment( vlc_meta_t **pp_meta,
+void vorbis_ParseComment( es_format_t *p_fmt, vlc_meta_t **pp_meta,
         const uint8_t *p_data, int i_data,
         int *i_attachments, input_attachment_t ***attachments,
         int *i_cover_score, int *i_cover_idx,
-        int *i_seekpoint, seekpoint_t ***ppp_seekpoint )
+        int *i_seekpoint, seekpoint_t ***ppp_seekpoint,
+        float (* ppf_replay_gain)[AUDIO_REPLAY_GAIN_MAX],
+        float (* ppf_replay_peak)[AUDIO_REPLAY_GAIN_MAX] )
 {
     int n;
     int i_comment;
@@ -240,6 +243,14 @@ void vorbis_ParseComment( vlc_meta_t **pp_meta,
         has##var = true; \
     }
 
+#define IF_EXTRACT_FMT(txt,var,fmt,target) \
+    IF_EXTRACT(txt,var)\
+    if( fmt && !strncasecmp(psz_comment, txt, strlen(txt)) )\
+        {\
+            if ( fmt->target ) free( fmt->target );\
+            fmt->target = strdup(&psz_comment[strlen(txt)]);\
+        }
+
         IF_EXTRACT("TITLE=", Title )
         else IF_EXTRACT("ARTIST=", Artist )
         else IF_EXTRACT("GENRE=", Genre )
@@ -273,7 +284,7 @@ void vorbis_ParseComment( vlc_meta_t **pp_meta,
         else IF_EXTRACT("COMMENTS=", Description )
         else IF_EXTRACT("RATING=", Rating )
         else IF_EXTRACT("DATE=", Date )
-        else IF_EXTRACT("LANGUAGE=", Language )
+        else IF_EXTRACT_FMT("LANGUAGE=", Language, p_fmt, psz_language )
         else IF_EXTRACT("ORGANIZATION=", Publisher )
         else IF_EXTRACT("ENCODER=", EncodedBy )
         else if( !strncasecmp( psz_comment, "METADATA_BLOCK_PICTURE=", strlen("METADATA_BLOCK_PICTURE=")))
@@ -292,10 +303,49 @@ void vorbis_ParseComment( vlc_meta_t **pp_meta,
                     *i_attachments, *attachments, p_attachment );
             }
         }
-        else if( !strncmp(psz_comment, "CHAPTER", 7) )
+        else if ( ppf_replay_gain && ppf_replay_peak && !strncmp(psz_comment, "REPLAYGAIN_", 11) )
+        {
+            char *p = strchr( psz_comment, '=' );
+            char *psz_val;
+            if (!p) continue;
+            if ( !strncasecmp(psz_comment, "REPLAYGAIN_TRACK_GAIN=", 22) )
+            {
+                psz_val = malloc( strlen(p+1) + 1 );
+                if (!psz_val) continue;
+                if( sscanf( ++p, "%s dB", psz_val ) == 1 )
+                {
+                    (*ppf_replay_gain)[AUDIO_REPLAY_GAIN_TRACK] = us_atof( psz_val );
+                    free( psz_val );
+                }
+            }
+            else if ( !strncasecmp(psz_comment, "REPLAYGAIN_ALBUM_GAIN=", 22) )
+            {
+                psz_val = malloc( strlen(p+1) + 1 );
+                if (!psz_val) continue;
+                if( sscanf( ++p, "%s dB", psz_val ) == 1 )
+                {
+                    (*ppf_replay_gain)[AUDIO_REPLAY_GAIN_ALBUM] = us_atof( psz_val );
+                    free( psz_val );
+                }
+            }
+            else if ( !strncasecmp(psz_comment, "REPLAYGAIN_ALBUM_PEAK=", 22) )
+            {
+                (*ppf_replay_peak)[AUDIO_REPLAY_GAIN_ALBUM] = us_atof( ++p );
+            }
+            else if ( !strncasecmp(psz_comment, "REPLAYGAIN_TRACK_PEAK=", 22) )
+            {
+                (*ppf_replay_peak)[AUDIO_REPLAY_GAIN_TRACK] = us_atof( ++p );
+            }
+        }
+        else if( !strncasecmp(psz_comment, "CHAPTER", 7) )
         {
             unsigned int i_chapt;
             seekpoint_t *p_seekpoint = NULL;
+
+            for( int i = 0; psz_comment[i] && psz_comment[i] != '='; i++ )
+                if( psz_comment[i] >= 'a' && psz_comment[i] <= 'z' )
+                    psz_comment[i] -= 'a' - 'A';
+
             if( strstr( psz_comment, "NAME=" ) &&
                     sscanf( psz_comment, "CHAPTER%uNAME=", &i_chapt ) == 1 )
             {