content_labelling_descriptor.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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_MPEG_CONTENT_LABELLING_DESCRIPTOR
  22. #define _UCSI_MPEG_CONTENT_LABELLING_DESCRIPTOR 1
  23. #ifdef __cplusplus
  24. extern "C"
  25. {
  26. #endif
  27. #include <libucsi/descriptor.h>
  28. #include <libucsi/endianops.h>
  29. /**
  30. * Possible values for content_time_base_indicator.
  31. */
  32. enum {
  33. MPEG_CONTENT_TIME_BASE_STC = 0x01,
  34. MPEG_CONTENT_TIME_BASE_NPT = 0x02,
  35. };
  36. /**
  37. * mpeg_content_labelling_descriptor structure.
  38. */
  39. struct mpeg_content_labelling_descriptor {
  40. struct descriptor d;
  41. uint16_t metadata_application_format;
  42. /* struct mpeg_content_labelling_descriptor_application_format_identifier id */
  43. /* struct mpeg_content_labelling_descriptor_flags flags */
  44. /* struct mpeg_content_labelling_descriptor_reference_id reference_id */
  45. /* struct mpeg_content_labelling_descriptor_time_base time_base */
  46. /* struct mpeg_content_labelling_descriptor_content_id content_id */
  47. /* struct mpeg_content_labelling_descriptor_time_base_association time_base_assoc */
  48. /* uint8_t private_data[] */
  49. } __ucsi_packed;
  50. /**
  51. * id field of a content_labelling_descriptor.
  52. */
  53. struct mpeg_content_labelling_descriptor_application_format_identifier {
  54. uint32_t id;
  55. } __ucsi_packed;
  56. /**
  57. * Flags field of a content_labelling_descriptor
  58. */
  59. struct mpeg_content_labelling_descriptor_flags {
  60. EBIT3(uint8_t content_reference_id_record_flag : 1; ,
  61. uint8_t content_time_base_indicator : 4; ,
  62. uint8_t reserved : 3; );
  63. } __ucsi_packed;
  64. /**
  65. * Reference_id field of a content_labelling_descriptor.
  66. */
  67. struct mpeg_content_labelling_descriptor_reference_id {
  68. uint8_t content_reference_id_record_length;
  69. /* uint8_t data[] */
  70. } __ucsi_packed;
  71. /**
  72. * time_base field of a content_labelling_descriptor.
  73. */
  74. struct mpeg_content_labelling_descriptor_time_base {
  75. EBIT2(uint64_t reserved_1 : 7; ,
  76. uint64_t content_time_base_value :33; );
  77. EBIT2(uint64_t reserved_2 : 7; ,
  78. uint64_t metadata_time_base_value :33; );
  79. } __ucsi_packed;
  80. /**
  81. * content_id field of a content_labelling_descriptor.
  82. */
  83. struct mpeg_content_labelling_descriptor_content_id {
  84. EBIT2(uint8_t reserved : 1; ,
  85. uint8_t contentId : 7; );
  86. } __ucsi_packed;
  87. /**
  88. * time_base_assoc field of a content_labelling_descriptor.
  89. */
  90. struct mpeg_content_labelling_descriptor_time_base_association {
  91. uint8_t time_base_association_data_length;
  92. /* uint8_t data[] */
  93. } __ucsi_packed;
  94. /**
  95. * Process an mpeg_content_labelling_descriptor.
  96. *
  97. * @param d Generic descriptor.
  98. * @return Pointer to an mpeg_content_labelling_descriptor, or NULL on error.
  99. */
  100. static inline struct mpeg_content_labelling_descriptor*
  101. mpeg_content_labelling_descriptor_codec(struct descriptor* d)
  102. {
  103. uint32_t pos = 2;
  104. uint8_t *buf = (uint8_t*) d;
  105. uint32_t len = d->len + 2;
  106. struct mpeg_content_labelling_descriptor_flags *flags;
  107. int id;
  108. if (len < sizeof(struct mpeg_content_labelling_descriptor))
  109. return NULL;
  110. bswap16(buf + pos);
  111. id = *((uint16_t*) (buf+pos));
  112. pos += 2;
  113. if (id == 0xffff) {
  114. if (len < (pos+4))
  115. return NULL;
  116. bswap32(buf+pos);
  117. pos += 4;
  118. }
  119. if (len < (pos + sizeof(struct mpeg_content_labelling_descriptor_flags)))
  120. return NULL;
  121. flags = (struct mpeg_content_labelling_descriptor_flags*) (buf+pos);
  122. pos += sizeof(struct mpeg_content_labelling_descriptor_flags);
  123. if (flags->content_reference_id_record_flag == 1) {
  124. if (len < (pos+1))
  125. return NULL;
  126. if (len < (pos+1+buf[pos]))
  127. return NULL;
  128. pos += 1 + buf[pos];
  129. }
  130. if ((flags->content_time_base_indicator == 1) ||
  131. (flags->content_time_base_indicator == 2)) {
  132. if (len < (pos + sizeof(struct mpeg_content_labelling_descriptor_time_base)))
  133. return NULL;
  134. bswap40(buf+pos);
  135. bswap40(buf+pos+5);
  136. pos += sizeof(struct mpeg_content_labelling_descriptor_time_base);
  137. }
  138. if (flags->content_time_base_indicator == 2) {
  139. if (len < (pos + sizeof(struct mpeg_content_labelling_descriptor_content_id)))
  140. return NULL;
  141. pos += sizeof(struct mpeg_content_labelling_descriptor_content_id);
  142. }
  143. if (flags->content_time_base_indicator > 2) {
  144. if (len < (pos+1))
  145. return NULL;
  146. if (len < (pos+1+buf[pos]))
  147. return NULL;
  148. pos += 1 + buf[pos];
  149. }
  150. if (len < pos)
  151. return NULL;
  152. return (struct mpeg_content_labelling_descriptor*) d;
  153. }
  154. /**
  155. * Accessor for pointer to id field of an mpeg_content_labelling_descriptor.
  156. *
  157. * @param d The mpeg_content_labelling_descriptor structure.
  158. * @return The pointer, or NULL on error.
  159. */
  160. static inline struct mpeg_content_labelling_descriptor_application_format_identifier*
  161. mpeg_content_labelling_descriptor_id(struct mpeg_content_labelling_descriptor *d)
  162. {
  163. uint8_t *buf = (uint8_t*) d;
  164. if (d->metadata_application_format != 0xffff)
  165. return NULL;
  166. return (struct mpeg_content_labelling_descriptor_application_format_identifier*)
  167. (buf + sizeof(struct mpeg_content_labelling_descriptor));
  168. }
  169. /**
  170. * Accessor for pointer to flags field of an mpeg_content_labelling_descriptor.
  171. *
  172. * @param d The mpeg_content_labelling_descriptor structure.
  173. * @return The pointer, or NULL on error.
  174. */
  175. static inline struct mpeg_content_labelling_descriptor_flags*
  176. mpeg_content_labelling_descriptor_flags(struct mpeg_content_labelling_descriptor *d)
  177. {
  178. uint8_t *buf = (uint8_t*) d + sizeof(struct mpeg_content_labelling_descriptor);
  179. if (d->metadata_application_format != 0xffff)
  180. buf += 4;
  181. return (struct mpeg_content_labelling_descriptor_flags *) buf;
  182. }
  183. /**
  184. * Accessor for reference_id field of an mpeg_content_labelling_descriptor.
  185. *
  186. * @param flags Pointer to the mpeg_content_labelling_descriptor_flags.
  187. * @return Pointer to the field, or NULL on error.
  188. */
  189. static inline struct mpeg_content_labelling_descriptor_reference_id*
  190. mpeg_content_labelling_descriptor_reference_id(struct mpeg_content_labelling_descriptor_flags *flags)
  191. {
  192. uint8_t *buf = (uint8_t*) flags + sizeof(struct mpeg_content_labelling_descriptor_flags);
  193. if (flags->content_reference_id_record_flag != 1)
  194. return NULL;
  195. return (struct mpeg_content_labelling_descriptor_reference_id *) buf;
  196. }
  197. /**
  198. * Accessor for data field of an mpeg_content_reference_id.
  199. *
  200. * @param d The mpeg_content_reference_id structure.
  201. * @return Pointer to the field.
  202. */
  203. static inline uint8_t*
  204. mpeg_content_reference_id_data(struct mpeg_content_labelling_descriptor_reference_id *d)
  205. {
  206. return (uint8_t*) d + sizeof(struct mpeg_content_labelling_descriptor_reference_id);
  207. }
  208. /**
  209. * Accessor for time_base field of an mpeg_content_labelling_descriptor.
  210. *
  211. * @param flags Pointer to the mpeg_content_labelling_descriptor_flags.
  212. * @return Pointer to the field, or NULL on error.
  213. */
  214. static inline struct mpeg_content_labelling_descriptor_time_base*
  215. mpeg_content_labelling_descriptor_time_base(struct mpeg_content_labelling_descriptor_flags *flags)
  216. {
  217. uint8_t *buf = (uint8_t*) flags + sizeof(struct mpeg_content_labelling_descriptor_flags);
  218. if ((flags->content_time_base_indicator!=1) && (flags->content_time_base_indicator!=2))
  219. return NULL;
  220. if (flags->content_reference_id_record_flag == 1)
  221. buf += 1 + buf[1];
  222. return (struct mpeg_content_labelling_descriptor_time_base *) buf;
  223. }
  224. /**
  225. * Accessor for content_id field of an mpeg_content_labelling_descriptor.
  226. *
  227. * @param flags Pointer to the mpeg_content_labelling_descriptor_flags.
  228. * @return Pointer to the field, or NULL on error.
  229. */
  230. static inline struct mpeg_content_labelling_descriptor_content_id*
  231. mpeg_content_labelling_descriptor_content_id(struct mpeg_content_labelling_descriptor_flags *flags)
  232. {
  233. uint8_t *buf = (uint8_t*) flags + sizeof(struct mpeg_content_labelling_descriptor_flags);
  234. if (flags->content_time_base_indicator!=2)
  235. return NULL;
  236. if (flags->content_reference_id_record_flag == 1)
  237. buf += 1 + buf[1];
  238. if ((flags->content_time_base_indicator==1) || (flags->content_time_base_indicator==2))
  239. buf += sizeof(struct mpeg_content_labelling_descriptor_time_base);
  240. return (struct mpeg_content_labelling_descriptor_content_id *) buf;
  241. }
  242. /**
  243. * Accessor for time_base_association field of an mpeg_content_labelling_descriptor.
  244. *
  245. * @param flags Pointer to the mpeg_content_labelling_descriptor_flags.
  246. * @return Pointer to the field, or NULL on error.
  247. */
  248. static inline struct mpeg_content_labelling_descriptor_time_base_association*
  249. mpeg_content_labelling_descriptor_time_base_assoc(struct mpeg_content_labelling_descriptor_flags *flags)
  250. {
  251. uint8_t *buf = (uint8_t*) flags + sizeof(struct mpeg_content_labelling_descriptor_flags);
  252. if (flags->content_time_base_indicator<3)
  253. return NULL;
  254. if (flags->content_reference_id_record_flag == 1)
  255. buf += 1 + buf[1];
  256. if ((flags->content_time_base_indicator==1) || (flags->content_time_base_indicator==2))
  257. buf += sizeof(struct mpeg_content_labelling_descriptor_time_base);
  258. if (flags->content_time_base_indicator==2)
  259. buf += sizeof(struct mpeg_content_labelling_descriptor_content_id);
  260. return (struct mpeg_content_labelling_descriptor_time_base_association *) buf;
  261. }
  262. /**
  263. * Accessor for data field of an mpeg_time_base_association.
  264. *
  265. * @param d The mpeg_time_base_association structure.
  266. * @return Pointer to the field.
  267. */
  268. static inline uint8_t*
  269. mpeg_time_base_association_data(struct mpeg_content_labelling_descriptor_time_base_association *d)
  270. {
  271. return (uint8_t*) d + sizeof(struct mpeg_content_labelling_descriptor_time_base_association);
  272. }
  273. /**
  274. * Accessor for private_data field of an mpeg_content_labelling_descriptor.
  275. *
  276. * @param d The mpeg_content_labelling_descriptor structure.
  277. * @param flags Pointer to the mpeg_content_labelling_descriptor_flags.
  278. * @param length Where the number of bytes in the field should be stored.
  279. * @return Pointer to the field.
  280. */
  281. static inline uint8_t*
  282. mpeg_content_labelling_descriptor_data(struct mpeg_content_labelling_descriptor *d,
  283. struct mpeg_content_labelling_descriptor_flags *flags,
  284. int *length)
  285. {
  286. uint8_t *buf = (uint8_t*) flags + sizeof(struct mpeg_content_labelling_descriptor_flags);
  287. uint8_t *end = (uint8_t*) d + d->d.len + 2;
  288. if (flags->content_reference_id_record_flag == 1)
  289. buf += 1 + buf[1];
  290. if ((flags->content_time_base_indicator==1) || (flags->content_time_base_indicator==2))
  291. buf += sizeof(struct mpeg_content_labelling_descriptor_time_base);
  292. if (flags->content_time_base_indicator==2)
  293. buf += sizeof(struct mpeg_content_labelling_descriptor_content_id);
  294. if (flags->content_time_base_indicator<3)
  295. buf += 1 + buf[1];
  296. *length = end - buf;
  297. return buf;
  298. }
  299. #ifdef __cplusplus
  300. }
  301. #endif
  302. #endif