add basic mkv inline ssa/ass subtitle support
authorAndreas Frisch <andreas.frisch@multimedia-labs.de>
Thu, 2 Oct 2008 14:57:27 +0000 (14:57 +0000)
committerAndreas Frisch <andreas.frisch@multimedia-labs.de>
Thu, 2 Oct 2008 14:57:27 +0000 (14:57 +0000)
lib/python/Plugins/Extensions/MediaPlayer/plugin.py
lib/python/Screens/Subtitles.py
lib/service/servicemp3.cpp
lib/service/servicemp3.h

index 752a036..c4282a0 100644 (file)
@@ -14,7 +14,7 @@ from Tools.Directories import resolveFilename, SCOPE_CONFIG, SCOPE_PLAYLIST, SCO
 from Components.ServicePosition import ServicePositionGauge
 from Components.ServiceEventTracker import ServiceEventTracker, InfoBarBase
 from Components.Playlist import PlaylistIOInternal, PlaylistIOM3U, PlaylistIOPLS
-from Screens.InfoBarGenerics import InfoBarSeek, InfoBarAudioSelection, InfoBarCueSheetSupport, InfoBarNotifications
+from Screens.InfoBarGenerics import InfoBarSeek, InfoBarAudioSelection, InfoBarCueSheetSupport, InfoBarNotifications, InfoBarSubtitleSupport
 from ServiceReference import ServiceReference
 from Screens.ChoiceBox import ChoiceBox
 from Screens.HelpMenu import HelpableScreen
@@ -42,7 +42,7 @@ class MediaPixmap(Pixmap):
                        self.default_pixmap = resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/no_coverArt.png")
                return Pixmap.applySkin(self, desktop, screen)
 
-class MediaPlayer(Screen, InfoBarBase, InfoBarSeek, InfoBarAudioSelection, InfoBarCueSheetSupport, InfoBarNotifications, HelpableScreen):
+class MediaPlayer(Screen, InfoBarBase, InfoBarSeek, InfoBarAudioSelection, InfoBarCueSheetSupport, InfoBarNotifications, InfoBarSubtitleSupport, HelpableScreen):
        ALLOW_SUSPEND = True
        ENABLE_RESUME_SUPPORT = True
 
