Index: mythmusic/mythmusic/visualize.cpp
===================================================================
--- mythmusic/mythmusic/visualize.cpp	(revision 13312)
+++ mythmusic/mythmusic/visualize.cpp	(working copy)
@@ -389,11 +389,6 @@
     return false;
 }
 
-QString AlbumArt::getImageFilename() 
-{
-    return m_pParent->metadata()->getAlbumArt(m_currImageType);
-}
-
 bool AlbumArt::draw(QPainter *p, const QColor &back)
 {
     if (!m_pParent->decoder())
@@ -402,7 +397,7 @@
     // If the directory has changed (new album) or the size, reload
     if (needsUpdate())
     {
-        QImage art(getImageFilename());
+        QImage art(m_pParent->metadata()->getAlbumArt(m_currImageType));
         if (art.isNull())
         {
             m_cursize = m_size;
Index: mythmusic/mythmusic/metadata.cpp
===================================================================
--- mythmusic/mythmusic/metadata.cpp	(revision 13312)
+++ mythmusic/mythmusic/metadata.cpp	(working copy)
@@ -381,6 +381,44 @@
 
     if (m_id < 1 && query.isActive() && 1 == query.numRowsAffected())
         m_id = query.lastInsertId().toInt();
+
+    if (! m_albumart.empty())
+    {
+        QValueList<struct AlbumArtImage>::iterator it;
+        for ( it = m_albumart.begin(); it != m_albumart.end(); ++it )
+        {
+            query.prepare("SELECT albumart_id FROM music_albumart WHERE "
+                          "song_id=:SONGID AND imagetype=:TYPE;");
+            query.bindValue(":TYPE", (*it).imageType);
+            query.bindValue(":SONGID", m_id);
+            query.exec();
+
+            if (query.next())
+            {
+                int artid = query.value(0).toInt();
+
+                query.prepare("UPDATE music_albumart SET "
+                            "filename=:FILENAME, imagetype=:TYPE, "
+                            "song_id=:SONGID, embedded=:EMBED "
+                            "WHERE albumart_id=:ARTID");
+
+                query.bindValue(":ARTID", artid);
+            }
+            else
+            {
+                query.prepare("INSERT INTO music_albumart ( filename, "
+                            "imagetype, song_id, embedded ) VALUES ( "
+                            ":FILENAME, :TYPE, :SONGID, :EMBED );");
+            }
+
+            query.bindValue(":FILENAME", (*it).description);
+            query.bindValue(":TYPE", (*it).imageType);
+            query.bindValue(":SONGID", m_id);
+            query.bindValue(":EMBED", 1);
+
+            query.exec();
+        }
+    }
 }
 
 // Default values for formats
@@ -616,6 +654,11 @@
     m_changed = true;
 }
 
+void Metadata::setEmbeddedAlbumArt(QValueList<struct AlbumArtImage> albumart)
+{
+    m_albumart = albumart;
+}
+
 QStringList Metadata::fillFieldList(QString field)
 {
     QStringList searchList;
@@ -658,38 +701,23 @@
     return searchList;
 }
 
-QStringList Metadata::AlbumArtInDir(QString directory)
+QImage Metadata::getAlbumArt(ImageType type)
 {
-    QStringList paths;
+    AlbumArtImages albumArt(this);
 
-    directory.remove(0, m_startdir.length());
+    QImage image;
 
-    MSqlQuery query(MSqlQuery::InitCon());
-    query.prepare("SELECT CONCAT_WS('/', music_directories.path, "
-                  "music_albumart.filename) FROM music_albumart "
-                  "LEFT JOIN music_directories ON "
-                  "music_directories.directory_id=music_albumart.directory_id "
-                  "WHERE music_directories.path = :DIR;");
-    query.bindValue(":DIR", directory.utf8());
-    if (query.exec())
+    if (albumArt.isImageAvailable(type))
     {
-        while (query.next())
-        {
-            paths += m_startdir + "/" +
-                QString::fromUtf8(query.value(0).toString());
-        }
+        AlbumArtImage albumart_image = albumArt.getImage(type);
+
+        if (albumart_image.embedded)
+            image = QImage(MetaIOTagLib::getAlbumArt(m_filename, type));
+        else
+            image = QImage(albumart_image.filename);
     }
-    return paths;
-}
 
-QString Metadata::getAlbumArt(ImageType type)
-{
-    QString res = "";
-    AlbumArtImages albumArt(this);
-
-    res = albumArt.getImageFilename(type);
-
-    return res;
+    return image;
 }
 
 MetadataLoadingThread::MetadataLoadingThread(AllMusic *parent_ptr)
@@ -1389,19 +1417,24 @@
     if (m_parent == NULL)
         return;
 
+    int trackid = m_parent->ID();
+
     QFileInfo fi(m_parent->Filename());
     QString dir = fi.dirPath(true);
     dir.remove(0, Metadata::GetStartdir().length());
 
     MSqlQuery query(MSqlQuery::InitCon());
     query.prepare("SELECT albumart_id, CONCAT_WS('/', music_directories.path, "
-            "music_albumart.filename), music_albumart.imagetype "
+            "music_albumart.filename), music_albumart.imagetype, "
+            "music_albumart.embedded "
             "FROM music_albumart "
             "LEFT JOIN music_directories ON "
             "music_directories.directory_id=music_albumart.directory_id "
             "WHERE music_directories.path = :DIR "
+            "OR song_id = :SONGID "
             "ORDER BY music_albumart.imagetype;");
     query.bindValue(":DIR", dir.utf8());
+    query.bindValue(":SONGID", trackid);
     if (query.exec())
     {
         while (query.next())
@@ -1412,12 +1445,20 @@
                     QString::fromUtf8(query.value(1).toString());
             image->imageType = (ImageType) query.value(2).toInt();
             image->typeName = getTypeName(image->imageType);
+            if (query.value(3).toInt() == 1)
+            {
+                image->description = query.value(1).toString();
+                image->embedded = true;
+            }
+            else {
+                image->embedded = false;
+            }
             m_imageList.append(image);
         }
     }
 }
 
