MythTV  master
cc608decoder.h
Go to the documentation of this file.
1 // -*- Mode: c++ -*-
2 
3 #ifndef CCDECODER_H_
4 #define CCDECODER_H_
5 
6 #include <cstdint>
7 #include <ctime>
8 
9 #include <array>
10 #include <vector>
11 
12 #include <QString>
13 #include <QRecursiveMutex>
14 #include <QChar>
15 
16 #include "libmythbase/mythchrono.h"
17 
19 {
20  unsigned char row;
21  unsigned char col;
22  unsigned char dbl;
23  unsigned char fg;
24  unsigned char bg;
25  unsigned char len;
26 };
27 
28 struct ccsubtitle
29 {
30  unsigned char row;
31  unsigned char rowcount;
32  unsigned char resumedirect;
33  unsigned char resumetext;
34  unsigned char clr; // clear the display
35  unsigned char len; //length of string to follow
36 };
37 
38 // resumedirect codes
39 enum CC_STYLE : std::uint8_t {
43 };
44 
45 // resumetext special codes
46 static constexpr uint8_t CC_LINE_CONT { 0x02 };
47 static constexpr uint8_t CC_MODE_MASK { 0xf0 };
48 static constexpr uint8_t CC_TXT_MASK { 0x20 };
49 enum CC_MODE : std::uint8_t {
50  CC_CC1 = 0x00,
51  CC_CC2 = 0x10,
52  CC_TXT1 = 0x20,
53  CC_TXT2 = 0x30,
54  CC_CC3 = 0x40,
55  CC_CC4 = 0x50,
56  CC_TXT3 = 0x60,
57  CC_TXT4 = 0x70,
58 };
59 
60 using CC608Seen = std::array<bool,4>;
61 using CC608ProgramType = std::array<QString,96>;
62 using CC608PerField = std::array<int,2>;
63 using CC608PerFieldTc = std::array<std::chrono::milliseconds,2>;
64 using CC608PerMode = std::array<int,8>;
65 using CC608PerModeTc = std::array<std::chrono::milliseconds,8>;
66 
68 {
69  public:
70  virtual ~CC608Input() = default;
71  virtual void AddTextData(unsigned char *buf, int len,
72  std::chrono::milliseconds timecode, char type) = 0;
73 };
74 
75 enum : std::uint8_t
76 {
77  kHasMPAA = 0x1,
78  kHasTPG = 0x2,
81 };
82 enum : std::uint8_t
83 {
88 };
89 
91 {
92  public:
93  explicit CC608Decoder(CC608Input *ccr);
94  CC608Decoder(const CC608Decoder& rhs);
95  ~CC608Decoder();
96 
97  void FormatCC(std::chrono::milliseconds tc, int code1, int code2);
98  void FormatCCField(std::chrono::milliseconds tc, size_t field, int data);
99  bool FalseDup(std::chrono::milliseconds tc, int field, int data);
100 
101  void DecodeVPS(const unsigned char *buf);
102  void DecodeWSS(const unsigned char *buf);
103 
104  void SetIgnoreTimecode(bool val) { m_ignoreTimeCode = val; }
105 
106  uint GetRatingSystems(bool future) const;
107  uint GetRating(uint i, bool future) const;
108  QString GetRatingString(uint i, bool future) const;
109  QString GetProgramName(bool future) const;
110  QString GetProgramType(bool future) const;
111  QString GetXDS(const QString &key) const;
112 
114  void GetServices(std::chrono::seconds seconds, CC608Seen& seen) const;
115 
116  static QString ToASCII(const QString &cc608, bool suppress_unknown);
117 
118  private:
119  QChar CharCC(int code) const { return m_stdChar[code]; }
120  void ResetCC(size_t mode);
121  void BufferCC(size_t mode, int len, int clr);
122  int NewRowCC(size_t mode, int len);
123 
124  QString XDSDecodeString(const std::vector<unsigned char>&buf,
125  uint start, uint end) const;
126  bool XDSDecode(int field, int b1, int b2);
127 
128  bool XDSPacketParseProgram(const std::vector<unsigned char> &xds_buf,
129  bool future);
130  bool XDSPacketParseChannel(const std::vector<unsigned char> &xds_buf);
131  void XDSPacketParse(const std::vector<unsigned char> &xds_buf);
132  bool XDSPacketCRC(const std::vector<unsigned char> &xds_buf);
133 
134  CC608Input *m_reader {nullptr};
135 
136  bool m_ignoreTimeCode {false};
137 
138  std::array<SystemTime,4> m_lastSeen {};
139 
140  // per-field
145  CC608PerField m_ccMode {-1, -1}; // 0=cc1/txt1, 1=cc2/txt2
147  std::array<int,4> m_txtMode { 0, 0, 0, 0};
148 
149  // per-mode state
153  CC608PerMode m_newAttr {0}; // color+italic+underline
162  std::array<QString,8> m_ccBuf;
163 
164  // translation table
165  std::array<QChar,128> m_stdChar;
166 
167  // temporary buffer
168  unsigned char *m_rbuf {nullptr};
171 
172  // VPS data
173  std::array<char,20> m_vpsPrLabel {0};
174  std::array<char,20> m_vpsLabel {0};
175  int m_vpsL {0};
176 
177  // WSS data
179  bool m_wssValid {false};
180 
181  int m_xdsCurService {-1};
182  std::array<std::vector<unsigned char>,7> m_xdsBuf;
185 
186  mutable QRecursiveMutex m_xdsLock;
187  std::array<uint,2> m_xdsRatingSystems {0};
188  std::array<std::array<uint,4>,2> m_xdsRating {{}};
189  std::array<QString,2> m_xdsProgramName;
190  std::array<std::vector<uint>,2> m_xdsProgramType;
191 
192  QString m_xdsNetCall;
193  QString m_xdsNetName;
195 
197 };
198 
199 #endif
CC608Decoder::m_rbuf
unsigned char * m_rbuf
Definition: cc608decoder.h:168
CC608Decoder::m_lastClr
CC608PerModeTc m_lastClr
Definition: cc608decoder.h:161
kHasTPG
@ kHasTPG
Definition: cc608decoder.h:78
CC608Decoder::m_lastCodeTc
CC608PerFieldTc m_lastCodeTc
Definition: cc608decoder.h:144
CC608Decoder::SetIgnoreTimecode
void SetIgnoreTimecode(bool val)
Definition: cc608decoder.h:104
CC608Decoder::m_newCol
CC608PerMode m_newCol
Definition: cc608decoder.h:152
CC608Decoder::m_xdsRatingSystems
std::array< uint, 2 > m_xdsRatingSystems
Definition: cc608decoder.h:187
CC_TXT3
@ CC_TXT3
Definition: cc608decoder.h:56
teletextsubtitle::row
unsigned char row
Definition: cc608decoder.h:20
kHasCanFrench
@ kHasCanFrench
Definition: cc608decoder.h:80
CC608Decoder::m_ccMode
CC608PerField m_ccMode
Definition: cc608decoder.h:145
CC608Decoder::m_xdsCurService
int m_xdsCurService
Definition: cc608decoder.h:181
CC608Decoder::GetRating
uint GetRating(uint i, bool future) const
Definition: cc608decoder.cpp:1017
CC_CC3
@ CC_CC3
Definition: cc608decoder.h:54
CC608PerMode
std::array< int, 8 > CC608PerMode
Definition: cc608decoder.h:64
kRatingMPAA
@ kRatingMPAA
Definition: cc608decoder.h:84
CC_MODE
CC_MODE
Definition: cc608decoder.h:49
CC608Decoder::FormatCC
void FormatCC(std::chrono::milliseconds tc, int code1, int code2)
Definition: cc608decoder.cpp:49
CC608Decoder::m_vpsLabel
std::array< char, 20 > m_vpsLabel
Definition: cc608decoder.h:174
CC_CC4
@ CC_CC4
Definition: cc608decoder.h:55
CC608Decoder
Definition: cc608decoder.h:90
CC_LINE_CONT
static constexpr uint8_t CC_LINE_CONT
Definition: cc608decoder.h:46
kHasCanEnglish
@ kHasCanEnglish
Definition: cc608decoder.h:79
CC608Decoder::m_xdsNetName
QString m_xdsNetName
Definition: cc608decoder.h:193
CC608Input::~CC608Input
virtual ~CC608Input()=default
CC608Decoder::m_vpsL
int m_vpsL
Definition: cc608decoder.h:175
CC608Decoder::m_badVbi
CC608PerField m_badVbi
Definition: cc608decoder.h:141
CC608Input
Definition: cc608decoder.h:67
CC608Decoder::m_xdsProgramTypeString
CC608ProgramType m_xdsProgramTypeString
Definition: cc608decoder.h:196
CC608Decoder::m_rowCount
CC608PerMode m_rowCount
Definition: cc608decoder.h:157
kRatingCanFrench
@ kRatingCanFrench
Definition: cc608decoder.h:87
CC608Decoder::GetProgramName
QString GetProgramName(bool future) const
Definition: cc608decoder.cpp:1059
CC_CC2
@ CC_CC2
Definition: cc608decoder.h:51
CC608Decoder::BufferCC
void BufferCC(size_t mode, int len, int clr)
Definition: cc608decoder.cpp:671
ccsubtitle::len
unsigned char len
Definition: cc608decoder.h:35
CC608Seen
std::array< bool, 4 > CC608Seen
Definition: cc608decoder.h:60
ccsubtitle::clr
unsigned char clr
Definition: cc608decoder.h:34
CC608Decoder::m_wssValid
bool m_wssValid
Definition: cc608decoder.h:179
CC608Decoder::XDSDecode
bool XDSDecode(int field, int b1, int b2)
Definition: cc608decoder.cpp:1131
CC608Decoder::m_ignoreTimeCode
bool m_ignoreTimeCode
Definition: cc608decoder.h:136
CC608Decoder::m_newAttr
CC608PerMode m_newAttr
Definition: cc608decoder.h:153
CC608Decoder::ToASCII
static QString ToASCII(const QString &cc608, bool suppress_unknown)
Definition: cc608decoder.cpp:628
ccsubtitle::resumetext
unsigned char resumetext
Definition: cc608decoder.h:33
CC608Decoder::m_col
CC608PerMode m_col
Definition: cc608decoder.h:156
CC608Decoder::FalseDup
bool FalseDup(std::chrono::milliseconds tc, int field, int data)
Definition: cc608decoder.cpp:546
CC608Decoder::m_lastTc
CC608PerFieldTc m_lastTc
Definition: cc608decoder.h:142
CC608Decoder::m_vpsPrLabel
std::array< char, 20 > m_vpsPrLabel
Definition: cc608decoder.h:173
CC608Decoder::m_txtMode
std::array< int, 4 > m_txtMode
Definition: cc608decoder.h:147
CC608Decoder::m_newRow
CC608PerMode m_newRow
Definition: cc608decoder.h:151
CC608Decoder::m_style
CC608PerMode m_style
Definition: cc608decoder.h:158
CC608Decoder::m_xdsCrcPassed
uint m_xdsCrcPassed
Definition: cc608decoder.h:183
CC_TXT2
@ CC_TXT2
Definition: cc608decoder.h:53
CC_STYLE
CC_STYLE
Definition: cc608decoder.h:39
CC608Decoder::DecodeWSS
void DecodeWSS(const unsigned char *buf)
Definition: cc608decoder.cpp:927
CC608ProgramType
std::array< QString, 96 > CC608ProgramType
Definition: cc608decoder.h:61
CC608Decoder::m_xdsProgramName
std::array< QString, 2 > m_xdsProgramName
Definition: cc608decoder.h:189
CC608Decoder::~CC608Decoder
~CC608Decoder()
Definition: cc608decoder.cpp:44
CC608Decoder::GetRatingString
QString GetRatingString(uint i, bool future) const
Definition: cc608decoder.cpp:1023
CC608Decoder::m_resumeText
CC608PerMode m_resumeText
Definition: cc608decoder.h:160
teletextsubtitle
Definition: cc608decoder.h:18
CC608Decoder::m_timeCode
CC608PerModeTc m_timeCode
Definition: cc608decoder.h:154
CC_STYLE_ROLLUP
@ CC_STYLE_ROLLUP
Definition: cc608decoder.h:42
CC608Decoder::m_lastFormatTc
CC608PerFieldTc m_lastFormatTc
Definition: cc608decoder.h:169
CC608Decoder::GetRatingSystems
uint GetRatingSystems(bool future) const
Definition: cc608decoder.cpp:1011
CC608PerModeTc
std::array< std::chrono::milliseconds, 8 > CC608PerModeTc
Definition: cc608decoder.h:65
CC_TXT4
@ CC_TXT4
Definition: cc608decoder.h:57
CC608Decoder::m_row
CC608PerMode m_row
Definition: cc608decoder.h:155
CC608Decoder::m_xds
CC608PerField m_xds
Definition: cc608decoder.h:146
CC608Decoder::m_xdsNetCall
QString m_xdsNetCall
Definition: cc608decoder.h:192
CC608PerField
std::array< int, 2 > CC608PerField
Definition: cc608decoder.h:62
CC608Decoder::NewRowCC
int NewRowCC(size_t mode, int len)
Definition: cc608decoder.cpp:739
CC608Decoder::m_reader
CC608Input * m_reader
Definition: cc608decoder.h:134
CC608Decoder::m_lineCont
CC608PerMode m_lineCont
Definition: cc608decoder.h:159
CC608Input::AddTextData
virtual void AddTextData(unsigned char *buf, int len, std::chrono::milliseconds timecode, char type)=0
CC_STYLE_POPUP
@ CC_STYLE_POPUP
Definition: cc608decoder.h:40
CC608Decoder::m_ccBuf
std::array< QString, 8 > m_ccBuf
Definition: cc608decoder.h:162
CC608Decoder::m_lastRow
CC608PerMode m_lastRow
Definition: cc608decoder.h:150
CC608Decoder::CharCC
QChar CharCC(int code) const
Definition: cc608decoder.h:119
CC608Decoder::FormatCCField
void FormatCCField(std::chrono::milliseconds tc, size_t field, int data)
Definition: cc608decoder.cpp:100
CC608Decoder::m_xdsCrcFailed
uint m_xdsCrcFailed
Definition: cc608decoder.h:184
CC608Decoder::m_lastFormatData
CC608PerField m_lastFormatData
Definition: cc608decoder.h:170
CC_CC1
@ CC_CC1
Definition: cc608decoder.h:50
CC608Decoder::m_xdsRating
std::array< std::array< uint, 4 >, 2 > m_xdsRating
Definition: cc608decoder.h:188
CC608Decoder::m_xdsTsid
uint m_xdsTsid
Definition: cc608decoder.h:194
teletextsubtitle::dbl
unsigned char dbl
Definition: cc608decoder.h:22
CC608Decoder::m_xdsLock
QRecursiveMutex m_xdsLock
Definition: cc608decoder.h:186
CC_STYLE_PAINT
@ CC_STYLE_PAINT
Definition: cc608decoder.h:41
CC_MODE_MASK
static constexpr uint8_t CC_MODE_MASK
Definition: cc608decoder.h:47
teletextsubtitle::len
unsigned char len
Definition: cc608decoder.h:25
CC608Decoder::ResetCC
void ResetCC(size_t mode)
Definition: cc608decoder.cpp:612
CC_TXT1
@ CC_TXT1
Definition: cc608decoder.h:52
kRatingCanEnglish
@ kRatingCanEnglish
Definition: cc608decoder.h:86
ccsubtitle::rowcount
unsigned char rowcount
Definition: cc608decoder.h:31
teletextsubtitle::col
unsigned char col
Definition: cc608decoder.h:21
mythchrono.h
kHasMPAA
@ kHasMPAA
Definition: cc608decoder.h:77
ccsubtitle::row
unsigned char row
Definition: cc608decoder.h:30
CC608Decoder::XDSPacketCRC
bool XDSPacketCRC(const std::vector< unsigned char > &xds_buf)
Definition: cc608decoder.cpp:1233
CC608Decoder::DecodeVPS
void DecodeVPS(const unsigned char *buf)
Definition: cc608decoder.cpp:889
CC608Decoder::GetXDS
QString GetXDS(const QString &key) const
Definition: cc608decoder.cpp:1081
CC608Decoder::XDSPacketParse
void XDSPacketParse(const std::vector< unsigned char > &xds_buf)
Definition: cc608decoder.cpp:1200
CC608PerFieldTc
std::array< std::chrono::milliseconds, 2 > CC608PerFieldTc
Definition: cc608decoder.h:63
CC_TXT_MASK
static constexpr uint8_t CC_TXT_MASK
Definition: cc608decoder.h:48
CC608Decoder::m_stdChar
std::array< QChar, 128 > m_stdChar
Definition: cc608decoder.h:165
CC608Decoder::XDSPacketParseChannel
bool XDSPacketParseChannel(const std::vector< unsigned char > &xds_buf)
Definition: cc608decoder.cpp:1419
CC608Decoder::m_lastSeen
std::array< SystemTime, 4 > m_lastSeen
Definition: cc608decoder.h:138
CC608Decoder::XDSDecodeString
QString XDSDecodeString(const std::vector< unsigned char > &buf, uint start, uint end) const
Definition: cc608decoder.cpp:968
ccsubtitle
Definition: cc608decoder.h:28
CC608Decoder::GetServices
void GetServices(std::chrono::seconds seconds, CC608Seen &seen) const
Definition: cc608decoder.cpp:55
CC608Decoder::m_xdsBuf
std::array< std::vector< unsigned char >, 7 > m_xdsBuf
Definition: cc608decoder.h:182
teletextsubtitle::bg
unsigned char bg
Definition: cc608decoder.h:24
CC608Decoder::XDSPacketParseProgram
bool XDSPacketParseProgram(const std::vector< unsigned char > &xds_buf, bool future)
Definition: cc608decoder.cpp:1254
CC608Decoder::m_xdsProgramType
std::array< std::vector< uint >, 2 > m_xdsProgramType
Definition: cc608decoder.h:190
CC608Decoder::m_wssFlags
uint m_wssFlags
Definition: cc608decoder.h:178
CC608Decoder::CC608Decoder
CC608Decoder(CC608Input *ccr)
Definition: cc608decoder.cpp:23
uint
unsigned int uint
Definition: freesurround.h:24
CC608Decoder::GetProgramType
QString GetProgramType(bool future) const
Definition: cc608decoder.cpp:1065
ccsubtitle::resumedirect
unsigned char resumedirect
Definition: cc608decoder.h:32
kRatingTPG
@ kRatingTPG
Definition: cc608decoder.h:85
teletextsubtitle::fg
unsigned char fg
Definition: cc608decoder.h:23
CC608Decoder::m_lastCode
CC608PerField m_lastCode
Definition: cc608decoder.h:143