mosaic_descriptor.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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_DVB_MOSAIC_DESCRIPTOR
  22. #define _UCSI_DVB_MOSAIC_DESCRIPTOR 1
  23. #ifdef __cplusplus
  24. extern "C"
  25. {
  26. #endif
  27. #include <libucsi/descriptor.h>
  28. #include <libucsi/endianops.h>
  29. /**
  30. * dvb_mosaic_descriptor structure.
  31. */
  32. struct dvb_mosaic_descriptor {
  33. struct descriptor d;
  34. EBIT4(uint8_t mosaic_entry_point : 1; ,
  35. uint8_t number_of_horiz_elementary_cells: 3; ,
  36. uint8_t reserved : 1; ,
  37. uint8_t number_of_vert_elementary_cells : 3; );
  38. /* struct dvb_mosaic_info infos[] */
  39. } __ucsi_packed;
  40. /**
  41. * An entry in the infos field of a dvb_mosaic_descriptor.
  42. */
  43. struct dvb_mosaic_info {
  44. EBIT3(uint16_t logical_cell_id : 6; ,
  45. uint16_t reserved : 7; ,
  46. uint16_t logical_cell_presentation_info : 3; );
  47. uint8_t elementary_cell_field_length;
  48. /* struct dvb_mosaic_elementary_cell_field fields[] */
  49. /* struct dvb_mosaic_info_part2 part2 */
  50. /* struct dvb_mosaic_linkage linkage */
  51. } __ucsi_packed;
  52. /**
  53. * An entry in the fields field of a dvb_mosaic_info.
  54. */
  55. struct dvb_mosaic_elementary_cell_field {
  56. EBIT2(uint8_t reserved : 2; ,
  57. uint8_t elementary_cell_id : 6; );
  58. } __ucsi_packed;
  59. /**
  60. * Part2 of dvb_mosaic_info, following the variable length fields field.
  61. */
  62. struct dvb_mosaic_info_part2 {
  63. uint8_t cell_linkage_info;
  64. } __ucsi_packed;
  65. struct dvb_mosaic_linkage_01 {
  66. uint16_t bouquet_id;
  67. } __ucsi_packed;
  68. struct dvb_mosaic_linkage_02 {
  69. uint16_t original_network_id;
  70. uint16_t transport_stream_id;
  71. uint16_t service_id;
  72. } __ucsi_packed;
  73. struct dvb_mosaic_linkage_03 {
  74. uint16_t original_network_id;
  75. uint16_t transport_stream_id;
  76. uint16_t service_id;
  77. } __ucsi_packed;
  78. struct dvb_mosaic_linkage_04 {
  79. uint16_t original_network_id;
  80. uint16_t transport_stream_id;
  81. uint16_t service_id;
  82. uint16_t event_id;
  83. } __ucsi_packed;
  84. /**
  85. * Structure describing the linkage field of a dvb_mosaic_info
  86. */
  87. struct dvb_mosaic_linkage {
  88. union {
  89. struct dvb_mosaic_linkage_01 linkage_01;
  90. struct dvb_mosaic_linkage_02 linkage_02;
  91. struct dvb_mosaic_linkage_03 linkage_03;
  92. struct dvb_mosaic_linkage_04 linkage_04;
  93. } u;
  94. } __ucsi_packed;
  95. /**
  96. * Process a dvb_mosaic_descriptor.
  97. *
  98. * @param d Pointer to a generic descriptor structure.
  99. */
  100. static inline struct dvb_mosaic_descriptor*
  101. dvb_mosaic_descriptor_codec(struct descriptor* d)
  102. {
  103. uint8_t* buf = (uint8_t*) d + 2;
  104. uint32_t pos = 0;
  105. uint32_t len = d->len;
  106. struct dvb_mosaic_descriptor * p =
  107. (struct dvb_mosaic_descriptor *) d;
  108. pos += (sizeof(struct dvb_mosaic_descriptor) - 2);
  109. if (pos > len)
  110. return NULL;
  111. while(pos < len) {
  112. struct dvb_mosaic_info *e =
  113. (struct dvb_mosaic_info*) (buf+pos);
  114. struct dvb_mosaic_info_part2 *e2;
  115. if ((pos + sizeof(struct dvb_mosaic_info)) > len)
  116. return NULL;
  117. bswap16(buf + pos);
  118. pos += sizeof(struct dvb_mosaic_info) +
  119. e->elementary_cell_field_length;
  120. if (pos > len)
  121. return NULL;
  122. e2 = (struct dvb_mosaic_info_part2*) (buf+pos);
  123. pos += sizeof(struct dvb_mosaic_info_part2);
  124. if (pos > len)
  125. return NULL;
  126. switch(e2->cell_linkage_info) {
  127. case 0x01:
  128. if ((pos + sizeof(struct dvb_mosaic_linkage_01)) > len)
  129. return NULL;
  130. bswap16(buf+pos);
  131. pos += sizeof(struct dvb_mosaic_linkage_01);
  132. break;
  133. case 0x02:
  134. if ((pos + sizeof(struct dvb_mosaic_linkage_02)) > len)
  135. return NULL;
  136. bswap16(buf+pos);
  137. bswap16(buf+pos+2);
  138. bswap16(buf+pos+4);
  139. pos += sizeof(struct dvb_mosaic_linkage_02);
  140. break;
  141. case 0x03:
  142. if ((pos + sizeof(struct dvb_mosaic_linkage_03)) > len)
  143. return NULL;
  144. bswap16(buf+pos);
  145. bswap16(buf+pos+2);
  146. bswap16(buf+pos+4);
  147. pos += sizeof(struct dvb_mosaic_linkage_03);
  148. break;
  149. case 0x04:
  150. if ((pos + sizeof(struct dvb_mosaic_linkage_04)) > len)
  151. return NULL;
  152. bswap16(buf+pos);
  153. bswap16(buf+pos+2);
  154. bswap16(buf+pos+4);
  155. bswap16(buf+pos+6);
  156. pos += sizeof(struct dvb_mosaic_linkage_04);
  157. break;
  158. }
  159. }
  160. return p;
  161. }
  162. /**
  163. * Iterator over the infos field of a dvb_mosaic_descriptor.
  164. *
  165. * @param d dvb_mosaic_descriptor pointer.
  166. * @param pos Variable containing a pointer to the current dvb_mosaic_info.
  167. */
  168. #define dvb_mosaic_descriptor_infos_for_each(d, pos) \
  169. for ((pos) = dvb_mosaic_descriptor_infos_first(d); \
  170. (pos); \
  171. (pos) = dvb_mosaic_descriptor_infos_next(d, pos))
  172. /**
  173. * Iterator over the fields field of a dvb_mosaic_info.
  174. *
  175. * @param info dvb_mosaic_info pointer.
  176. * @param pos Variable containing a pointer to the current dvb_mosaic_elementary_cell_field.
  177. */
  178. #define dvb_mosaic_info_fields_for_each(info, pos) \
  179. for ((pos) = dvb_mosaic_info_fields_first(info); \
  180. (pos); \
  181. (pos) = dvb_mosaic_info_fields_next(info, pos))
  182. /**
  183. * Accessor for the second part of the dvb_mosaic_info structure.
  184. *
  185. * @param entry dvb_mosaic_info pointer.
  186. * @return dvb_mosaic_info_part2 pointer.
  187. */
  188. static inline struct dvb_mosaic_info_part2*
  189. dvb_mosaic_info_part2(struct dvb_mosaic_info* entry)
  190. {
  191. return (struct dvb_mosaic_info_part2*)
  192. ((uint8_t*) entry + sizeof(struct dvb_mosaic_info) +
  193. entry->elementary_cell_field_length);
  194. }
  195. /**
  196. * Accessor for the linkage field a dvb_mosaic_info structure.
  197. *
  198. * @param entry dvb_mosaic_info_part2 pointer.
  199. * @return dvb_mosaic_linkage pointer, or NULL on error.
  200. */
  201. static inline struct dvb_mosaic_linkage*
  202. dvb_mosaic_linkage(struct dvb_mosaic_info_part2* entry)
  203. {
  204. if ((entry->cell_linkage_info != 0x01) &&
  205. (entry->cell_linkage_info != 0x02) &&
  206. (entry->cell_linkage_info != 0x03) &&
  207. (entry->cell_linkage_info != 0x04))
  208. return NULL;
  209. return (struct dvb_mosaic_linkage*)
  210. ((uint8_t*) entry + sizeof(struct dvb_mosaic_info_part2));
  211. }
  212. /******************************** PRIVATE CODE ********************************/
  213. static inline struct dvb_mosaic_info*
  214. dvb_mosaic_descriptor_infos_first(struct dvb_mosaic_descriptor *d)
  215. {
  216. if (d->d.len == 1)
  217. return NULL;
  218. return (struct dvb_mosaic_info *)
  219. ((uint8_t*) d + sizeof(struct dvb_mosaic_descriptor));
  220. }
  221. static inline struct dvb_mosaic_info*
  222. dvb_mosaic_descriptor_infos_next(struct dvb_mosaic_descriptor *d,
  223. struct dvb_mosaic_info *pos)
  224. {
  225. struct dvb_mosaic_info_part2* part2 = dvb_mosaic_info_part2(pos);
  226. uint8_t *end = (uint8_t*) d + 2 + d->d.len;
  227. uint8_t *next = (uint8_t *) pos + sizeof(struct dvb_mosaic_info) +
  228. pos->elementary_cell_field_length +
  229. sizeof(struct dvb_mosaic_info_part2);
  230. if (part2->cell_linkage_info == 0x01)
  231. next += sizeof(struct dvb_mosaic_linkage_01);
  232. else if (part2->cell_linkage_info == 0x02)
  233. next += sizeof(struct dvb_mosaic_linkage_02);
  234. else if (part2->cell_linkage_info == 0x03)
  235. next += sizeof(struct dvb_mosaic_linkage_03);
  236. else if (part2->cell_linkage_info == 0x04)
  237. next += sizeof(struct dvb_mosaic_linkage_04);
  238. if (next >= end)
  239. return NULL;
  240. return (struct dvb_mosaic_info *) next;
  241. }
  242. static inline struct dvb_mosaic_elementary_cell_field*
  243. dvb_mosaic_info_fields_first(struct dvb_mosaic_info *d)
  244. {
  245. if (d->elementary_cell_field_length == 0)
  246. return NULL;
  247. return (struct dvb_mosaic_elementary_cell_field*)
  248. ((uint8_t*) d + sizeof(struct dvb_mosaic_info));
  249. }
  250. static inline struct dvb_mosaic_elementary_cell_field*
  251. dvb_mosaic_info_fields_next(struct dvb_mosaic_info *d,
  252. struct dvb_mosaic_elementary_cell_field* pos)
  253. {
  254. uint8_t *end = (uint8_t*) d + sizeof(struct dvb_mosaic_info) +
  255. d->elementary_cell_field_length;
  256. uint8_t *next = (uint8_t *) pos +
  257. sizeof(struct dvb_mosaic_elementary_cell_field);
  258. if (next >= end)
  259. return NULL;
  260. return (struct dvb_mosaic_elementary_cell_field *) next;
  261. }
  262. #ifdef __cplusplus
  263. }
  264. #endif
  265. #endif