-QString AlbumArtImages::getImageFilename(ImageType type)
+AlbumArtImage AlbumArtImages::getImage(ImageType type)
 {
     // try to find a matching image
     AlbumArtImage *image;
@@ -1425,10 +1466,10 @@
     for (image = m_imageList.first(); image; image = m_imageList.next())
     {
         if (image->imageType == type)
-            return image->filename;
+            return *image;
     }
 
-    return "";
+    return *image;
 }
 
 QStringList AlbumArtImages::getImageFilenames()
@@ -1445,6 +1486,11 @@
     return paths;
 }
 
+AlbumArtImage AlbumArtImages::getImageAt(uint index)
+{
+    return *(m_imageList.at(index));
+}
+
 bool AlbumArtImages::isImageAvailable(ImageType type)
 {
     // try to find a matching image
@@ -1459,27 +1505,14 @@
     return false;
 }
 
-bool AlbumArtImages::saveImageType(const QString &filename, ImageType type)
+bool AlbumArtImages::saveImageType(const int id, ImageType type)
 {
-    // try to find a matching filename
-    AlbumArtImage *image;
-
-    for (image = m_imageList.first(); image; image = m_imageList.next())
-    {
-        if (image->filename == filename)
-        {
-            image->imageType = type;
-
-            MSqlQuery query(MSqlQuery::InitCon());
-            query.prepare("UPDATE music_albumart SET imagetype = :TYPE "
-                          "WHERE albumart_id = :ID");
-            query.bindValue(":TYPE", type);
-            query.bindValue(":ID", image->id);
-            return (query.exec());
-        }
-    }
-
-    return false;
+    MSqlQuery query(MSqlQuery::InitCon());
+    query.prepare("UPDATE music_albumart SET imagetype = :TYPE "
+                    "WHERE albumart_id = :ID");
+    query.bindValue(":TYPE", type);
+    query.bindValue(":ID", id);
+    return (query.exec());
 }
 
 QString AlbumArtImages::getTypeName(ImageType type)
Index: mythmusic/mythmusic/dbcheck.cpp
===================================================================
--- mythmusic/mythmusic/dbcheck.cpp	(revision 13312)
+++ mythmusic/mythmusic/dbcheck.cpp	(working copy)
@@ -9,7 +9,7 @@
 #include "mythtv/mythcontext.h"
 #include "mythtv/mythdbcon.h"
 
-const QString currentDatabaseVersion = "1011";
+const QString currentDatabaseVersion = "1012";
 
 static bool UpdateDBVersionNumber(const QString &newnumber)
 {   
@@ -587,6 +587,18 @@
             return false;
 
     }
+
+    if (dbver == "1011")
+    {
+        const QString updates[] = {
+"ALTER TABLE music_albumart ADD COLUMN song_id int(11) NOT NULL DEFAULT '0', ADD COLUMN embedded TINYINT(1) NOT NULL DEFAULT '0';",
+        ""
+};
+
+        if (!performActualUpdate(updates, "1012", dbver))
+            return false;
+
+    }
 /* in 0.21 */
 //"DROP TABLE musicmetadata;",
 //"DROP TABLE musicplaylist;",
