changeset 19532:13599373bb02

added new helpers to allow easy metadata retrieval and make libmenu use them
author ben
date Fri, 25 Aug 2006 23:14:32 +0000
parents 2effceebd29c
children 0fc9a4ca9e66
files libmenu/menu_param.c metadata.h mplayer.c
diffstat 3 files changed, 249 insertions(+), 126 deletions(-) [+]
line wrap: on
line diff
--- a/libmenu/menu_param.c	Fri Aug 25 19:52:00 2006 +0000
+++ b/libmenu/menu_param.c	Fri Aug 25 23:14:32 2006 +0000
@@ -25,13 +25,7 @@
 #include "menu_list.h"
 #include "input/input.h"
 #include "osdep/keycodes.h"
-
-#include "codec-cfg.h"
-#include "stream/stream.h"
-#include "libmpdemux/demuxer.h"
-#include "libmpdemux/stheader.h"
-
-extern demuxer_t *get_current_demuxer (void);
+#include "metadata.h"
 
 struct list_entry_s {
   struct list_entry p;
@@ -48,7 +42,7 @@
   char* na;
   int hide_na;
 };
-
+ 
 static struct menu_priv_s cfg_dflt = {
   MENU_LIST_PRIV_DFLT,
   NULL,
@@ -82,8 +76,6 @@
 #define OPT_INFO_TRACK "track"
 #define OPT_INFO_GENRE "genre"
 
-#define mp_basename(s) (strrchr(s,'/')==NULL?(char*)s:(strrchr(s,'/')+1))
-
 m_option_t*  mp_property_find(const char* name);
 
 static void entry_set_text(menu_t* menu, list_entry_t* e) {
@@ -110,69 +102,6 @@
     if(e->opt) entry_set_text(menu,e);
 }
 
-static int is_valid_video_property(char *prop) {
-  demuxer_t *demuxer = get_current_demuxer ();
-  sh_video_t *video = (sh_video_t *) demuxer->video->sh;
-
-  if (!prop || !video)
-    return 0;
-
-  if (strcmp (prop, OPT_VCODEC) != 0 &&
-      strcmp (prop, OPT_VBITRATE) != 0 &&
-      strcmp (prop, OPT_RESOLUTION) != 0)
-    return 0;
-  
-  return 1;
-}
-
-static int is_valid_audio_property(char *prop) {
-  demuxer_t *demuxer = get_current_demuxer ();
-  sh_audio_t *audio = (sh_audio_t *) demuxer->audio->sh;
-  
-  if (!prop || !audio)
-    return 0;
-
-  if (strcmp (prop, OPT_ACODEC) != 0 &&
-      strcmp (prop, OPT_ABITRATE) != 0 &&
-      strcmp (prop, OPT_SAMPLES) != 0)
-    return 0;
-  
-  return 1;
-}
-
-static int is_valid_info_property(char *prop) {
-  demuxer_t *demuxer = get_current_demuxer ();
-  
-  if (!prop || !demuxer)
-    return 0;
-
-  if (strcmp (prop, OPT_INFO_TITLE) != 0 &&
-      strcmp (prop, OPT_INFO_ARTIST) != 0 &&
-      strcmp (prop, OPT_INFO_ALBUM) != 0 &&
-      strcmp (prop, OPT_INFO_YEAR) != 0 &&
-      strcmp (prop, OPT_INFO_COMMENT) != 0 &&
-      strcmp (prop, OPT_INFO_TRACK) != 0 &&
-      strcmp (prop, OPT_INFO_GENRE) != 0)
-    return 0;
-  
-  return 1;
-}
-
-static char *grab_demuxer_info(char *tag) {
-  demuxer_t *demuxer = get_current_demuxer ();
-  char **info = demuxer->info;
-  int n;
-
-  if (!info || !tag)
-    return strdup ("");
-
-  for (n = 0; info[2*n] != NULL ; n++)
-    if (!strcmp (info[2*n], tag))
-      break;
-
-  return info[2*n+1] ? strdup (info[2*n+1]) : strdup ("");
-}
-
 static int parse_args(menu_t* menu,char* args) {
   char *element,*body, **attribs, *name, *meta, *val;
   list_entry_t* m = NULL;
@@ -214,53 +143,34 @@
     meta = asx_get_attrib("meta",attribs);
     val = NULL;
     if(meta) {
-      demuxer_t *demuxer = get_current_demuxer ();
-      sh_video_t *video = (sh_video_t *) demuxer->video->sh;
-      sh_audio_t *audio = (sh_audio_t *) demuxer->audio->sh;
-      if (!strcmp (meta, OPT_NAME)) {
-        extern char *filename;
-        val = strdup (mp_basename (filename));
-      } else if(!strcmp (meta, OPT_VCODEC) && is_valid_video_property(meta)) {
-        val = (char *) malloc (8);
-        if (video->format == 0x10000001)
-          sprintf (val, "mpeg1");
-        else if (video->format == 0x10000002)
-          sprintf (val, "mpeg2");
-        else if (video->format == 0x10000004)
-          sprintf (val, "mpeg4");
-        else if (video->format == 0x10000005)
-          sprintf (val, "h264");
-        else if (video->format >= 0x20202020)
-          sprintf (val, "%.4s", (char *) &video->format);
-        else
-          sprintf (val, "0x%08X", video->format);
-      } else if (!strcmp(meta, OPT_VBITRATE)&& is_valid_video_property(meta)){
-        val = (char *) malloc (16);
-        sprintf (val, "%d kbps", (int)(video->i_bps * 8 / 1024));
-      } else if(!strcmp(meta, OPT_RESOLUTION)
-                && is_valid_video_property(meta)) {
-        val = (char *) malloc (16);
-        sprintf(val, "%d x %d", video->disp_w, video->disp_h);
-      } else if (!strcmp(meta, OPT_ACODEC) && is_valid_audio_property(meta)) {
-        val = strdup (audio->codec->name);
-      } else if(!strcmp(meta, OPT_ABITRATE) && is_valid_audio_property(meta)){
-        val = (char *) malloc (16);
-        sprintf (val, "%d kbps", (int) (audio->i_bps * 8/1000));
-      } else if(!strcmp(meta, OPT_SAMPLES) && is_valid_audio_property(meta)) {
-        val = (char *) malloc (16);
-        sprintf (val, "%d Hz, %d ch.", audio->samplerate, audio->channels);
-      } else if ((!strcmp (meta, OPT_INFO_TITLE) ||
-                 !strcmp (meta, OPT_INFO_ARTIST) ||
-                 !strcmp (meta, OPT_INFO_ALBUM) ||
-                 !strcmp (meta, OPT_INFO_YEAR) ||
-                 !strcmp (meta, OPT_INFO_COMMENT) ||
-                 !strcmp (meta, OPT_INFO_TRACK) ||
-                 !strcmp (meta, OPT_INFO_GENRE)) &&
-                 is_valid_info_property(meta) &&
-                 strcmp(grab_demuxer_info(meta), "") ) {
-        val = grab_demuxer_info (meta);
-      }
-    }
+      if (!strcmp (meta, OPT_NAME))
+        val = get_metadata (META_NAME);
+      else if (!strcmp (meta, OPT_VCODEC))
+        val = get_metadata (META_VIDEO_CODEC);
+      else if (!strcmp(meta, OPT_VBITRATE))
+        val = get_metadata (META_VIDEO_BITRATE);
+      else if(!strcmp(meta, OPT_RESOLUTION))
+      val = get_metadata (META_VIDEO_RESOLUTION);
+      else if (!strcmp(meta, OPT_ACODEC))
+        val = get_metadata (META_AUDIO_CODEC);
+      else if(!strcmp(meta, OPT_ABITRATE))
+        val = get_metadata (META_AUDIO_BITRATE);
+      else if(!strcmp(meta, OPT_SAMPLES))
+        val = get_metadata (META_AUDIO_SAMPLES);
+      else if (!strcmp (meta, OPT_INFO_TITLE))
+        val = get_metadata (META_INFO_TITLE);
+      else if (!strcmp (meta, OPT_INFO_ARTIST))
+        val = get_metadata (META_INFO_ARTIST);
+      else if (!strcmp (meta, OPT_INFO_ALBUM))
+        val = get_metadata (META_INFO_ALBUM);
+      else if (!strcmp (meta, OPT_INFO_YEAR))
+        val = get_metadata (META_INFO_YEAR);
+      else if (!strcmp (meta, OPT_INFO_COMMENT))
+        val = get_metadata (META_INFO_COMMENT);
+      else if (!strcmp (meta, OPT_INFO_TRACK))
+        val = get_metadata (META_INFO_TRACK);
+      else if (!strcmp (meta, OPT_INFO_GENRE))
+      val = get_metadata (META_INFO_GENRE);
     if (val) {
       char *item = asx_get_attrib("name",attribs);
       int l;
@@ -274,10 +184,15 @@
       free(val);
       free(item);
       menu_list_add_entry(menu,m);
-      goto next_element;
     }
-    if (meta)
-      goto next_element;
+    free (meta);
+    if (element)
+      free(element);
+    if(body)
+      free(body);
+    asx_free_attribs(attribs);
+    continue;
+    }
     
     name = asx_get_attrib("property",attribs);
     opt = name ? mp_property_find(name) : NULL;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/metadata.h	Fri Aug 25 23:14:32 2006 +0000
@@ -0,0 +1,51 @@
+/*
+ *  Copyright (C) 2006 Benjamin Zores
+ *   Set of helper routines for stream metadata and properties retrieval.
+ *
+ *   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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef _METADATA_H_
+#define _METADATA_H_
+
+typedef enum metadata_s metadata_t;
+enum metadata_s {
+  /* common info */
+  META_NAME = 0,
+
+  /* video stream properties */
+  META_VIDEO_CODEC,
+  META_VIDEO_BITRATE,
+  META_VIDEO_RESOLUTION,
+
+  /* audio stream properties */
+  META_AUDIO_CODEC,
+  META_AUDIO_BITRATE,
+  META_AUDIO_SAMPLES,
+
+  /* ID3 tags and other stream infos */
+  META_INFO_TITLE,
+  META_INFO_ARTIST,
+  META_INFO_ALBUM,
+  META_INFO_YEAR,
+  META_INFO_COMMENT,
+  META_INFO_TRACK,
+  META_INFO_GENRE
+};
+
+char *get_metadata (metadata_t type);
+
+#endif /* _METADATA_H_ */
+
--- a/mplayer.c	Fri Aug 25 19:52:00 2006 +0000
+++ b/mplayer.c	Fri Aug 25 23:14:32 2006 +0000
@@ -424,8 +424,166 @@
 #define INITED_VCODEC  2048
 #define INITED_ALL 0xFFFF
 
