[cosmetics] update date in GPL header
[vuplus_xbmc] / xbmc / cores / dvdplayer / DVDSubtitles / DVDSubtitleParserMPL2.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://www.xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include "DVDSubtitleParserMPL2.h"
22 #include "DVDCodecs/Overlay/DVDOverlayText.h"
23 #include "DVDClock.h"
24 #include "utils/RegExp.h"
25 #include "DVDStreamInfo.h"
26 #include "utils/StdString.h"
27 #include "DVDSubtitleTagMicroDVD.h"
28
29 using namespace std;
30
31 CDVDSubtitleParserMPL2::CDVDSubtitleParserMPL2(CDVDSubtitleStream* stream, const string& filename)
32     : CDVDSubtitleParserText(stream, filename), m_framerate(DVD_TIME_BASE / 10.0)
33 {
34
35 }
36
37 CDVDSubtitleParserMPL2::~CDVDSubtitleParserMPL2()
38 {
39   Dispose();
40 }
41
42 bool CDVDSubtitleParserMPL2::Open(CDVDStreamInfo &hints)
43 {
44   if (!CDVDSubtitleParserText::Open())
45     return false;
46
47   // MPL2 is time-based, with 0.1s accuracy
48   m_framerate = DVD_TIME_BASE / 10.0;
49
50   char line[1024];
51
52   CRegExp reg;
53   if (!reg.RegComp("\\[([0-9]+)\\]\\[([0-9]+)\\]"))
54     return false;
55   CDVDSubtitleTagMicroDVD TagConv;
56
57   while (m_pStream->ReadLine(line, sizeof(line)))
58   {
59     if ((strlen(line) > 0) && (line[strlen(line) - 1] == '\r'))
60       line[strlen(line) - 1] = 0;
61
62     int pos = reg.RegFind(line);
63     if (pos > -1)
64     {
65       const char* text = line + pos + reg.GetFindLen();
66       std::string startFrame = reg.GetReplaceString("\\1");
67       std::string endFrame   = reg.GetReplaceString("\\2");
68       CDVDOverlayText* pOverlay = new CDVDOverlayText();
69       pOverlay->Acquire(); // increase ref count with one so that we can hold a handle to this overlay
70
71       pOverlay->iPTSStartTime = m_framerate * atoi(startFrame.c_str());
72       pOverlay->iPTSStopTime  = m_framerate * atoi(endFrame.c_str());
73
74       TagConv.ConvertLine(pOverlay, text, strlen(text));
75       m_collection.Add(pOverlay);
76     }
77   }
78
79   return true;
80 }
81