Index: mythmusic/mythmusic/metadata.h
===================================================================
--- mythmusic/mythmusic/metadata.h	(revision 13312)
+++ mythmusic/mythmusic/metadata.h	(working copy)
@@ -4,12 +4,14 @@
 #include <qstring.h>
 #include <qstringlist.h>
 #include <qptrlist.h>
+#include <qvaluelist.h>
 #include <qmap.h>
 #include <qthread.h>
 
 #include "treecheckitem.h"
 #include <mythtv/uitypes.h>
 
+
 class AllMusic;
 class CoverArt;
 
@@ -23,14 +25,20 @@
     IT_LAST
 };
 
+#include "metaiotaglib.h"
+
 typedef struct AlbumArtImage
 {
     int       id;
     QString   filename;
     ImageType imageType;
     QString   typeName;
+    QString   description;
+    bool      embedded;
 } AlbumArtImage;
 
+//typedef QValueList<struct AlbumArtImage> AlbumArtList;
+
 class Metadata
 {
   public:
@@ -59,6 +67,7 @@
                    m_lastplay(llastplay),
                    m_playcount(lplaycount),
                    m_compilation(lcompilation),
+                   m_albumart(),
                    m_id(lid),
                    m_filename(lfilename),
                    m_changed(false),
@@ -159,6 +168,8 @@
     }
     bool determineIfCompilation(bool cd = false);
 
+    void setEmbeddedAlbumArt(QValueList<struct AlbumArtImage> art);
+
     bool isInDatabase(void);
     void dumpToDatabase(void);
     void setField(const QString &field, const QString &data);
@@ -173,8 +184,7 @@
 
     static QStringList fillFieldList(QString field);
 
-    QStringList AlbumArtInDir(QString directory);
-    QString getAlbumArt(ImageType type);
+    QImage getAlbumArt(ImageType type);
 
   private:
     void setCompilationFormatting(bool cd = false);
@@ -201,6 +211,7 @@
     QString m_lastplay;
     int m_playcount;
     bool m_compilation;
+    QValueList<struct AlbumArtImage> m_albumart;
 
     unsigned int m_id;
     QString m_filename;
@@ -389,7 +400,7 @@
     AlbumArtImages(Metadata *metadata);
 
     uint                     getImageCount() { return m_imageList.count(); }
-    QString                  getImageFilename(ImageType type);
+    AlbumArtImage            getImage(ImageType type);
     QString                  getTypeName(ImageType type);
     QStringList              getImageFilenames();
     QPtrList<AlbumArtImage> *getImageList() { return &m_imageList; }
@@ -397,7 +408,7 @@
 
     bool isImageAvailable(ImageType type);
 
-    bool saveImageType(const QString &filename, ImageType type);
+    bool saveImageType(const int id, ImageType type);
 
     static ImageType guessImageType(const QString &filename);
 
Index: mythmusic/mythmusic/editmetadata.cpp
===================================================================
--- mythmusic/mythmusic/editmetadata.cpp	(revision 13312)
+++ mythmusic/mythmusic/editmetadata.cpp	(working copy)
@@ -148,6 +148,9 @@
 
     for (uint x = 0; x < albumArtList->count(); x++)
     {
+        if (albumArtList->at(x)->embedded)
+            continue;
+
         QPixmap *pixmap = createScaledPixmap(albumArtList->at(x)->filename,
                                              size.width(), size.height(),
                                              QImage::ScaleMin);
@@ -738,7 +741,7 @@
                 image->typeName = item->text;
 
                 // save the image type to the DB
-                albumArt->saveImageType(image->filename, image->imageType);
+                albumArt->saveImageType(image->id, image->imageType);
 
                 gridItemChanged(item);
             }
Index: mythmusic/mythmusic/metaiotaglib.h
===================================================================
--- mythmusic/mythmusic/metaiotaglib.h	(revision 13312)
+++ mythmusic/mythmusic/metaiotaglib.h	(working copy)
@@ -2,14 +2,19 @@
 #define METAIOTAGLIB_H_
 
 #include "metaio.h"
+#include "metadata.h"
 #include <id3v2tag.h>
 #include <textidentificationframe.h>
+#include <attachedpictureframe.h>
 #include <mpegfile.h>
+#include <mpegproperties.h>
 
 using TagLib::MPEG::File;
 using TagLib::Tag;
 using TagLib::ID3v2::UserTextIdentificationFrame;
