1 /*****************************************************************************
2  * desc_5a.h: ETSI EN 300 468 Descriptor 0x5a: Terrestrial delivery system
3  *****************************************************************************
4  * Copyright (C) 2009-2010 VideoLAN
5  *
6  * Authors: Christophe Massiot <massiot@via.ecp.fr>
7  *          Georgi Chorbadzhiyski <georgi@unixsol.org>
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining
10  * a copy of this software and associated documentation files (the
11  * "Software"), to deal in the Software without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject
15  * to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27  *****************************************************************************/
28 
29 /*
30  * Normative references:
31  *  - ETSI EN 300 468 V1.11.1 (2010-04) (SI in DVB systems)
32  */
33 
34 #ifndef __BITSTREAM_DVB_DESC_5A_H__
35 #define __BITSTREAM_DVB_DESC_5A_H__
36 
37 #include <inttypes.h> /* PRIu64 */
38 
39 #include <bitstream/common.h>
40 #include <bitstream/mpeg/psi/descriptors.h>
41 #include <bitstream/dvb/si/desc_43.h>
42 
43 #ifdef __cplusplus
44 extern "C"
45 {
46 #endif
47 
48 /*****************************************************************************
49  * Descriptor 0x5a: Terrestrial delivery system descriptor
50  *****************************************************************************/
51 #define DESC5A_HEADER_SIZE      (DESC_HEADER_SIZE + 11)
52 
desc5a_init(uint8_t * p_desc)53 static inline void desc5a_init(uint8_t *p_desc)
54 {
55     desc_set_tag(p_desc, 0x5a);
56     desc_set_length(p_desc, DESC5A_HEADER_SIZE - DESC_HEADER_SIZE);
57     p_desc[ 6] = 0x03;
58     p_desc[ 9] = 0xff;
59     p_desc[10] = 0xff;
60     p_desc[11] = 0xff;
61     p_desc[12] = 0xff;
62 }
63 
desc5a_get_frequency(const uint8_t * p_desc)64 static inline uint64_t desc5a_get_frequency(const uint8_t *p_desc)
65 {
66     return (((uint64_t)p_desc[2] << 24) | (p_desc[3] << 16) | (p_desc[4] << 8)
67              | p_desc[5]) * 10; /* Hz */
68 }
69 
desc5a_set_frequency(uint8_t * p_desc,uint32_t i_freq)70 static inline void desc5a_set_frequency(uint8_t *p_desc, uint32_t i_freq)
71 {
72     p_desc[2] = (i_freq >> 24) & 0xff;
73     p_desc[3] = (i_freq >> 16) & 0xff;
74     p_desc[4] = (i_freq >>  8) & 0xff;
75     p_desc[5] = i_freq         & 0xff;
76 }
77 
desc5a_get_bandwidth(const uint8_t * p_desc)78 static inline uint8_t desc5a_get_bandwidth(const uint8_t *p_desc)
79 {
80     return p_desc[6] >> 5;
81 }
82 
desc5a_set_bandwidth(uint8_t * p_desc,uint8_t i_bandwidth)83 static inline void desc5a_set_bandwidth(uint8_t *p_desc, uint8_t i_bandwidth)
84 {
85     p_desc[6] = (p_desc[6] &~ 0xe0) | (i_bandwidth << 5); // 111xxxxx
86 }
87 
desc5a_get_priority(const uint8_t * p_desc)88 static inline bool desc5a_get_priority(const uint8_t *p_desc)
89 {
90     return !!((p_desc[6] >> 4) & 0x1);
91 }
92 
desc5a_set_priority(uint8_t * p_desc,bool b_priority)93 static inline void desc5a_set_priority(uint8_t *p_desc, bool b_priority)
94 {
95     p_desc[6] = b_priority ? (p_desc[6] | 0x10) : (p_desc[6] &~ 0x10); // xxx1xxxx
96 }
97 
98 /* ! inverted logic ! */
desc5a_get_timeslicing(const uint8_t * p_desc)99 static inline bool desc5a_get_timeslicing(const uint8_t *p_desc)
100 {
101     return !((p_desc[6] >> 3) & 0x1);
102 }
103 
desc5a_set_timeslicing(uint8_t * p_desc,bool b_timeslicing)104 static inline void desc5a_set_timeslicing(uint8_t *p_desc, bool b_timeslicing)
105 {
106     p_desc[6] = !b_timeslicing ? (p_desc[6] | 0x08) : (p_desc[6] &~ 0x08); // xxxx1xxx
107 }
108 
109 /* ! inverted logic ! */
desc5a_get_mpefec(const uint8_t * p_desc)110 static inline bool desc5a_get_mpefec(const uint8_t *p_desc)
111 {
112     return !((p_desc[6] >> 2) & 0x1);
113 }
114 
desc5a_set_mpefec(uint8_t * p_desc,bool b_mpefec)115 static inline void desc5a_set_mpefec(uint8_t *p_desc, bool b_mpefec)
116 {
117     p_desc[6] = !b_mpefec ? (p_desc[6] | 0x04) : (p_desc[6] &~ 0x04); // xxxxx1xx
118 }
119 
desc5a_get_constellation(const uint8_t * p_desc)120 static inline uint8_t desc5a_get_constellation(const uint8_t *p_desc)
121 {
122     return p_desc[7] >> 6;
123 }
124 
desc5a_set_constellation(uint8_t * p_desc,uint8_t i_constellation)125 static inline void desc5a_set_constellation(uint8_t *p_desc, uint8_t i_constellation)
126 {
127     p_desc[7] = (p_desc[7] &~ 0xc0) | (i_constellation << 6); // 11xxxxxx
128 }
129 
desc5a_get_hierarchy(const uint8_t * p_desc)130 static inline uint8_t desc5a_get_hierarchy(const uint8_t *p_desc)
131 {
132     return (p_desc[7] >> 3) & 0x7;
133 }
134 
desc5a_set_hierarchy(uint8_t * p_desc,uint8_t i_hierarchy)135 static inline void desc5a_set_hierarchy(uint8_t *p_desc, uint8_t i_hierarchy)
136 {
137     p_desc[7] = (p_desc[7] &~ 0x38) | ((i_hierarchy & 0x07) << 3); // xx111xxx
138 }
139 
desc5a_get_coderatehp(const uint8_t * p_desc)140 static inline uint8_t desc5a_get_coderatehp(const uint8_t *p_desc)
141 {
142     return p_desc[7] & 0x7;
143 }
144 
desc5a_set_coderatehp(uint8_t * p_desc,uint8_t i_coderatehp)145 static inline void desc5a_set_coderatehp(uint8_t *p_desc, uint8_t i_coderatehp)
146 {
147     p_desc[7] = (p_desc[7] &~ 0x07) | (i_coderatehp & 0x07); // xxxxx111
148 }
149 
desc5a_get_coderatelp(const uint8_t * p_desc)150 static inline uint8_t desc5a_get_coderatelp(const uint8_t *p_desc)
151 {
152     return p_desc[8] >> 5;
153 }
154 
desc5a_set_coderatelp(uint8_t * p_desc,uint8_t i_coderatelp)155 static inline void desc5a_set_coderatelp(uint8_t *p_desc, uint8_t i_coderatelp)
156 {
157     p_desc[8] = (p_desc[8] &~ 0xE0) | (i_coderatelp << 5); // 111xxxxx
158 }
159 
desc5a_get_guard(const uint8_t * p_desc)160 static inline uint8_t desc5a_get_guard(const uint8_t *p_desc)
161 {
162     return (p_desc[8] >> 3) & 0x3;
163 }
164 
desc5a_set_guard(uint8_t * p_desc,uint8_t i_guard)165 static inline void desc5a_set_guard(uint8_t *p_desc, uint8_t i_guard)
166 {
167     p_desc[8] = (p_desc[8] &~ 0x18) | ((i_guard & 0x03) << 3); // xxx11xxx
168 }
169 
desc5a_get_transmission(const uint8_t * p_desc)170 static inline uint8_t desc5a_get_transmission(const uint8_t *p_desc)
171 {
172     return (p_desc[8] >> 1) & 0x3;
173 }
174 
desc5a_set_transmission(uint8_t * p_desc,uint8_t i_transmission)175 static inline void desc5a_set_transmission(uint8_t *p_desc, uint8_t i_transmission)
176 {
177     p_desc[8] = (p_desc[8] &~ 0x06) | ((i_transmission & 0x03) << 1); // xxxxx11x
178 }
179 
desc5a_get_otherfrequency(const uint8_t * p_desc)180 static inline bool desc5a_get_otherfrequency(const uint8_t *p_desc)
181 {
182     return !!(p_desc[8] & 0x1);
183 }
184 
desc5a_set_otherfrequency(uint8_t * p_desc,uint8_t b_otherfrequency)185 static inline void desc5a_set_otherfrequency(uint8_t *p_desc, uint8_t b_otherfrequency)
186 {
187     p_desc[8] = b_otherfrequency ? (p_desc[8] | 0x01) : (p_desc[8] &~ 0x01); // xxxxxxx1
188 }
189 
desc5a_validate(const uint8_t * p_desc)190 static inline bool desc5a_validate(const uint8_t *p_desc)
191 {
192     return desc_get_length(p_desc) >= DESC5A_HEADER_SIZE - DESC_HEADER_SIZE;
193 }
194 
desc5a_print(const uint8_t * p_desc,f_print pf_print,void * opaque,print_type_t i_print_type)195 static inline void desc5a_print(const uint8_t *p_desc, f_print pf_print,
196                                 void *opaque, print_type_t i_print_type)
197 {
198     uint8_t i_bandwidth = desc5a_get_bandwidth(p_desc);
199     uint8_t i_constellation = desc5a_get_constellation(p_desc);
200     const char *psz_constellation = "reserved";
201     uint8_t i_hierarchy = desc5a_get_hierarchy(p_desc);
202     const char *psz_hierarchy = "";
203     bool b_hierarchy = !!(i_hierarchy & 0x3);
204     uint8_t i_guard = desc5a_get_guard(p_desc);
205     const char *psz_guard = "";
206     uint8_t i_transmission = desc5a_get_transmission(p_desc);
207     const char *psz_transmission = "reserved";
208 
209     switch (i_bandwidth) {
210         case 0x0: i_bandwidth = 8; break;
211         case 0x1: i_bandwidth = 7; break;
212         case 0x2: i_bandwidth = 6; break;
213         case 0x3: i_bandwidth = 5; break;
214         default: i_bandwidth = 0; break;
215     }
216 
217     switch (i_constellation) {
218         case 0x0: psz_constellation = "QPSK"; break;
219         case 0x1: psz_constellation = "16-qam"; break;
220         case 0x2: psz_constellation = "64-qam"; break;
221     }
222 
223     switch (i_hierarchy) {
224         case 0x0: psz_hierarchy = "none"; break;
225         case 0x1: psz_hierarchy = "1"; break;
226         case 0x2: psz_hierarchy = "2"; break;
227         case 0x3: psz_hierarchy = "4"; break;
228         case 0x4: psz_hierarchy = "none+in-depth"; break;
229         case 0x5: psz_hierarchy = "1+in-depth"; break;
230         case 0x6: psz_hierarchy = "2+in-depth"; break;
231         case 0x7: psz_hierarchy = "4+in-depth"; break;
232     }
233 
234     switch (i_guard) {
235         case 0x0: psz_guard = "1/32"; break;
236         case 0x1: psz_guard = "1/16"; break;
237         case 0x2: psz_guard = "1/8"; break;
238         case 0x3: psz_guard = "1/4"; break;
239     }
240 
241     switch (i_transmission) {
242         case 0x0: psz_transmission = "2k"; break;
243         case 0x1: psz_transmission = "8k"; break;
244         case 0x2: psz_transmission = "4k"; break;
245     }
246 
247     switch (i_print_type) {
248     case PRINT_XML:
249         pf_print(opaque,
250              "<TERRESTRIAL_DESC frequency=\"%" PRIu64 "\" bandwidth=\"%u\" priority=\"%s\" timeslicing=\"%d\" mpefec=\"%d\" constellation=\"%s\" hierarchy=\"%s\" coderatehp=\"%s\" coderatelp=\"%s\" guard=\"%s\" transmission=\"%s\" otherfrequency=\"%d\"/>",
251              desc5a_get_frequency(p_desc), i_bandwidth,
252              desc5a_get_priority(p_desc) ? "HP" : "LP",
253              desc5a_get_timeslicing(p_desc) ? 1 : 0,
254              desc5a_get_mpefec(p_desc) ? 1 : 0, psz_constellation,
255              psz_hierarchy,
256              dvb_delivery_get_fec(desc5a_get_coderatehp(p_desc) + 1),
257              b_hierarchy ? dvb_delivery_get_fec(desc5a_get_coderatelp(p_desc) + 1) : "not_applicable",
258              psz_guard, psz_transmission,
259              desc5a_get_otherfrequency(p_desc) ? 1 : 0);
260         break;
261     default:
262         pf_print(opaque,
263              "    - desc 5a dvb-t frequency=%" PRIu64 " Hz bandwidth=%u MHz priority=%s timeslicing=%d mpefec=%d constellation=%s hierarchy=%s coderatehp=%s coderatelp=%s guard=%s transmission=%s otherfrequency=%d",
264              desc5a_get_frequency(p_desc), i_bandwidth,
265              desc5a_get_priority(p_desc) ? "HP" : "LP",
266              desc5a_get_timeslicing(p_desc) ? 1 : 0,
267              desc5a_get_mpefec(p_desc) ? 1 : 0, psz_constellation,
268              psz_hierarchy,
269              dvb_delivery_get_fec(desc5a_get_coderatehp(p_desc) + 1),
270              b_hierarchy ? dvb_delivery_get_fec(desc5a_get_coderatelp(p_desc) + 1) : "not_applicable",
271              psz_guard, psz_transmission,
272              desc5a_get_otherfrequency(p_desc) ? 1 : 0);
273     }
274 }
275 
276 #ifdef __cplusplus
277 }
278 #endif
279 
280 #endif
281