1 /*
2  * section and descriptor parser
3  *
4  * Copyright (C) 2005 Kenneth Aafloy (kenneth@linuxtv.org)
5  * Copyright (C) 2005 Andrew de Quincey (adq_dvb@lidskialf.net)
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
20  */
21 
22 #ifndef _UCSI_ATSC_TVCT_SECTION_H
23 #define _UCSI_ATSC_TVCT_SECTION_H 1
24 
25 #ifdef __cplusplus
26 extern "C"
27 {
28 #endif
29 
30 #include <libucsi/atsc/section.h>
31 
32 /**
33  * atsc_tvct_section structure.
34  */
35 struct atsc_tvct_section {
36 	struct atsc_section_psip head;
37 
38 	uint8_t num_channels_in_section;
39 	/* struct atsc_tvct_channel channels[] */
40  	/* struct atsc_tvct_channel_part2 part2 */
41 } __ucsi_packed;
42 
43 struct atsc_tvct_channel {
44 	uint16_t short_name[7]; // UTF-16 network ordered
45   EBIT4(uint32_t reserved			: 4; ,
46 	uint32_t major_channel_number		:10; ,
47   	uint32_t minor_channel_number		:10; ,
48 	uint32_t modulation_mode		: 8; );
49 	uint32_t carrier_frequency;
50 	uint16_t channel_TSID;
51 	uint16_t program_number;
52   EBIT7(uint16_t ETM_location			: 2; ,
53 	uint16_t access_controlled		: 1; ,
54 	uint16_t hidden				: 1; ,
55 	uint16_t reserved1			: 2; ,
56 	uint16_t hide_guide			: 1; ,
57 	uint16_t reserved2			: 3; ,
58 	uint16_t service_type			: 6; );
59 	uint16_t source_id;
60   EBIT2(uint16_t reserved3			: 6; ,
61 	uint16_t descriptors_length		:10; );
62 	/* struct descriptor descriptors[] */
63 } __ucsi_packed;
64 
65 struct atsc_tvct_section_part2 {
66   EBIT2(uint16_t reserved			: 6; ,
67 	uint16_t descriptors_length		:10; );
68 	/* struct descriptor descriptors[] */
69 } __ucsi_packed;
70 
71 static inline struct atsc_tvct_channel *atsc_tvct_section_channels_first(struct atsc_tvct_section *tvct);
72 static inline struct atsc_tvct_channel *
73 	atsc_tvct_section_channels_next(struct atsc_tvct_section *tvct, struct atsc_tvct_channel *pos, int idx);
74 
75 /**
76  * Process a atsc_tvct_section.
77  *
78  * @param section Pointer to an atsc_section_psip structure.
79  * @return atsc_tvct_section pointer, or NULL on error.
80  */
81 struct atsc_tvct_section *atsc_tvct_section_codec(struct atsc_section_psip *section);
82 
83 /**
84  * Accessor for the transport_stream_id field of a TVCT.
85  *
86  * @param tvct TVCT pointer.
87  * @return The transport_stream_id.
88  */
atsc_tvct_section_transport_stream_id(struct atsc_tvct_section * tvct)89 static inline uint16_t atsc_tvct_section_transport_stream_id(struct atsc_tvct_section *tvct)
90 {
91 	return tvct->head.ext_head.table_id_ext;
92 }
93 
94 /**
95  * Iterator for the channels field in an atsc_tvct_section.
96  *
97  * @param mgt atsc_tvct_section pointer.
98  * @param pos Variable containing a pointer to the current atsc_tvct_channel.
99  * @param idx Integer used to count which channel we in.
100  */
101 #define atsc_tvct_section_channels_for_each(mgt, pos, idx) \
102 	for ((pos) = atsc_tvct_section_channels_first(mgt), idx=0; \
103 	     (pos); \
104 	     (pos) = atsc_tvct_section_channels_next(mgt, pos, ++idx))
105 
106 /**
107  * Iterator for the descriptors field in a atsc_tvct_channel structure.
108  *
109  * @param channel atsc_tvct_channel pointer.
110  * @param pos Variable containing a pointer to the current descriptor.
111  */
112 #define atsc_tvct_channel_descriptors_for_each(channel, pos) \
113 	for ((pos) = atsc_tvct_channel_descriptors_first(channel); \
114 	     (pos); \
115 	     (pos) = atsc_tvct_channel_descriptors_next(channel, pos))
116 
117 /**
118  * Accessor for the second part of an atsc_tvct_section.
119  *
120  * @param mgt atsc_tvct_section pointer.
121  * @return atsc_tvct_section_part2 pointer.
122  */
123 static inline struct atsc_tvct_section_part2 *
atsc_tvct_section_part2(struct atsc_tvct_section * mgt)124 	atsc_tvct_section_part2(struct atsc_tvct_section *mgt)
125 {
126 	int pos = sizeof(struct atsc_tvct_section);
127 
128 	struct atsc_tvct_channel *cur_channel;
129 	int idx;
130 	atsc_tvct_section_channels_for_each(mgt, cur_channel, idx) {
131 		pos += sizeof(struct atsc_tvct_channel);
132 		pos += cur_channel->descriptors_length;
133 	}
134 
135 	return (struct atsc_tvct_section_part2 *) (((uint8_t*) mgt) + pos);
136 }
137 
138 /**
139  * Iterator for the descriptors field in a atsc_tvct_section structure.
140  *
141  * @param part2 atsc_tvct_section_part2 pointer.
142  * @param pos Variable containing a pointer to the current descriptor.
143  */
144 #define atsc_tvct_section_part2_descriptors_for_each(part2, pos) \
145 	for ((pos) = atsc_tvct_section_part2_descriptors_first(part2); \
146 	     (pos); \
147 	     (pos) = atsc_tvct_section_part2_descriptors_next(part2, pos))
148 
149 
150 
151 
152 
153 
154 
155 
156 
157 
158 
159 /******************************** PRIVATE CODE ********************************/
160 static inline struct atsc_tvct_channel *
atsc_tvct_section_channels_first(struct atsc_tvct_section * tvct)161 	atsc_tvct_section_channels_first(struct atsc_tvct_section *tvct)
162 {
163 	size_t pos = sizeof(struct atsc_tvct_section);
164 
165 	if (tvct->num_channels_in_section == 0)
166 		return NULL;
167 
168 	return (struct atsc_tvct_channel*) (((uint8_t *) tvct) + pos);
169 }
170 
171 static inline struct atsc_tvct_channel *
atsc_tvct_section_channels_next(struct atsc_tvct_section * tvct,struct atsc_tvct_channel * pos,int idx)172 	atsc_tvct_section_channels_next(struct atsc_tvct_section *tvct,
173 				     struct atsc_tvct_channel *pos,
174 				     int idx)
175 {
176 	if (idx >= tvct->num_channels_in_section)
177 		return NULL;
178 
179 	return (struct atsc_tvct_channel *)
180 		(((uint8_t*) pos) + sizeof(struct atsc_tvct_channel) + pos->descriptors_length);
181 }
182 
183 static inline struct descriptor *
atsc_tvct_channel_descriptors_first(struct atsc_tvct_channel * channel)184 	atsc_tvct_channel_descriptors_first(struct atsc_tvct_channel *channel)
185 {
186 	size_t pos = sizeof(struct atsc_tvct_channel);
187 
188 	if (channel->descriptors_length == 0)
189 		return NULL;
190 
191 	return (struct descriptor*) (((uint8_t *) channel) + pos);
192 }
193 
194 static inline struct descriptor *
atsc_tvct_channel_descriptors_next(struct atsc_tvct_channel * channel,struct descriptor * pos)195 	atsc_tvct_channel_descriptors_next(struct atsc_tvct_channel *channel,
196 					struct descriptor *pos)
197 {
198 	return next_descriptor((uint8_t*) channel + sizeof(struct atsc_tvct_channel),
199 				channel->descriptors_length,
200 				pos);
201 }
202 
203 static inline struct descriptor *
atsc_tvct_section_part2_descriptors_first(struct atsc_tvct_section_part2 * part2)204 	atsc_tvct_section_part2_descriptors_first(struct atsc_tvct_section_part2 *part2)
205 {
206 	size_t pos = sizeof(struct atsc_tvct_section_part2);
207 
208 	if (part2->descriptors_length == 0)
209 		return NULL;
210 
211 	return (struct descriptor*) (((uint8_t *) part2) + pos);
212 }
213 
214 static inline struct descriptor *
atsc_tvct_section_part2_descriptors_next(struct atsc_tvct_section_part2 * part2,struct descriptor * pos)215 	atsc_tvct_section_part2_descriptors_next(struct atsc_tvct_section_part2 *part2,
216 						 struct descriptor *pos)
217 {
218 	return next_descriptor((uint8_t*) part2 + sizeof(struct atsc_tvct_section_part2),
219 				part2->descriptors_length,
220 				pos);
221 }
222 
223 #ifdef __cplusplus
224 }
225 #endif
226 
227 #endif
228