+using TagLib::ID3v2::AttachedPictureFrame;
 using TagLib::String;
+using TagLib::MPEG::Properties;
 
 class MetaIOTagLib : public MetaIO
 {
@@ -20,11 +25,14 @@
     bool write(Metadata* mdata, bool exclusive = false);
     Metadata* read(QString filename);
 
+    static QImage getAlbumArt(QString filename, ImageType type);
+
 private:
 
-     int getTrackLength(QString filename);
+    int getTrackLength(QString filename);
 
-     UserTextIdentificationFrame* find(TagLib::ID3v2::Tag *tag, const String &description);
+    QValueList<struct AlbumArtImage> readAlbumArt(TagLib::ID3v2::Tag *tag);
+    UserTextIdentificationFrame* find(TagLib::ID3v2::Tag *tag, const String &description);
 };
 
 #endif
Index: mythmusic/mythmusic/metaiotaglib.cpp
===================================================================
--- mythmusic/mythmusic/metaiotaglib.cpp	(revision 13312)
+++ mythmusic/mythmusic/metaiotaglib.cpp	(working copy)
@@ -102,6 +102,7 @@
             genre = "";
     int year = 0, tracknum = 0, length = 0, playcount = 0, rating = 0, id = 0;
     bool compilation = false;
+    QValueList<struct AlbumArtImage> albumart;
 
     QString extension = filename.section( '.', -1 ) ;
 
@@ -151,6 +152,12 @@
         // Length
         if(!taglib->ID3v2Tag()->frameListMap()["TLEN"].isEmpty())
             length = taglib->ID3v2Tag()->frameListMap()["TLEN"].front()->toString().toInt();
+
+        // Album Art
+        if(!taglib->ID3v2Tag()->frameListMap()["APIC"].isEmpty())
+        {
+            albumart = readAlbumArt(taglib->ID3v2Tag());
+        }
     }
 
     // Fallback to filename reading
@@ -183,6 +190,7 @@
                                      id, rating, playcount);
 
     retdata->setCompilation(compilation);
+    retdata->setEmbeddedAlbumArt(albumart);
 
     return retdata;
 }