-demuxer_t *get_current_demuxer (void) {
-  return demuxer;
+#include "metadata.h"
+
+#define mp_basename2(s) (strrchr(s,'/')==NULL?(char*)s:(strrchr(s,'/')+1))
+
+static int is_valid_metadata_type (metadata_t type) {
+  switch (type)
+  {
+  /* check for valid video stream */
+  case META_VIDEO_CODEC:
+  case META_VIDEO_BITRATE:
+  case META_VIDEO_RESOLUTION:
+  {
+    if (!sh_video)
+      return 0;
+    break;
+  }
+
+  /* check for valid audio stream */
+  case META_AUDIO_CODEC:
+  case META_AUDIO_BITRATE:
+  case META_AUDIO_SAMPLES:
+  {
+    if (!sh_audio)
+      return 0;
+    break;
+  }
+
+  /* check for valid demuxer */
+  case META_INFO_TITLE:
+  case META_INFO_ARTIST:
+  case META_INFO_ALBUM:
+  case META_INFO_YEAR:
+  case META_INFO_COMMENT:
+  case META_INFO_TRACK:
+  case META_INFO_GENRE:
+  {
+    if (!demuxer)
+      return 0;
+    break;
+  }
+
+  default:
+    break;
+  }
+
+  return 1;
+}
+
+static char *get_demuxer_info (char *tag) {
+  char **info = demuxer->info;
+  int n;
+
+  if (!info || !tag)
+    return NULL;
+
+  for (n = 0; info[2*n] != NULL ; n++)
+    if (!strcmp (info[2*n], tag))
+      break;
+
+  return info[2*n+1] ? strdup (info[2*n+1]) : NULL;
+}
+
+char *get_metadata (metadata_t type) {
+  char *meta = NULL;
+
+  if (!is_valid_metadata_type (type))
+    return NULL;
+  
+  switch (type)
+  {
+  case META_NAME:
+  {
+    return strdup (mp_basename2 (filename));
+  }
+    
+  case META_VIDEO_CODEC:
+  {
+    if (sh_video->format == 0x10000001)
+      meta = strdup ("mpeg1");
+    else if (sh_video->format == 0x10000002)
+      meta = strdup ("mpeg2");
+    else if (sh_video->format == 0x10000004)
+      meta = strdup ("mpeg4");
+    else if (sh_video->format == 0x10000005)
+      meta = strdup ("h264");
+    else if (sh_video->format >= 0x20202020)
+    {
+      meta = (char *) malloc (8);
+      sprintf (meta, "%.4s", (char *) &sh_video->format);
+    }
+    else
+    {
+      meta = (char *) malloc (8);
+      sprintf (meta, "0x%08X", sh_video->format);
+    }
+    return meta;
+  }
+  
+  case META_VIDEO_BITRATE:
+  {
+    meta = (char *) malloc (16);
+    sprintf (meta, "%d kbps", (int) (sh_video->i_bps * 8 / 1024));
+    return meta;
+  }
+  
+  case META_VIDEO_RESOLUTION:
+  {
+    meta = (char *) malloc (16);
+    sprintf (meta, "%d x %d", sh_video->disp_w, sh_video->disp_h);
+    return meta;
+  }
+
+  case META_AUDIO_CODEC:
+  {
+    if (sh_audio->codec && sh_audio->codec->name)
+      meta = strdup (sh_audio->codec->name);
+    return meta;
+  }
+  
+  case META_AUDIO_BITRATE:
+  {
+    meta = (char *) malloc (16);
+    sprintf (meta, "%d kbps", (int) (sh_audio->i_bps * 8/1000));
+    return meta;
+  }
+  
+  case META_AUDIO_SAMPLES:
+  {
+    meta = (char *) malloc (16);
+    sprintf (meta, "%d Hz, %d ch.", sh_audio->samplerate, sh_audio->channels);
+    return meta;
+  }
+
+  /* check for valid demuxer */
+  case META_INFO_TITLE:
+    return get_demuxer_info ("Title");
+  
+  case META_INFO_ARTIST:
+    return get_demuxer_info ("Artist");
+
+  case META_INFO_ALBUM:
+    return get_demuxer_info ("Album");
+
+  case META_INFO_YEAR:
+    return get_demuxer_info ("Year");
+
+  case META_INFO_COMMENT:
+    return get_demuxer_info ("Comment");
+
+  case META_INFO_TRACK:
+    return get_demuxer_info ("Track");
+
+  case META_INFO_GENRE:
+    return get_demuxer_info ("Genre");
+
+  default:
+    break;
+  }
+
+  return meta;
 }
 
 static void uninit_player(unsigned int mask){
@@ -744,7 +902,6 @@
   return eof;
 }
 
-#define mp_basename2(s) (strrchr(s,'/')==NULL?(char*)s:(strrchr(s,'/')+1))
 #define mp_basename(s) (strrchr(s,'\\')==NULL?(mp_basename2(s)):(strrchr(s,'\\')+1))
 
 int playtree_add_playlist(play_tree_t* entry)