MythTV  master
atscdescriptors.h
Go to the documentation of this file.
1 // -*- Mode: c++ -*-
2 // Copyright (c) 2003-2004, Daniel Thor Kristjansson
3 #ifndef ATSC_DESCRIPTORS_H
4 #define ATSC_DESCRIPTORS_H
5 
6 #include <array>
7 #include <vector>
8 
9 #include <QString>
10 #include <QMap>
11 
12 #include "mpegdescriptors.h"
13 
14 using IntToBuf = QMap<int, const unsigned char*>;
15 
17 {
18  public:
19  explicit MultipleStringStructure(const unsigned char *data) : m_data(data)
20  {
21  Parse();
22  }
23 
24  uint StringCount(void) const { return m_data[0]; }
25  //uimsbf for (i= 0;i< number_strings;i++) {
26  // ISO_639_language_code 24;
27  int LanguageKey(uint i) const
28  { return iso639_str3_to_key(Offset(i,-1)); }
29  QString LanguageString(uint i) const
30  { return iso639_key_to_str3(LanguageKey(i)); }
33  QString CanonicalLanguageString(uint i) const
35  // uimsbf cc_type 1 3.0
36 
37  // uimsbf number_segments 8;
38  uint SegmentCount(uint i) const { return *(Offset(i,-1)+3); }
39 
40  // uimsbf for (j=0;j<number_segments;j++) {
41  // compression_type 8;
42  uint CompressionType(uint i, uint j) const { return *Offset(i,j); }
43  QString CompressionTypeString(uint i, uint j) const;
44  // uimsbf mode 8;
45  int Mode(int i, int j) const { return *(Offset(i,j)+1); }
46  // uimsbf number_bytes 8;
47  int Bytes(int i, int j) const { return *(Offset(i,j)+2); }
48  // uimsbf for (k= 0;k<number_bytes;k++)
49  // compressed_string_byte [k] 8 bslbf;
50  // }
51  //}
52 
53  uint GetIndexOfBestMatch(QMap<uint,uint> &langPrefs) const;
54  QString GetBestMatch(QMap<uint,uint> &langPrefs) const;
55 
56  QString GetSegment(uint i, uint j) const;
57  QString GetFullString(uint i) const;
58 
59  void Parse(void) const;
60 
61  QString toString() const;
62 
63  private:
64  static QString Uncompressed(const unsigned char *buf, int len, int mode);
65  static uint Index(int i, int j) { return (i<<8)|(j&0xff); }
66  const unsigned char *Offset(int i, int j) const
67  { return m_ptrs[Index(i,j)]; }
68 
69  private:
70  const unsigned char *m_data;
71  mutable IntToBuf m_ptrs;
72 };
73 
75 {
76  public:
77  explicit CaptionServiceDescriptor(const unsigned char *data, int len = 300) :
78  MPEGDescriptor(data, len, DescriptorID::caption_service)
79  {
80  if (m_data && !Parse())
81  m_data = nullptr;
82  }
83  // Name bits loc expected value
84  // descriptor_tag 8 0.0 0x86
85  // descriptor_length 8 1.0
86  // reserved 3 2.0 0x07
87  // number_of_services 5 2.3
88  uint ServicesCount() const { return m_data[2]&0x1f; }
89  //uimsbf for (i=0;i<number_of_services;i++) {
90  // language 8*3 0.0
91  int LanguageKey(int i) const
92  { return iso639_str3_to_key(Offset(i,-1)); }
93  QString LanguageString(int i) const
94  { return iso639_key_to_str3(LanguageKey(i)); }
95  int CanonicalLanguageKey(int i) const
97  QString CanonicalLanguageString(int i) const
99  // uimsbf cc_type 1 3.0
100  bool Type(int i) const
101  { return (((Offset(i,-1)[3])>>7) & 1) != 0; }
102  // bslbf reserved 1 3.1 1
103  // if (cc_type==line21) {
104  // reserved 5 3.2 0x1f
105  // line21_field 1 3.7
106  bool Line21Field(int i) const
107  { return bool(((Offset(i,-1)[3])) & 1); }
108  // } else
109  // cap_service_number 6 3.2
110  int CaptionServiceNumber(int i) const
111  { return ((Offset(i,-1)[3])) & 0x3f; }
112  // easy_reader 1 4.0
113  bool EasyReader(int i) const
114  { return bool(((Offset(i,-1)[4])>>7) & 1); }
115  // wide_aspect_ratio 1 4.1
116  bool WideAspectRatio(int i) const
117  { return bool(((Offset(i,-1)[4])>>6) & 1); }
118  // reserved 14 4.2 0x3fff
119  //} 6.0
120  bool Parse(void);
121  QString toString() const override; // MPEGDescriptor
122 
123  private:
124  static int Index(int i, int j) { return (i<<8) | (j & 0xff); }
125  const unsigned char *Offset(int i, int j) const
126  { return m_ptrs[Index(i,j)]; }
127 
128  private:
130 };
131 
133 {
134  public:
135  explicit ContentAdvisoryDescriptor(const unsigned char *data, int len = 300) :
136  MPEGDescriptor(data, len, DescriptorID::content_advisory)
137  {
138  if (m_data && !Parse())
139  m_data = nullptr;
140  }
141  // Name bits loc expected value
142  // descriptor_tag 8 0.0 0x87
143  // descriptor_length 8 1.0
144  // reserved 2 2.0 0x03
145  // rating_region_count 6 2.2
146  uint RatingRegionCount(void) const { return m_data[2] & 0x3f; }
147  // for (i=0; i<rating_region_count; i++) {
148  // rating_region 8 x+0.0
150  { return *Offset(i,-1); }
151  // rated_dimensions 8 x+1.0
153  { return *(Offset(i,-1) + 1); }
154  // for (j=0;j<rated_dimensions;j++) {
155  // rating_dimension_j 8 y+0.0
157  { return *Offset(i,j); }
158  // reserved 4 y+1.0 0x0f
159  // rating_value 4 y+1.4
160  uint RatingValue(uint i, uint j) const
161  { return (*(Offset(i,j) + 1)) & 0xf; }
162  // }
163  // rating_desc_length 8 x+2+(rated_dimensions*2)+0.0
165  { return (*(Offset(i,-1) + 2 + (RatedDimensions(i)<<1))); }
166  // rating_desc_text x+2+(rated_dimensions*2)+1.0
168  {
169  const unsigned char *data = Offset(i,-1) + 3 + (RatedDimensions(i)<<1);
170  return MultipleStringStructure(data);
171  }
172  // }
173 
174  bool Parse(void);
175  QString toString() const override; // MPEGDescriptor
176  protected:
177  static int Index(int i, int j) { return (i<<8)|(j&0xff); }
178  const unsigned char *Offset(int i, int j) const
179  {
180  IntToBuf::const_iterator it = m_ptrs.find(Index(i,j));
181  return (it != m_ptrs.end()) ? *it : nullptr;
182  }
184 };
185 
187 {
188  public:
189  explicit ComponentNameDescriptor(const unsigned char *data, int len = 300) :
190  MPEGDescriptor(data, len, DescriptorID::component_name)
191  {
192  }
194  {
196  }
197  QString toString() const override // MPEGDescriptor
198  {
199  return QString("Component Name Descriptor %1")
200  .arg(ComponentNameStrings().toString());
201  }
202 };
203 
204 
205 // a_52a.pdf p120, Table A2
207 {
208  public:
209  explicit AC3AudioStreamDescriptor(const unsigned char *data, int len = 300) :
210  MPEGDescriptor(data, len, DescriptorID::ac3_audio_stream) { }
211  // descriptor_tag 8 0.0 0x81
212  // sample_rate_code 3 2.0
213  uint SampleRateCode(void) const { return (m_data[2]>>5)&7; }
214  QString SampleRateCodeString(void) const;
215  // bsid 5 2.3
216  uint bsid(void) const { return m_data[2]&0x1f; }
217  // bit_rate_code 6 3.0
218  uint BitRateCode(void) const { return (m_data[3]>>2)&0x3f; }
219  QString BitRateCodeString(void) const;
220  // surround_mode 2 3.6
221  uint SurroundMode(void) const { return m_data[3]&3; }
222  QString SurroundModeString(void) const;
223  /*
224  000 Major ?
225  001 Major ?
226  010-111 Minor
227  111 Karaoke Mode if acmod >= 0x2. a_52a.pdf p130
228  */
229  // bsmod 3 4.0
230  uint BasicServiceMode(void) const { return (m_data[4]>>5)&7; }
231  // num_channels 4 4.3
232  uint Channels(void) const { return (m_data[4]>>1)&0xf; }
233  QString ChannelsString(void) const;
234 
235  // full service that can be presented alone to listener?
236  // full_svc 1 4.7
237  bool FullService(void) const { return bool((m_data[4])&1); }
238 
239  // langcod 8 5.0
240  // ignore for language specification
241  uint LanguageCode(void) const { return m_data[5]; }
242  // if(num_channels==0) /* 1+1 mode */
243  // langcod2 8 6.0
244  // ignore for language specification
245  uint LanguageCode2(void) const { return m_data[6]; }
246 
247  // if(bsmod<2) {
248  // mainid 3 7.0/6.0
249  uint MainID(void) const
250  {
251  return m_data[(Channels()==0)?7:6]>>5;
252  }
253  // reserved 5 7.3/6.3
254  //uint reserved(void) const { return _data[7]&0x1f; }
255  // } else asvcflags 8 7.0/6.0
256  uint AServiceFlags(void) const
257  {
258  return m_data[(Channels()==0)?7:6];
259  }
260 
261  // textlen 7 8.0/7.0
262  uint TextLength(void) const
263  {
264  return m_data[(Channels()==0)?8:7]>>1;
265  }
266 
267  /* If this bit is a 1 , the text is encoded as 1-byte characters
268  using the ISO Latin-1 alphabet (ISO 8859-1). If this bit is a 0,
269  the text is encoded with 2-byte unicode characters. */
270  // text_code 1 bslbf
271  bool IsTextLatin1(void) const
272  {
273  return bool(m_data[(Channels()==0)?8:7]&1);
274  }
275  // for(i=0; i<M; i++) {
276  // text[i] 8 bslbf
277  // }
278 
279  // NOLINTNEXTLINE(readability-convert-member-functions-to-static)
280  QString Text(void) const
281  { // TODO
282 #if 0
283  char *tmp = new char[TextLength()+2];
284  if (IsTextLatin1())
285  {
286  memcpy(tmp, &m_data[(Channels()==0)?9:8], TextLength());
287  tmp[TextLength()]=0;
288  for (uint i=0; i<TextLength(); i++)
289  if (!tmp[i]) tmp[i]='H';
290  QString str(tmp);
291  delete[] tmp;
292  return str;
293  }
294  else
295  {
296  QString str; int len = TextLength();
297  const unsigned char *buf = (&m_data[(Channels()==0)?9:8]);
298  const unsigned short* ustr =
299  reinterpret_cast<const unsigned short*>(buf);
300  for (int j=0; j<(len>>1); j++)
301  str.append( QChar( (ustr[j]<<8) | (ustr[j]>>8) ) );
302  return str;
303  }
304 #endif
305  return QString("TODO");
306  }
307  // for(i=0; i<N; i++) {
308  // additional_info[i] Nx8 bslbf
309  // }
310 
311  QString toString() const override; // MPEGDescriptor
312 };
313 
319 {
320  explicit ContentIdentifierDescriptor(const unsigned char *data, int len = 300) :
321  MPEGDescriptor(data, len, DescriptorID::atsc_content_identifier) { }
322  // descriptor_tag 8 0.0 0xB6
323  // descriptor_length 8 1.0
324  // content_ID_structure
325  // ID_system 8 2.0
326  // 0x00 ISAN (ISO 15706[1])
327  // 0x01 V-ISAN (ISO 20925-1[2])
328  // 0x02-0xFF ATSC Reserved
329  // ID_length 8 3.0
330  // content_identifier v 4.0
331 };
332 
341 {
342  public:
343  explicit ExtendedChannelNameDescriptor(const unsigned char *data, int len = 300) :
344  MPEGDescriptor(data, len, DescriptorID::extended_channel_name) { }
346  QString LongChannelNameString(void) const;
347  QString toString() const override; // MPEGDescriptor
348 };
349 
350 #endif // ATSC_DESCRIPTORS_H
DescriptorID
Definition: mpegdescriptors.h:20
AC3AudioStreamDescriptor::AC3AudioStreamDescriptor
AC3AudioStreamDescriptor(const unsigned char *data, int len=300)
Definition: atscdescriptors.h:209
ContentAdvisoryDescriptor::toString
QString toString() const override
Definition: atscdescriptors.cpp:228
AC3AudioStreamDescriptor::LanguageCode2
uint LanguageCode2(void) const
Definition: atscdescriptors.h:245
bool
bool
Definition: pxsup2dast.c:30
iso639_key_to_canonical_key
int iso639_key_to_canonical_key(int iso639_2)
Definition: iso639.cpp:118
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
mpegdescriptors.h
AC3AudioStreamDescriptor::IsTextLatin1
bool IsTextLatin1(void) const
Definition: atscdescriptors.h:271
CaptionServiceDescriptor::EasyReader
bool EasyReader(int i) const
Definition: atscdescriptors.h:113
ContentAdvisoryDescriptor::RatingDimension
uint RatingDimension(uint i, uint j) const
Definition: atscdescriptors.h:156
ExtendedChannelNameDescriptor
Provides the long channel name for the virtual channel containing this descriptor.
Definition: atscdescriptors.h:340
ContentAdvisoryDescriptor::Index
static int Index(int i, int j)
Definition: atscdescriptors.h:177
CaptionServiceDescriptor
Definition: atscdescriptors.h:74
IntToBuf
QMap< int, const unsigned char * > IntToBuf
Definition: atscdescriptors.h:14
ContentAdvisoryDescriptor::RatingRegion
uint RatingRegion(uint i) const
Definition: atscdescriptors.h:149
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
AC3AudioStreamDescriptor::LanguageCode
uint LanguageCode(void) const
Definition: atscdescriptors.h:241
ContentAdvisoryDescriptor
Definition: atscdescriptors.h:132
ContentAdvisoryDescriptor::RatingDescriptionLength
uint RatingDescriptionLength(uint i) const
Definition: atscdescriptors.h:164
CaptionServiceDescriptor::CaptionServiceDescriptor
CaptionServiceDescriptor(const unsigned char *data, int len=300)
Definition: atscdescriptors.h:77
ContentAdvisoryDescriptor::RatingDescription
MultipleStringStructure RatingDescription(uint i) const
Definition: atscdescriptors.h:167
MultipleStringStructure::m_ptrs
IntToBuf m_ptrs
Definition: atscdescriptors.h:71
ComponentNameDescriptor::ComponentNameStrings
MultipleStringStructure ComponentNameStrings() const
Definition: atscdescriptors.h:193
MultipleStringStructure::MultipleStringStructure
MultipleStringStructure(const unsigned char *data)
Definition: atscdescriptors.h:19
AC3AudioStreamDescriptor
Definition: atscdescriptors.h:206
MultipleStringStructure::LanguageKey
int LanguageKey(uint i) const
Definition: atscdescriptors.h:27
MultipleStringStructure::StringCount
uint StringCount(void) const
Definition: atscdescriptors.h:24
ContentAdvisoryDescriptor::Offset
const unsigned char * Offset(int i, int j) const
Definition: atscdescriptors.h:178
ExtendedChannelNameDescriptor::ExtendedChannelNameDescriptor
ExtendedChannelNameDescriptor(const unsigned char *data, int len=300)
Definition: atscdescriptors.h:343
ComponentNameDescriptor::ComponentNameDescriptor
ComponentNameDescriptor(const unsigned char *data, int len=300)
Definition: atscdescriptors.h:189
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
CaptionServiceDescriptor::CanonicalLanguageString
QString CanonicalLanguageString(int i) const
Definition: atscdescriptors.h:97
MPEGDescriptor
Definition: mpegdescriptors.h:302
MultipleStringStructure::CanonicalLanguageString
QString CanonicalLanguageString(uint i) const
Definition: atscdescriptors.h:33
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
CaptionServiceDescriptor::LanguageKey
int LanguageKey(int i) const
Definition: atscdescriptors.h:91
ContentAdvisoryDescriptor::RatingValue
uint RatingValue(uint i, uint j) const
Definition: atscdescriptors.h:160
AC3AudioStreamDescriptor::Text
QString Text(void) const
Definition: atscdescriptors.h:280
iso639_key_to_str3
static QString iso639_key_to_str3(int code)
Definition: iso639.h:44
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
ContentIdentifierDescriptor::ContentIdentifierDescriptor
ContentIdentifierDescriptor(const unsigned char *data, int len=300)
Definition: atscdescriptors.h:320
ExtendedChannelNameDescriptor::toString
QString toString() const override
Definition: atscdescriptors.cpp:353
MultipleStringStructure::CompressionType
uint CompressionType(uint i, uint j) const
Definition: atscdescriptors.h:42
ContentIdentifierDescriptor
Definition: atscdescriptors.h:318
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
ComponentNameDescriptor::toString
QString toString() const override
Definition: atscdescriptors.h:197
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
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::ContentAdvisoryDescriptor
ContentAdvisoryDescriptor(const unsigned char *data, int len=300)
Definition: atscdescriptors.h:135
ContentAdvisoryDescriptor::m_ptrs
IntToBuf m_ptrs
Definition: atscdescriptors.h:183
iso639_str3_to_key
static int iso639_str3_to_key(const unsigned char *iso639_2)
Definition: iso639.h:59
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
ComponentNameDescriptor
Definition: atscdescriptors.h:186
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
CaptionServiceDescriptor::CanonicalLanguageKey
int CanonicalLanguageKey(int i) const
Definition: atscdescriptors.h:95
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