@@ -203,6 +211,126 @@
 }
 
 /*!
+ * \brief Read the albumart image from the file
+ *
+ * \param filename The filename for which we want to find the length.
+ * \param type The type of image we want - front/back etc
+ * \returns A QByteArray that can contains the image data.
+ */
+QImage MetaIOTagLib::getAlbumArt(QString filename, ImageType type)
+{
+    QImage picture;
+
+    AttachedPictureFrame::Type apicType 
+        = AttachedPictureFrame::FrontCover;
+
+    switch (type)
+    {
+        case IT_UNKNOWN :
+            apicType = AttachedPictureFrame::Other;
+            break;
+        case IT_FRONTCOVER :
+            apicType = AttachedPictureFrame::FrontCover;
+            break;
+        case IT_BACKCOVER :
+            apicType = AttachedPictureFrame::BackCover;
+            break;
+        case IT_CD :
+            apicType = AttachedPictureFrame::Media;
+            break;
+        case IT_INLAY :
+            apicType = AttachedPictureFrame::LeafletPage;
+            break;
+        default:
+            return picture;
+    }
+
+    File *taglib = new TagLib::MPEG::File(filename.local8Bit());
+
+    if (taglib->isOpen() && !taglib->ID3v2Tag()->frameListMap()["APIC"].isEmpty())
+    {
+        TagLib::ID3v2::FrameList apicframes = taglib->ID3v2Tag()->frameListMap()["APIC"];
+
+        for(TagLib::ID3v2::FrameList::Iterator it = apicframes.begin(); it != apicframes.end(); ++it) {
+            AttachedPictureFrame *frame = static_cast<AttachedPictureFrame *>(*it);
+            if(frame && frame->type() == apicType)
+            {
+                QImage picture;
+                picture.loadFromData((const uchar *)frame->picture().data(), frame->picture().size());
+                return picture;
+            }
+        }
+    }
+
+    return picture;
+}
+
+/*!
+ * \brief Read the albumart image from the file
+ *
+ * \param tag The ID3v2 tag object in which to look for Album Art
+ * \returns A QValueList containing a list of AlbumArtImage structs
+ *          with the type and description of the APIC tag.
+ */
+QValueList<struct AlbumArtImage> MetaIOTagLib::readAlbumArt(TagLib::ID3v2::Tag *tag)
+{
+
+    QValueList<struct AlbumArtImage> artlist;
+
+    if (!tag->frameListMap()["APIC"].isEmpty())
+    {
+        TagLib::ID3v2::FrameList apicframes = tag->frameListMap()["APIC"];
+
+        for(TagLib::ID3v2::FrameList::Iterator it = apicframes.begin();
+            it != apicframes.end(); ++it)
+        {
+
+            AttachedPictureFrame *frame =
+                static_cast<AttachedPictureFrame *>(*it);
+
+            AlbumArtImage art;
+
+            if (frame->description().isEmpty())
+            {
+                art.description = "";
+            }
+            else {
+                art.description = TStringToQString(frame->description());
+            }
+
+            art.embedded = true;
+
+            switch (frame->type())
+            {
+                case AttachedPictureFrame::FrontCover :
+                    art.imageType = IT_FRONTCOVER;
+                    break;
+                case AttachedPictureFrame::BackCover :
+                    art.imageType = IT_BACKCOVER;
+                    break;
+                case AttachedPictureFrame::Media :
+                    art.imageType = IT_CD;
+                    break;
+                case AttachedPictureFrame::LeafletPage :
+                    art.imageType = IT_INLAY;
+                    break;
+                case AttachedPictureFrame::Other :
+                    art.imageType = IT_UNKNOWN;
+                    break;
+                default:
+                    VERBOSE(VB_GENERAL, "Music Scanner - APIC tag found "
+                                        "with unsupported type");
+                    continue;
+            }
+
+            artlist.append(art);
+        }
+    }
+
+    return artlist;
+}
+
+/*!
  * \brief Find the a custom comment tag by description.
  *        This is a copy of the same function in the
  *        TagLib::ID3v2::UserTextIdentificationFrame Class with a static
Index: mythmusic/mythmusic/mainvisual.cpp
===================================================================
--- mythmusic/mythmusic/mainvisual.cpp	(revision 13312)
+++ mythmusic/mythmusic/mainvisual.cpp	(working copy)
@@ -353,7 +353,7 @@
         return;
 
     QString  text = "\"" + mdata->Title() + "\"\n" +  mdata->Artist() + "\n" + mdata->Album();
-    QString albumArt = mdata->getAlbumArt(IT_FRONTCOVER);
+    QImage albumArt = mdata->getAlbumArt(IT_FRONTCOVER);
 
     if (text == info)
         return;
@@ -373,13 +373,13 @@
     }
 
     // ...and only then when we have an album art image to show
-    if (visMode != 2 && fullScreen && albumArt == "")
+    if (visMode != 2 && fullScreen && albumArt.isNull())
     {
         hide();
         return;
     }
 
-    if (fullScreen && albumArt != "")
+    if (fullScreen && ! albumArt.isNull())
     {
         resize(parentWidget()->width(), parentWidget()->height());
         move(0, 0);
@@ -403,7 +403,7 @@
     int x = indent;
     int y = indent;
 
-    if (fullScreen && albumArt != "")
+    if (fullScreen && ! albumArt.isNull())
     {
         p.fillRect(0, 0, info_pixmap.width(), info_pixmap.height(), QColor ("black"));
 
@@ -422,7 +422,7 @@
     {
         p.fillRect(0, 0, info_pixmap.width(), info_pixmap.height(), QColor ("darkblue"));
 
-        if (albumArt != "")
+        if (! albumArt.isNull())
         {
             // draw the albumArt image
 
Index: mythmusic/mythmusic/visualize.h
===================================================================
--- mythmusic/mythmusic/visualize.h	(revision 13312)
+++ mythmusic/mythmusic/visualize.h	(working copy)
@@ -91,7 +91,6 @@
 
   private:
     bool needsUpdate(void);
-    QString getImageFilename(void);
     void findFrontCover(void);
 
     QSize m_size, m_cursize;
Index: mythmusic/mythmusic/filescanner.cpp
===================================================================
--- mythmusic/mythmusic/filescanner.cpp	(revision 13312)
+++ mythmusic/mythmusic/filescanner.cpp	(working copy)
@@ -469,7 +469,8 @@
     MSqlQuery query(MSqlQuery::InitCon());
     query.exec("SELECT CONCAT_WS('/', path, filename) "
                "FROM music_albumart LEFT JOIN music_directories "
-               "ON music_albumart.directory_id=music_directories.directory_id ");
+               "ON music_albumart.directory_id=music_directories.directory_id "
+               "WHERE music_albumart.embedded=0");
 
     int counter = 0;
 

