MythTV  master
atscdescriptors.cpp
Go to the documentation of this file.
1 // -*- Mode: c++ -*-
2 // Copyright (c) 2003-2004, Daniel Thor Kristjansson
3 
4 #include <algorithm>
5 
6 #include "libmythbase/iso639.h"
8 
9 #include "atscdescriptors.h"
10 #include "atsc_huffman.h"
11 
13 {
14  uint ct = CompressionType(i, j);
15  if (0 == ct)
16  return {"no compression"};
17  if (1 == ct)
18  return {"Huffman Coding using C.4, C.5"};
19  if (2 == ct)
20  return {"Huffman Coding using C.6, C.7"};
21  if (ct < 0xaf)
22  return {"reserved"};
23  return {"compression not used by ATSC in North America, unknown"};
24 }
25 
27 {
28  QString str;
29  if (1 == StringCount() && 1 == SegmentCount(0))
30  {
31  str.append(QString("lang(%1) ").arg(LanguageString(0)));
32  if (0 != Bytes(0, 0))
33  str.append(GetSegment(0, 0));
34  return str;
35  }
36 
37  str.append(QString("MultipleStringStructure count(%1)")
38  .arg(StringCount()));
39 
40  for (uint i = 0; i < StringCount(); i++)
41  {
42  str.append(QString(" String #%1 lang(%2:%3)")
43  .arg(i).arg(LanguageString(i))
44  .arg(LanguageKey(i)));
45 
46  if (SegmentCount(i) > 1)
47  str.append(QString(" segment count(%1)").arg(SegmentCount(i)));
48 
49  for (uint j=0; j<SegmentCount(i); j++)
50  str.append(QString(" Segment #%1 ct(%2) str(%3)").arg(j)
51  .arg(CompressionType(i, j)).arg(GetSegment(i, j)));
52  }
53 
54  return str;
55 }
56 
57 static uint maxPriority(const QMap<uint,uint> &langPrefs)
58 {
59  uint max_pri = 0;
60  for (uint pref : qAsConst(langPrefs))
61  max_pri = std::max(max_pri, pref);
62  return max_pri;
63 }
64 
66  QMap<uint,uint> &langPrefs) const
67 {
68  uint match_idx = 0;
69  uint match_pri = 0;
70 
71  for (uint i = 0; i < StringCount(); i++)
72  {
73  QMap<uint,uint>::const_iterator it =
74  langPrefs.constFind(CanonicalLanguageKey(i));
75  if ((it != langPrefs.constEnd()) && (*it > match_pri))
76  {
77  match_idx = i;
78  match_pri = *it;
79  }
80  }
81 
82  if (match_pri)
83  return match_idx;
84 
85  if (StringCount())
86  langPrefs[CanonicalLanguageKey(0)] = maxPriority(langPrefs) + 1;
87 
88  return 0;
89 }
90 
91 QString MultipleStringStructure::GetBestMatch(QMap<uint,uint> &langPrefs) const
92 {
93  if (StringCount())
94  return GetFullString(GetIndexOfBestMatch(langPrefs));
95  return {};
96 }
97 
99 {
100  const unsigned char* buf = (Offset(i, j)+3);
101  int len = Bytes(i, j);
102 
103  if (len <= 0)
104  return "";
105 
106  int ct = CompressionType(i, j);
107 
108  if (ct == 0)
109  return Uncompressed(buf, len, Mode(i, j));
110 
111  if (ct < 3)
112  return atsc_huffman1_to_string(buf, len, ct);
113 
114  return QString("MSS unknown text compression %1").arg(ct);
115 }
116 
118 {
119  QString tmp = "";
120  for (uint j = 0; j < SegmentCount(i); j++)
121  tmp += GetSegment(i, j);
122  return tmp.simplified();
123 }
124 
126  const unsigned char* buf, int len, int mode) {
127 
128  QString str=QString("");
129  if (mode<=6 ||
130  (9<=mode && mode<=0xe) ||
131  (0x10==mode) ||
132  (0x20<=mode && mode<=0x27) ||
133  (0x30<=mode && mode<=0x33)) { // basic runlength encoding
134  int hb=mode<<8;
135  for (int j=0; j<len; j++)
136  {
137 #if 0
138  LOG(VB_GENERAL, LOG_DEBUG, QString("str.append(0x%1:0x%2) -> %3")
139  .arg(mode, 0, 16) .arg(buf[j], 0, 16) .arg(QChar(hb|buf[j])));
140 #endif
141  if (hb|buf[j])
142  str.append( QChar( hb|buf[j] ) );
143  }
144  } else if (mode==0x3e) {
145  // Standard Compression Scheme for Unicode (SCSU)
146  str=QString("TODO SCSU encoding");
147  } else if (mode==0x3f) { // Unicode, UTF-16 Form
148  const auto* ustr = reinterpret_cast<const unsigned short*>(buf);
149  for (int j=0; j<(len>>1); j++)
150  str.append( QChar( (ustr[j]<<8) | (ustr[j]>>8) ) );
151  } else if (0x40<=mode && mode<=0x41)
152  str = QString("TODO Tawain Characters");
153  else if (0x48==mode)
154  str = QString("TODO South Korean Characters");
155  else
156  str = QString("unknown character encoding mode(%0)").arg(mode);
157  return str;
158 }
159 
161 {
162  m_ptrs.clear();
163  m_ptrs[Index(0,-1)] = m_data + 1;
164  for (uint i = 0; i < StringCount(); i++)
165  {
166  m_ptrs[Index(i,0)] = Offset(i,-1) + 4;
167  uint j = 0;
168  for (; j < SegmentCount(i); j++)
169  m_ptrs[Index(i,j+1)] = Offset(i,j) + Bytes(i,j) + 3;
170  m_ptrs[Index(i+1,-1)] = Offset(i,j);
171  }
172 }
173 
175 {
176  m_ptrs.clear();
177  m_ptrs[Index(0,-1)] = m_data+3;
178 
179  for (uint i = 0; i < ServicesCount(); i++)
180  m_ptrs[Index(i+1,-1)] = Offset(i,-1) + 6;
181 
182  return true;
183 }
184 
186 {
187  QString str("Caption Service Descriptor ");
188  str.append(QString("services(%2)").arg(ServicesCount()));
189 
190  for (uint i = 0; i < ServicesCount(); i++)
191  {
192  str.append(QString("\n lang(%1) type(%2) ")
193  .arg(LanguageString(i)).arg(static_cast<int>(Type(i))));
194  str.append(QString("easy_reader(%1) wide(%2) ")
195  .arg(static_cast<int>(EasyReader(i))).arg(static_cast<int>(WideAspectRatio(i))));
196  if (Type(i))
197  {
198  str.append(QString("service_num(%1)")
199  .arg(CaptionServiceNumber(i)));
200  }
201  else
202  {
203  str.append(QString("line_21_field(%1)").arg(static_cast<int>(Line21Field(i))));
204  }
205  }
206 
207  return str;
208 }
209 
211 {
212  m_ptrs.clear();
213  m_ptrs[Index(0,-1)] = m_data + 2;
214 
215  for (uint i = 0; i < RatingRegionCount(); i++)
216  {
217  m_ptrs[Index(i,0)] = Offset(i,-1)+2;
218  uint j = 0;
219  for (; j < RatedDimensions(i); j++)
220  m_ptrs[Index(i,j+1)] = Offset(i,j) + 2;
221  const unsigned char *tmp = Offset(i,-1) + 3 + (RatedDimensions(i)<<1);
222  uint len = RatingDescriptionLength(i);
223  m_ptrs[Index(i+1,-1)] = tmp + len;
224  }
225 
226  return true;
227 }
228 
230 {
231  return "ContentAdvisoryDescriptor::toString(): Not implemented";
232 }
233 
235 {
236  static const std::array<const std::string,8> s_asd
237  {
238  "48kbps", "44.1kbps", "32kbps", "Reserved",
239  "48kbps or 44.1kbps", "48kbps or 32kbps",
240  "44.1kbps or 32kbps", "48kbps or 44.1kbps or 32kbps"
241  };
242  return QString::fromStdString(s_asd[SampleRateCode()]);
243 }
244 
246 {
247  static const std::array<const std::string,19> s_ebr
248  {
249  "=32kbps", "=40kbps", "=48kbps", "=56kbps", "=64kbps",
250  "=80kbps", "=96kbps", "=112kbps", "=128kbps", "=160kbps",
251  "=192kbps", "=224kbps", "=256kbps", "=320kbps", "=384kbps",
252  "=448kbps", "=512kbps", "=576kbps", "=640kbps"
253  };
254  static const std::array<const std::string,19> s_ubr
255  {
256  "<=32kbps", "<=40kbps", "<=48kbps", "<=56kbps", "<=64kbps",
257  "<=80kbps", "<=96kbps", "<=112kbps", "<=128kbps", "<=160kbps",
258  "<=192kbps","<=224kbps", "<=256kbps", "<=320kbps", "<=384kbps",
259  "<=448kbps","<=512kbps", "<=576kbps", "<=640kbps"
260  };
261 
262  if (BitRateCode() <= 18)
263  return QString::fromStdString(s_ebr[BitRateCode()]);
264  if ((BitRateCode() >= 32) && (BitRateCode() <= 50))
265  return QString::fromStdString(s_ubr[BitRateCode()-32]);
266  return {"Unknown Bit Rate Code"};
267 }
268 
270 {
271  static const std::array<const std::string,4> s_sms
272  {
273  "Not indicated",
274  "Not Dolby surround encoded",
275  "Dolby surround encoded",
276  "Reserved",
277  };
278  return QString::fromStdString(s_sms[SurroundMode()]);
279 }
280 
282 {
283  static const std::array<const std::string,16> s_cs
284  {
285  "1 + 1", "1/0", "2/0", "3/0",
286  "2/1", "3/1", "2/2 ", "3/2",
287  "1", "<= 2", "<= 3", "<= 4",
288  "<= 5", "<= 6", "Reserved", "Reserved"
289  };
290  return QString::fromStdString(s_cs[Channels()]);
291 }
292 
294 {
295  QString str;
296  str.append(QString("AC-3 Audio Stream Descriptor "));
297  str.append(QString(" full_srv(%1) sample_rate(%2) bit_rate(%3, %4)\n")
298  .arg(static_cast<int>(FullService()))
299  .arg(SampleRateCodeString(),
301  .arg(BitRateCode()));
302  str.append(QString(" bsid(%1) bs_mode(%2) channels(%3) Dolby(%4)\n")
303  .arg(bsid()).arg(BasicServiceMode())
305 
306  /*
307  str.append(QString(" language code: %1").arg(languageCode()));
308  if (0==channels()) {
309  str.append(QString(" language code 2: %1").arg(languageCode2()));
310  }
311  */
312 
313  if (BasicServiceMode() < 2)
314  str.append(QString(" mainID(%1) ").arg(MainID()));
315  else
316  str.append(QString(" associated_service(0x%1) ")
317  .arg(AServiceFlags(),0,16));
318 
319  if (TextLength())
320  {
321  str.append(QString("isLatin-1(%1) ")
322  .arg(IsTextLatin1() ? "true" : "false"));
323  str.append(QString("text_length(%1) ").arg(TextLength()));
324  str.append(QString("text(%1)").arg(Text()));
325  }
326  return str;
327 }
328 
334  void) const
335 {
336  return MultipleStringStructure(m_data + 2);
337 }
338 
344 {
345  QString str = "";
347 
348  for (uint i = 0; i < mstr.StringCount(); i++)
349  str += mstr.GetFullString(i);
350 
351  return str;
352 }
353 
355 {
356  return QString("ExtendedChannelNameDescriptor: '%1'")
357  .arg(LongChannelNameString());
358 }
ContentAdvisoryDescriptor::toString
QString toString() const override
Definition: atscdescriptors.cpp:229
maxPriority
static uint maxPriority(const QMap< uint, uint > &langPrefs)
Definition: atscdescriptors.cpp:57
MultipleStringStructure::LanguageString
QString LanguageString(uint i) const
Definition: atscdescriptors.h:29
CaptionServiceDescriptor::Offset
const unsigned char * Offset(int i, int j) const
Definition: atscdescriptors.h:125
AC3AudioStreamDescriptor::IsTextLatin1
bool IsTextLatin1(void) const
Definition: atscdescriptors.h:271
CaptionServiceDescriptor::EasyReader
bool EasyReader(int i) const
Definition: atscdescriptors.h:113
ContentAdvisoryDescriptor::Index
static int Index(int i, int j)
Definition: atscdescriptors.h:177
MultipleStringStructure::Index
static uint Index(int i, int j)
Definition: atscdescriptors.h:65
ContentAdvisoryDescriptor::RatedDimensions
uint RatedDimensions(uint i) const
Definition: atscdescriptors.h:152
MultipleStringStructure::Mode
int Mode(int i, int j) const
Definition: atscdescriptors.h:45
ContentAdvisoryDescriptor::RatingDescriptionLength
uint RatingDescriptionLength(uint i) const
Definition: atscdescriptors.h:164
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
MultipleStringStructure::m_ptrs
IntToBuf m_ptrs
Definition: atscdescriptors.h:71
MultipleStringStructure::LanguageKey
int LanguageKey(uint i) const
Definition: atscdescriptors.h:27
MultipleStringStructure::StringCount
uint StringCount(void) const
Definition: atscdescriptors.h:24
atsc_huffman.h
ContentAdvisoryDescriptor::Offset
const unsigned char * Offset(int i, int j) const
Definition: atscdescriptors.h:178
atscdescriptors.h
CaptionServiceDescriptor::WideAspectRatio
bool WideAspectRatio(int i) const
Definition: atscdescriptors.h:116
tmp
static guint32 * tmp
Definition: goom_core.cpp:26
ContentAdvisoryDescriptor::RatingRegionCount
uint RatingRegionCount(void) const
Definition: atscdescriptors.h:146
ExtendedChannelNameDescriptor::LongChannelName
MultipleStringStructure LongChannelName(void) const
Returns a MultipleStringStructure representing the long name of the associated channel.
Definition: atscdescriptors.cpp:333
CaptionServiceDescriptor::Line21Field
bool Line21Field(int i) const
Definition: atscdescriptors.h:106
AC3AudioStreamDescriptor::BitRateCode
uint BitRateCode(void) const
Definition: atscdescriptors.h:218
MultipleStringStructure::Uncompressed
static QString Uncompressed(const unsigned char *buf, int len, int mode)
Definition: atscdescriptors.cpp:125
CaptionServiceDescriptor::m_ptrs
IntToBuf m_ptrs
Definition: atscdescriptors.h:129
AC3AudioStreamDescriptor::BasicServiceMode
uint BasicServiceMode(void) const
Definition: atscdescriptors.h:230
mythlogging.h
AC3AudioStreamDescriptor::SurroundMode
uint SurroundMode(void) const
Definition: atscdescriptors.h:221
MPEGDescriptor::m_data
const unsigned char * m_data
Definition: mpegdescriptors.h:370
AC3AudioStreamDescriptor::SurroundModeString
QString SurroundModeString(void) const
Definition: atscdescriptors.cpp:269
MultipleStringStructure::Parse
void Parse(void) const
Definition: atscdescriptors.cpp:160
MultipleStringStructure::SegmentCount
uint SegmentCount(uint i) const
Definition: atscdescriptors.h:38
AC3AudioStreamDescriptor::TextLength
uint TextLength(void) const
Definition: atscdescriptors.h:262
AC3AudioStreamDescriptor::ChannelsString
QString ChannelsString(void) const
Definition: atscdescriptors.cpp:281
MultipleStringStructure
Definition: atscdescriptors.h:16
AC3AudioStreamDescriptor::Text
QString Text(void) const
Definition: atscdescriptors.h:280
uint
unsigned int uint
Definition: compat.h:79
AC3AudioStreamDescriptor::MainID
uint MainID(void) const
Definition: atscdescriptors.h:249
CaptionServiceDescriptor::CaptionServiceNumber
int CaptionServiceNumber(int i) const
Definition: atscdescriptors.h:110
AC3AudioStreamDescriptor::toString
QString toString() const override
Definition: atscdescriptors.cpp:293
ExtendedChannelNameDescriptor::toString
QString toString() const override
Definition: atscdescriptors.cpp:354
MultipleStringStructure::CompressionType
uint CompressionType(uint i, uint j) const
Definition: atscdescriptors.h:42
MultipleStringStructure::Bytes
int Bytes(int i, int j) const
Definition: atscdescriptors.h:47
CaptionServiceDescriptor::toString
QString toString() const override
Definition: atscdescriptors.cpp:185
AC3AudioStreamDescriptor::AServiceFlags
uint AServiceFlags(void) const
Definition: atscdescriptors.h:256
CaptionServiceDescriptor::LanguageString
QString LanguageString(int i) const
Definition: atscdescriptors.h:93
ContentAdvisoryDescriptor::Parse
bool Parse(void)
Definition: atscdescriptors.cpp:210
MultipleStringStructure::m_data
const unsigned char * m_data
Definition: atscdescriptors.h:70
MultipleStringStructure::GetFullString
QString GetFullString(uint i) const
Definition: atscdescriptors.cpp:117
CaptionServiceDescriptor::Type
bool Type(int i) const
Definition: atscdescriptors.h:100
CaptionServiceDescriptor::Parse
bool Parse(void)
Definition: atscdescriptors.cpp:174
MultipleStringStructure::GetIndexOfBestMatch
uint GetIndexOfBestMatch(QMap< uint, uint > &langPrefs) const
Definition: atscdescriptors.cpp:65
AC3AudioStreamDescriptor::SampleRateCodeString
QString SampleRateCodeString(void) const
Definition: atscdescriptors.cpp:234
CaptionServiceDescriptor::ServicesCount
uint ServicesCount() const
Definition: atscdescriptors.h:88
iso639.h
ISO 639-1 and ISO 639-2 support functions.
MultipleStringStructure::CanonicalLanguageKey
int CanonicalLanguageKey(uint i) const
Definition: atscdescriptors.h:31
MultipleStringStructure::GetSegment
QString GetSegment(uint i, uint j) const
Definition: atscdescriptors.cpp:98
MultipleStringStructure::CompressionTypeString
QString CompressionTypeString(uint i, uint j) const
Definition: atscdescriptors.cpp:12
ContentAdvisoryDescriptor::m_ptrs
IntToBuf m_ptrs
Definition: atscdescriptors.h:183
AC3AudioStreamDescriptor::BitRateCodeString
QString BitRateCodeString(void) const
Definition: atscdescriptors.cpp:245
MultipleStringStructure::GetBestMatch
QString GetBestMatch(QMap< uint, uint > &langPrefs) const
Definition: atscdescriptors.cpp:91
AC3AudioStreamDescriptor::Channels
uint Channels(void) const
Definition: atscdescriptors.h:232
AC3AudioStreamDescriptor::FullService
bool FullService(void) const
Definition: atscdescriptors.h:237
AC3AudioStreamDescriptor::bsid
uint bsid(void) const
Definition: atscdescriptors.h:216
ExtendedChannelNameDescriptor::LongChannelNameString
QString LongChannelNameString(void) const
Convenience function that returns a QString comprising a concatenation of all the segments in the Lon...
Definition: atscdescriptors.cpp:343
atsc_huffman1_to_string
QString atsc_huffman1_to_string(const unsigned char *compressed, uint size, uint table_index)
Definition: atsc_huffman.cpp:2211
MultipleStringStructure::toString
QString toString() const
Definition: atscdescriptors.cpp:26
MultipleStringStructure::Offset
const unsigned char * Offset(int i, int j) const
Definition: atscdescriptors.h:66
AC3AudioStreamDescriptor::SampleRateCode
uint SampleRateCode(void) const
Definition: atscdescriptors.h:213
CaptionServiceDescriptor::Index
static int Index(int i, int j)
Definition: atscdescriptors.h:124