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