rrt_section.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. #ifndef _UCSI_ATSC_RRT_SECTION_H
  22. #define _UCSI_ATSC_RRT_SECTION_H 1
  23. #ifdef __cplusplus
  24. extern "C"
  25. {
  26. #endif
  27. #include <libucsi/atsc/section.h>
  28. #include <libucsi/atsc/types.h>
  29. /**
  30. * atsc_rrt_section structure.
  31. */
  32. struct atsc_rrt_section {
  33. struct atsc_section_psip head;
  34. uint8_t rating_region_name_length;
  35. /* struct atsc_text rating_region_name_text */
  36. /* struct atsc_rrt_section_part2 part2 */
  37. } __ucsi_packed;
  38. struct atsc_rrt_section_part2 {
  39. uint8_t dimensions_defined;
  40. /* struct atsc_rrt_dimension dimensions[] */
  41. /* struct atsc_rrt_section_part3 part3 */
  42. } __ucsi_packed;
  43. struct atsc_rrt_dimension {
  44. uint8_t dimension_name_length;
  45. /* struct atsc_text dimension_name_text */
  46. /* struct atsc_rrt_dimension_part2 part2 */
  47. } __ucsi_packed;
  48. struct atsc_rrt_dimension_part2 {
  49. EBIT3(uint8_t reserved : 3; ,
  50. uint8_t graduated_scale : 1; ,
  51. uint8_t values_defined : 4; );
  52. /* struct atsc_rrt_dimension_value values[] */
  53. } __ucsi_packed;
  54. struct atsc_rrt_dimension_value {
  55. uint8_t abbrev_rating_value_length;
  56. /* struct atsc_text abbrev_rating_value_text */
  57. /* struct atsc_rrt_dimension_value_part2 */
  58. } __ucsi_packed;
  59. struct atsc_rrt_dimension_value_part2 {
  60. uint8_t rating_value_length;
  61. /* struct atsc_text rating_value_text */
  62. } __ucsi_packed;
  63. struct atsc_rrt_section_part3 {
  64. EBIT2(uint16_t reserved : 6; ,
  65. uint16_t descriptors_length :10; );
  66. /* struct descriptor descriptors[] */
  67. } __ucsi_packed;
  68. static inline struct atsc_rrt_dimension *
  69. atsc_rrt_section_dimensions_first(struct atsc_rrt_section_part2 *part2);
  70. static inline struct atsc_rrt_dimension *
  71. atsc_rrt_section_dimensions_next(struct atsc_rrt_section_part2 *part2,
  72. struct atsc_rrt_dimension *pos,
  73. int idx);
  74. static inline struct atsc_rrt_dimension_value *
  75. atsc_rrt_dimension_part2_values_first(struct atsc_rrt_dimension_part2 *part2);
  76. static inline struct atsc_rrt_dimension_value *
  77. atsc_rrt_dimension_part2_values_next(struct atsc_rrt_dimension_part2 *part2,
  78. struct atsc_rrt_dimension_value *pos,
  79. int idx);
  80. /**
  81. * Process a atsc_rrt_section.
  82. *
  83. * @param section Pointer to anj atsc_section_psip structure.
  84. * @return atsc_rrt_section pointer, or NULL on error.
  85. */
  86. struct atsc_rrt_section *atsc_rrt_section_codec(struct atsc_section_psip *section);
  87. /**
  88. * Accessor for the rating_region field of an RRT.
  89. *
  90. * @param rrt RRT pointer.
  91. * @return The transport_stream_id.
  92. */
  93. static inline uint8_t atsc_rrt_section_rating_region(struct atsc_rrt_section *rrt)
  94. {
  95. return rrt->head.ext_head.table_id_ext & 0xff;
  96. }
  97. /**
  98. * Accessor for the rating_region_name_text field of an RRT.
  99. *
  100. * @param rrt RRT pointer.
  101. * @return struct atsc_text pointer, or NULL.
  102. */
  103. static inline struct atsc_text *atsc_rrt_section_rating_region_name_text(struct atsc_rrt_section *rrt)
  104. {
  105. if (rrt->rating_region_name_length == 0)
  106. return NULL;
  107. return (struct atsc_text*)(((uint8_t*) rrt) + sizeof(struct atsc_rrt_section));
  108. }
  109. /**
  110. * Accessor for the part2 field of an RRT.
  111. *
  112. * @param rrt RRT pointer.
  113. * @return struct atsc_rrt_section_part2 pointer.
  114. */
  115. static inline struct atsc_rrt_section_part2 *atsc_rrt_section_part2(struct atsc_rrt_section *rrt)
  116. {
  117. return (struct atsc_rrt_section_part2 *)
  118. (((uint8_t*) rrt) + sizeof(struct atsc_rrt_section) +
  119. rrt->rating_region_name_length);
  120. }
  121. /**
  122. * Iterator for the dimensions field in an atsc_rrt_section_part2.
  123. *
  124. * @param rrt atsc_rrt_section pointer.
  125. * @param pos Variable containing a pointer to the current atsc_rrt_dimension.
  126. * @param idx Integer used to count which dimension we are in.
  127. */
  128. #define atsc_rrt_section_dimensions_for_each(rrt, pos, idx) \
  129. for ((pos) = atsc_rrt_section_dimensions_first(rrt), idx=0; \
  130. (pos); \
  131. (pos) = atsc_rrt_section_dimensions_next(rrt, pos, ++idx))
  132. /**
  133. * Accessor for the dimension_name_text field of an atsc_rrt_dimension.
  134. *
  135. * @param dimension atsc_rrt_dimension pointer.
  136. * @return struct atsc_text pointer, or NULL on error.
  137. */
  138. static inline struct atsc_text *atsc_rrt_dimension_name_text(struct atsc_rrt_dimension *dimension)
  139. {
  140. if (dimension->dimension_name_length == 0)
  141. return NULL;
  142. return (struct atsc_text*)(((uint8_t*) dimension) + sizeof(struct atsc_rrt_dimension));
  143. }
  144. /**
  145. * Accessor for the part2 field of an atsc_rrt_dimension.
  146. *
  147. * @param dimension atsc_rrt_dimension pointer.
  148. * @return struct atsc_rrt_dimension_part2 pointer.
  149. */
  150. static inline struct atsc_rrt_dimension_part2 *atsc_rrt_dimension_part2(struct atsc_rrt_dimension *dimension)
  151. {
  152. return (struct atsc_rrt_dimension_part2 *)
  153. (((uint8_t*) dimension) +
  154. sizeof(struct atsc_rrt_dimension) +
  155. dimension->dimension_name_length);
  156. }
  157. /**
  158. * Iterator for the values field in a atsc_rrt_dimension_part2 structure.
  159. *
  160. * @param part2 atsc_rrt_dimension_part2 pointer.
  161. * @param pos Variable containing a pointer to the current value.
  162. * @param idx Integer used to count which value we are in
  163. */
  164. #define atsc_rrt_dimension_part2_values_for_each(part2, pos, idx) \
  165. for ((pos) = atsc_rrt_dimension_part2_values_first(part2), idx=0; \
  166. (pos); \
  167. (pos) = atsc_rrt_dimension_part2_values_next(part2, pos, ++idx))
  168. /**
  169. * Accessor for the dimension_name_text field of an atsc_rrt_dimension.
  170. *
  171. * @param dimension atsc_rrt_dimension pointer.
  172. * @return struct atsc_text pointer.
  173. */
  174. static inline struct atsc_text *
  175. atsc_rrt_dimension_value_abbrev_rating_value_text(struct atsc_rrt_dimension_value *value)
  176. {
  177. if (value->abbrev_rating_value_length == 0)
  178. return NULL;
  179. return (struct atsc_text*)(((uint8_t*) value) + sizeof(struct atsc_rrt_dimension_value));
  180. }
  181. /**
  182. * Accessor for the part2 field of an atsc_rrt_dimension_value.
  183. *
  184. * @param value atsc_rrt_dimension_value pointer.
  185. * @return struct atsc_rrt_dimension_value_part2 pointer.
  186. */
  187. static inline struct atsc_rrt_dimension_value_part2 *atsc_rrt_dimension_value_part2(struct atsc_rrt_dimension_value *value)
  188. {
  189. return (struct atsc_rrt_dimension_value_part2 *)
  190. (((uint8_t*) value) +
  191. sizeof(struct atsc_rrt_dimension_value) +
  192. value->abbrev_rating_value_length);
  193. }
  194. /**
  195. * Accessor for the rating_value_text field of an atsc_rrt_dimension_value_part2.
  196. *
  197. * @param part2 atsc_rrt_dimension_value_part2 pointer.
  198. * @return struct atsc_text pointer.
  199. */
  200. static inline struct atsc_text *atsc_rrt_dimension_value_part2_rating_value_text(struct atsc_rrt_dimension_value_part2 *part2)
  201. {
  202. if (part2->rating_value_length == 0)
  203. return NULL;
  204. return (struct atsc_text*)(((uint8_t*) part2) + sizeof(struct atsc_rrt_dimension_value_part2));
  205. }
  206. /**
  207. * Accessor for the third part of an atsc_rrt_section.
  208. *
  209. * @param part2 atsc_rrt_section_part2 pointer.
  210. * @return atsc_rrt_section_part3 pointer.
  211. */
  212. static inline struct atsc_rrt_section_part3 *
  213. atsc_rrt_section_part3(struct atsc_rrt_section_part2 *part2)
  214. {
  215. int pos = sizeof(struct atsc_rrt_section_part2);
  216. struct atsc_rrt_dimension *cur_dimension;
  217. int idx;
  218. atsc_rrt_section_dimensions_for_each(part2, cur_dimension, idx) {
  219. pos += sizeof(struct atsc_rrt_dimension);
  220. pos += cur_dimension->dimension_name_length;
  221. pos += sizeof(struct atsc_rrt_dimension_part2);
  222. // now we need to iterate over the values. yuck
  223. struct atsc_rrt_dimension_part2 *dpart2 = atsc_rrt_dimension_part2(cur_dimension);
  224. struct atsc_rrt_dimension_value *cur_value;
  225. int vidx;
  226. atsc_rrt_dimension_part2_values_for_each(dpart2, cur_value, vidx) {
  227. pos += sizeof(struct atsc_rrt_dimension_value);
  228. pos += cur_value->abbrev_rating_value_length;
  229. struct atsc_rrt_dimension_value_part2 *vpart2 = atsc_rrt_dimension_value_part2(cur_value);
  230. pos += sizeof(struct atsc_rrt_dimension_value_part2);
  231. pos += vpart2->rating_value_length;
  232. }
  233. }
  234. return (struct atsc_rrt_section_part3 *) (((uint8_t*) part2) + pos);
  235. }
  236. /**
  237. * Iterator for the descriptors field in a atsc_rrt_section structure.
  238. *
  239. * @param part3 atsc_rrt_section_part3 pointer.
  240. * @param pos Variable containing a pointer to the current descriptor.
  241. */
  242. #define atsc_rrt_section_part3_descriptors_for_each(part3, pos) \
  243. for ((pos) = atsc_rrt_section_part3_descriptors_first(part3); \
  244. (pos); \
  245. (pos) = atsc_rrt_section_part3_descriptors_next(part3, pos))
  246. /******************************** PRIVATE CODE ********************************/
  247. static inline struct atsc_rrt_dimension *
  248. atsc_rrt_section_dimensions_first(struct atsc_rrt_section_part2 *part2)
  249. {
  250. size_t pos = sizeof(struct atsc_rrt_section_part2);
  251. if (part2->dimensions_defined == 0)
  252. return NULL;
  253. return (struct atsc_rrt_dimension*) (((uint8_t *) part2) + pos);
  254. }
  255. static inline struct atsc_rrt_dimension *
  256. atsc_rrt_section_dimensions_next(struct atsc_rrt_section_part2 *part2,
  257. struct atsc_rrt_dimension *pos,
  258. int idx)
  259. {
  260. if (idx >= part2->dimensions_defined)
  261. return NULL;
  262. struct atsc_rrt_dimension_part2 *dpart2 = atsc_rrt_dimension_part2(pos);
  263. int len = sizeof(struct atsc_rrt_dimension_part2);
  264. // now we need to iterate over the values. yuck
  265. struct atsc_rrt_dimension_value *cur_value;
  266. int vidx;
  267. atsc_rrt_dimension_part2_values_for_each(dpart2, cur_value, vidx) {
  268. len += sizeof(struct atsc_rrt_dimension_value);
  269. len += cur_value->abbrev_rating_value_length;
  270. struct atsc_rrt_dimension_value_part2 *vpart2 = atsc_rrt_dimension_value_part2(cur_value);
  271. len += sizeof(struct atsc_rrt_dimension_value_part2);
  272. len += vpart2->rating_value_length;
  273. }
  274. return (struct atsc_rrt_dimension *) (((uint8_t*) dpart2) + len);
  275. }
  276. static inline struct atsc_rrt_dimension_value *
  277. atsc_rrt_dimension_part2_values_first(struct atsc_rrt_dimension_part2 *part2)
  278. {
  279. size_t pos = sizeof(struct atsc_rrt_dimension_part2);
  280. if (part2->values_defined == 0)
  281. return NULL;
  282. return (struct atsc_rrt_dimension_value*) (((uint8_t *) part2) + pos);
  283. }
  284. static inline struct atsc_rrt_dimension_value *
  285. atsc_rrt_dimension_part2_values_next(struct atsc_rrt_dimension_part2 *part2,
  286. struct atsc_rrt_dimension_value *pos,
  287. int idx)
  288. {
  289. if (idx >= part2->values_defined)
  290. return NULL;
  291. struct atsc_rrt_dimension_value_part2 *vpart2 = atsc_rrt_dimension_value_part2(pos);
  292. int len = sizeof(struct atsc_rrt_dimension_value_part2);
  293. len += vpart2->rating_value_length;
  294. return (struct atsc_rrt_dimension_value *) (((uint8_t*) vpart2) + len);
  295. }
  296. static inline struct descriptor *
  297. atsc_rrt_section_part3_descriptors_first(struct atsc_rrt_section_part3 *part3)
  298. {
  299. size_t pos = sizeof(struct atsc_rrt_section_part3);
  300. if (part3->descriptors_length == 0)
  301. return NULL;
  302. return (struct descriptor*) (((uint8_t *) part3) + pos);
  303. }
  304. static inline struct descriptor *
  305. atsc_rrt_section_part3_descriptors_next(struct atsc_rrt_section_part3 *part3,
  306. struct descriptor *pos)
  307. {
  308. return next_descriptor((uint8_t*) part3 + sizeof(struct atsc_rrt_section_part3),
  309. part3->descriptors_length,
  310. pos);
  311. }
  312. #ifdef __cplusplus
  313. }
  314. #endif
  315. #endif