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