@@ -52,6 +52,7 @@ class MediaPlayer(Screen, InfoBarBase, InfoBarSeek, InfoBarAudioSelection, InfoB
                InfoBarCueSheetSupport.__init__(self, actionmap = "MediaPlayerCueSheetActions")
                InfoBarNotifications.__init__(self)
                InfoBarBase.__init__(self)
+               InfoBarSubtitleSupport.__init__(self)
                HelpableScreen.__init__(self)
                self.summary = None
                self.oldService = self.session.nav.getCurrentlyPlayingServiceReference()
@@ -123,6 +124,7 @@ class MediaPlayer(Screen, InfoBarBase, InfoBarSeek, InfoBarAudioSelection, InfoB
                                "delete": (self.deletePlaylistEntry, _("delete playlist entry")),
                                "shift_stop": (self.clear_playlist, _("clear playlist")),
                                "shift_record": (self.playlist.PlayListShuffle, _("shuffle playlist")),
+                               "subtitles": (self.subtitleSelection, _("Subtitle selection")),
                        }, -2)
 
                self["InfobarEPGActions"] = HelpableActionMap(self, "InfobarEPGActions", 
@@ -751,6 +753,10 @@ class MediaPlayer(Screen, InfoBarBase, InfoBarSeek, InfoBarAudioSelection, InfoB
 
        def unPauseService(self):
                self.setSeekState(self.SEEK_STATE_PLAY)
+               
+       def subtitleSelection(self):
+               from Screens.Subtitles import Subtitles
+               self.session.open(Subtitles)            
 
 class MediaPlayerLCDScreen(Screen):
        skin = """
index b1c4475..31733d8 100644 (file)
@@ -55,6 +55,14 @@ class Subtitles(Screen, ConfigListScreen):
                                                self.list.append(getConfigListEntry(text+" TTX "+_("Page")+" %x%02x"%(x[3],x[2])+" "+LanguageCodes[x[4]][0], ConfigNothing(), x))
                                        else:
                                                self.list.append(getConfigListEntry(text+" TTX "+_("Page")+" %x%02x"%(x[3],x[2])+" "+x[4], ConfigNothing(), x))
+                       elif x[0] == 2:
+                               if x[4] == 'und': #undefined
+                                       self.list.append(getConfigListEntry(text+" subtitle stream %d " % x[1], ConfigNothing(), x))
+                               else:
+                                       if LanguageCodes.has_key(x[4]):
+                                               self.list.append(getConfigListEntry(text+" subtitle stream %d " % x[1] +LanguageCodes[x[4]][0], ConfigNothing(), x))
+                                       else:
+                                               self.list.append(getConfigListEntry(text+" subtitle stream %d " % x[1] +x[4], ConfigNothing(), x))
 #              return _("Disable subtitles")
                self["config"].list = self.list
                self["config"].l.setList(self.list)
index d94ced4..9a783b6 100644 (file)
@@ -12,6 +12,8 @@
 #include <lib/base/init_num.h>
 #include <lib/base/init.h>
 #include <gst/gst.h>
+               /* for subtitles */
+#include <lib/gui/esubtitle.h>
 
 // eServiceFactoryMP3
 
@@ -169,7 +171,10 @@ eServiceMP3::eServiceMP3(const char *filename): m_filename(filename), m_pump(eAp
 {
        m_stream_tags = 0;
        m_audioStreams.clear();
+       m_subtitleStreams.clear();
        m_currentAudioStream = 0;
+       m_currentSubtitleStream = 0;
+       m_subtitle_widget = 0;
        m_currentTrickRatio = 0;
        CONNECT(m_seekTimeout.timeout, eServiceMP3::seekTimeoutCB);
        CONNECT(m_pump.recv_msg, eServiceMP3::gstPoll);
@@ -418,6 +423,7 @@ eServiceMP3::eServiceMP3(const char *filename): m_filename(filename), m_pump(eAp
 
 eServiceMP3::~eServiceMP3()
 {
+       delete m_subtitle_widget;
        if (m_state == stRunning)
                stop();
        
@@ -732,6 +738,12 @@ RESULT eServiceMP3::audioTracks(ePtr<iAudioTrackSelection> &ptr)
        return 0;
 }
 
+RESULT eServiceMP3::subtitle(ePtr<iSubtitleOutput> &ptr)
+{
+       ptr = this;
+       return 0;
+}
+
 int eServiceMP3::getNumberOfTracks()
 {
        return m_audioStreams.size();
@@ -823,7 +835,7 @@ void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
        GstObject *source;
 
        source = GST_MESSAGE_SRC(msg);
-       sourceName = gst_object_get_name(source);       
+       sourceName = gst_object_get_name(source);
 
        if (gst_message_get_structure(msg))
        {
@@ -919,7 +931,7 @@ void eServiceMP3::gstCBpadAdded(GstElement *decodebin, GstPad *pad, gpointer use
                _this->m_audioStreams.push_back(audio);
                if ( selector )
                {
-                       GstPadLinkReturn ret = gst_pad_link(pad, gst_element_get_request_pad (selector, "sink%d"));
+                       gst_pad_link(pad, gst_element_get_request_pad (selector, "sink%d"));
                        if ( _this->m_audioStreams.size() == 1 )
                        {
                                _this->selectAudioStream(0);
@@ -935,6 +947,30 @@ void eServiceMP3::gstCBpadAdded(GstElement *decodebin, GstPad *pad, gpointer use
        {
                gst_pad_link(pad, gst_element_get_static_pad(gst_bin_get_by_name(pipeline,"queue_video"), "sink"));
        }
+       if (g_strrstr(name,"subtitle"))
+       {
+//             GstCaps *caps;
+//             const GstStructure *structure;  
+//             caps = gst_pad_get_caps(name);
+//             structure = gst_caps_get_structure(caps, 0);
+               char elemname[17];
+               sprintf(elemname, "%s_pars", name);
+               GstElement *parser = gst_element_factory_make("ssaparse", elemname);
+               eDebug ("ssaparse %s = %p", elemname, parser);
+               sprintf(elemname, "%s_sink", name);
+               GstElement *sink = gst_element_factory_make("fakesink", elemname);
+               eDebug ("fakesink %s = %p", elemname, sink);
+               g_object_set (G_OBJECT(sink), "signal-handoffs", TRUE, NULL);
+               gst_bin_add_many(pipeline, parser, sink, NULL);
+               GstPadLinkReturn res = gst_pad_link(pad, gst_element_get_static_pad(parser, "sink"));
+               eDebug ("parser link = %d", res);
+               res = gst_element_link(parser, sink);
+               eDebug ("sink link = %d", res);
+               g_signal_connect(sink, "handoff", G_CALLBACK(gstCBsubtitleAvail), _this);
+               subtitleStream subs;
+               subs.element = sink;
+               _this->m_subtitleStreams.push_back(subs);
+       }
        g_free (name);
 }
 
@@ -1010,3 +1046,98 @@ eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::servi
 #else
 #warning gstreamer not available, not building media player
 #endif
+
+void eServiceMP3::gstCBsubtitleAvail(GstElement *element, GstBuffer *buffer, GstPad *pad, gpointer user_data)
+{
+       const char *text = (unsigned char *)GST_BUFFER_DATA(buffer);
+       eServiceMP3 *_this = (eServiceMP3*)user_data;
+       gchar *sourceName;
+       sourceName = gst_object_get_name(GST_OBJECT(element));
+       if ( _this->m_subtitle_widget && _this->m_subtitleStreams.at(_this->m_currentSubtitleStream).element == element)
+       {
+               eDVBTeletextSubtitlePage page;
+               gRGB rgbcol(0xD0,0xD0,0xD0);
+               page.m_elements.push_back(eDVBTeletextSubtitlePageElement(rgbcol, text));
+               (_this->m_subtitle_widget)->setPage(page);
+       }
+       else
+               eDebug("on inactive element: %s (%p) saw subtitle: %s",sourceName, element, text);
+       return TRUE;
+}
+
+RESULT eServiceMP3::enableSubtitles(eWidget *parent, ePyObject tuple)
+{
+       eDebug("eServiceMP3::enableSubtitles");
+
+       ePyObject entry;
+       int tuplesize = PyTuple_Size(tuple);
+       int type = 0;
+       int page, magazine, pid;
+
+       if (!PyTuple_Check(tuple))
+               goto error_out;
+
+       if (tuplesize < 1)
+               goto error_out;
+
+       entry = PyTuple_GET_ITEM(tuple, 0);
+
+       if (!PyInt_Check(entry))
+               goto error_out;
+
+       type = PyInt_AsLong(entry);
+
+       entry = PyTuple_GET_ITEM(tuple, 1);
+       if (!PyInt_Check(entry))
+               goto error_out;
+       pid = PyInt_AsLong(entry);
+
+       m_subtitle_widget = new eSubtitleWidget(parent);
+       m_subtitle_widget->resize(parent->size()); /* full size */
+       m_currentSubtitleStream = pid;
+
+       return 0;
+error_out:
+       eDebug("enableSubtitles needs a tuple as 2nd argument!\n"
+               "for gst subtitles (2, subtitle_stream_count)"
+       return -1;
+}
+
+RESULT eServiceMP3::disableSubtitles(eWidget *parent)
+{
+       eDebug("eServiceMP3::disableSubtitles");
+       delete m_subtitle_widget;
+       m_subtitle_widget = 0;
+       return 0;
+}
+
+PyObject *eServiceMP3::getCachedSubtitle()
+{
+       eDebug("eServiceMP3::eDVBServicePlay");
+       Py_RETURN_NONE;
+}
+
+PyObject *eServiceMP3::getSubtitleList()
+{
+       eDebug("eServiceMP3::getSubtitleList");
+
+       ePyObject l = PyList_New(0);
+       gchar *sourceName;
+       int stream_count = 0;
+
+       for (std::vector<subtitleStream>::iterator IterSubtitleStream(m_subtitleStreams.begin()); IterSubtitleStream != m_subtitleStreams.end(); ++IterSubtitleStream)
+       {
+               ePyObject tuple = PyTuple_New(5);
+               PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(2));
+               PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(stream_count));
+               PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(0));
+               PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(0));
+               sourceName = gst_object_get_name(GST_OBJECT (IterSubtitleStream->element));
+               PyTuple_SET_ITEM(tuple, 4, PyString_FromString(sourceName));
+               PyList_Append(l, tuple);
+               Py_DECREF(tuple);
+               stream_count++;
+       }
+
+       return l;
+}
index 17a2599..1ab4669 100644 (file)
@@ -5,10 +5,14 @@
 #include <lib/base/message.h>
 #include <lib/service/iservice.h>
 #include <lib/dvb/pmt.h>
+#include <lib/dvb/subtitle.h>
+#include <lib/dvb/teletext.h>
 #include <gst/gst.h>
 
 class eStaticServiceMP3Info;
 
+class eSubtitleWidget;
+
 class eServiceFactoryMP3: public iServiceHandler
 {
        DECLARE_REF(eServiceFactoryMP3);
@@ -40,7 +44,7 @@ public:
 typedef struct _GstElement GstElement;
 
 class eServiceMP3: public iPlayableService, public iPauseableService, 
-       public iServiceInformation, public iSeekableService, public iAudioTrackSelection, public iAudioChannelSelection, public Object
+       public iServiceInformation, public iSeekableService, public iAudioTrackSelection, public iAudioChannelSelection, public iSubtitleOutput, public Object
 {
        DECLARE_REF(eServiceMP3);
 public:
@@ -59,13 +63,13 @@ public:
        RESULT seek(ePtr<iSeekableService> &ptr);
        RESULT audioTracks(ePtr<iAudioTrackSelection> &ptr);
        RESULT audioChannel(ePtr<iAudioChannelSelection> &ptr);
+       RESULT subtitle(ePtr<iSubtitleOutput> &ptr);
 
                // not implemented (yet)
        RESULT frontendInfo(ePtr<iFrontendInformation> &ptr) { ptr = 0; return -1; }
        RESULT subServices(ePtr<iSubserviceList> &ptr) { ptr = 0; return -1; }
        RESULT timeshift(ePtr<iTimeshiftService> &ptr) { ptr = 0; return -1; }
        RESULT cueSheet(ePtr<iCueSheet> &ptr) { ptr = 0; return -1; }
-       RESULT subtitle(ePtr<iSubtitleOutput> &ptr) { ptr = 0; return -1; }
        RESULT audioDelay(ePtr<iAudioDelay> &ptr) { ptr = 0; return -1; }
        RESULT rdsDecoder(ePtr<iRdsDecoder> &ptr) { ptr = 0; return -1; }
        RESULT stream(ePtr<iStreamableService> &ptr) { ptr = 0; return -1; }
@@ -100,6 +104,12 @@ public:
        int getCurrentChannel();
        RESULT selectChannel(int i);
 
+               // iSubtitleOutput
+       RESULT enableSubtitles(eWidget *parent, SWIG_PYOBJECT(ePyObject) entry);
+       RESULT disableSubtitles(eWidget *parent);
+       PyObject *getSubtitleList();
+       PyObject *getCachedSubtitle();
+
        struct audioStream
        {
                GstPad* pad;
@@ -107,13 +117,21 @@ public:
                int type; // mpeg2, ac3, dts, ...
                std::string language_code; /* iso-639, if available. */
        };
+       struct subtitleStream
+       {
+               GstElement* element;
+               std::string language_code; /* iso-639, if available. */
+       };
 private:
        int m_currentAudioStream;
+       int m_currentSubtitleStream;
+       int selectAudioStream(int i);
+       std::vector<audioStream> m_audioStreams;
+       std::vector<subtitleStream> m_subtitleStreams;
+       eSubtitleWidget *m_subtitle_widget;
        int m_currentTrickRatio;
        eTimer m_seekTimeout;
        void eServiceMP3::seekTimeoutCB();
-       int selectAudioStream(int i);
-       std::vector<audioStream> m_audioStreams;
        friend class eServiceFactoryMP3;
        std::string m_filename;
        eServiceMP3(const char *filename);
@@ -133,6 +151,7 @@ private:
        static void gstCBfilterPadAdded(GstElement *filter, GstPad *pad, gpointer user_data); /* for id3demux */
        static void gstCBnewPad(GstElement *decodebin, GstPad *pad, gboolean last, gpointer data); /* for decodebin */
        static void gstCBunknownType(GstElement *decodebin, GstPad *pad, GstCaps *l, gpointer data);
+       static void gstCBsubtitleAvail(GstElement *element, GstBuffer *buffer, GstPad *pad, gpointer user_data);
        void gstPoll(const int&);
 };
 #endif