metadata_descriptor.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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_METADATA_DESCRIPTOR
  22. #define _UCSI_MPEG_METADATA_DESCRIPTOR 1
  23. #ifdef __cplusplus
  24. extern "C"
  25. {
  26. #endif
  27. #include <libucsi/descriptor.h>
  28. #include <libucsi/endianops.h>
  29. /**
  30. * Values for the decoder_config_flags field.
  31. */
  32. enum {
  33. MPEG_DECODER_CONFIG_NONE = 0x00,
  34. MPEG_DECODER_CONFIG_IN_DECODER_CONFIG = 0x01,
  35. MPEG_DECODER_CONFIG_SAME_METADATA_SERVICE = 0x02,
  36. MPEG_DECODER_CONFIG_DSMCC = 0x03,
  37. MPEG_DECODER_CONFIG_SAME_PROGRAM = 0x04,
  38. };
  39. /**
  40. * mpeg_metadata_descriptor structure.
  41. */
  42. struct mpeg_metadata_descriptor {
  43. struct descriptor d;
  44. uint16_t metadata_application_format;
  45. /* struct mpeg_metadata_descriptor_application_format_identifier appid */
  46. /* uint8_t metadata_format */
  47. /* struct mpeg_metadata_descriptor_format_identifier formid */
  48. /* struct mpeg_metadata_descriptor_flags flags */
  49. /* struct mpeg_metadata_descriptor_service_identifier service_identifier */
  50. /* struct mpeg_metadata_descriptor_decoder_config decoder_config */
  51. /* struct mpeg_metadata_descriptor_decoder_config_id_record decoder_config_id_record */
  52. /* struct mpeg_metadata_descriptor_decoder_config_service_id decoder_config_service_id */
  53. /* struct mpeg_metadata_descriptor_decoder_config_reserved decoder_config_reserved */
  54. /* uint8_t private_data[] */
  55. } __ucsi_packed;
  56. /**
  57. * appid field of a metadata_descriptor.
  58. */
  59. struct mpeg_metadata_descriptor_application_format_identifier {
  60. uint32_t id;
  61. } __ucsi_packed;
  62. /**
  63. * formid field of a metadata_descriptor.
  64. */
  65. struct mpeg_metadata_descriptor_format_identifier {
  66. uint32_t id;
  67. } __ucsi_packed;
  68. /**
  69. * Flags field of a metadata_descriptor
  70. */
  71. struct mpeg_metadata_descriptor_flags {
  72. uint8_t metadata_service_id;
  73. EBIT3(uint8_t decoder_config_flags : 3; ,
  74. uint8_t dsm_cc_flag : 1; ,
  75. uint8_t reserved : 4; );
  76. } __ucsi_packed;
  77. /**
  78. * service_identifier field of a metadata_descriptor.
  79. */
  80. struct mpeg_metadata_descriptor_service_identifier {
  81. uint8_t service_identification_length;
  82. /* uint8_t data[] */
  83. } __ucsi_packed;
  84. /**
  85. * decoder_config field of a metadata_descriptor.
  86. */
  87. struct mpeg_metadata_descriptor_decoder_config {
  88. uint8_t decoder_config_length;
  89. /* uint8_t data[] */
  90. } __ucsi_packed;
  91. /**
  92. * decoder_config_id_record field of a metadata_descriptor.
  93. */
  94. struct mpeg_metadata_descriptor_decoder_config_id_record {
  95. uint8_t decoder_config_id_record_length;
  96. /* uint8_t data[] */
  97. } __ucsi_packed;
  98. /**
  99. * decoder_config_service_id field of a metadata_descriptor.
  100. */
  101. struct mpeg_metadata_descriptor_decoder_config_service_id {
  102. uint8_t decoder_config_metadata_service_id;
  103. } __ucsi_packed;
  104. /**
  105. * decoder_config_reserved field of a metadata_descriptor.
  106. */
  107. struct mpeg_metadata_descriptor_decoder_config_reserved {
  108. uint8_t reserved_data_length;
  109. /* uint8_t data[] */
  110. } __ucsi_packed;
  111. /**
  112. * Process an mpeg_metadata_descriptor.
  113. *
  114. * @param d Generic descriptor.
  115. * @return Pointer to an mpeg_metadata_descriptor, or NULL on error.
  116. */
  117. static inline struct mpeg_metadata_descriptor*
  118. mpeg_metadata_descriptor_codec(struct descriptor* d)
  119. {
  120. uint32_t pos = 2;
  121. uint8_t *buf = (uint8_t*) d;
  122. uint32_t len = d->len + 2;
  123. struct mpeg_metadata_descriptor_flags *flags;
  124. int id;
  125. if (len < sizeof(struct mpeg_metadata_descriptor))
  126. return NULL;
  127. bswap16(buf + pos);
  128. id = *((uint16_t*) (buf+pos));
  129. pos += 2;
  130. if (id == 0xffff) {
  131. if (len < (pos+4))
  132. return NULL;
  133. bswap32(buf+pos);
  134. pos += 4;
  135. }
  136. if (len < (pos+1))
  137. return NULL;
  138. id = buf[pos];
  139. pos++;
  140. if (id == 0xff) {
  141. if (len < (pos+4))
  142. return NULL;
  143. bswap32(buf+pos);
  144. pos += 4;
  145. }
  146. if (len < (pos + sizeof(struct mpeg_metadata_descriptor_flags)))
  147. return NULL;
  148. flags = (struct mpeg_metadata_descriptor_flags*) (buf+pos);
  149. pos += sizeof(struct mpeg_metadata_descriptor_flags);
  150. if (flags->dsm_cc_flag == 1) {
  151. if (len < (pos+1))
  152. return NULL;
  153. if (len < (pos+1+buf[pos]))
  154. return NULL;
  155. pos += 1 + buf[pos];
  156. }
  157. if (flags->decoder_config_flags == 1) {
  158. if (len < (pos+1))
  159. return NULL;
  160. if (len < (pos+1+buf[pos]))
  161. return NULL;
  162. pos += 1 + buf[pos];
  163. }
  164. if (flags->decoder_config_flags == 3) {
  165. if (len < (pos+1))
  166. return NULL;
  167. if (len < (pos+1+buf[pos]))
  168. return NULL;
  169. pos += 1 + buf[pos];
  170. }
  171. if (flags->decoder_config_flags == 4) {
  172. if (len < (pos+1))
  173. return NULL;
  174. pos++;
  175. }
  176. if ((flags->decoder_config_flags == 5) ||
  177. (flags->decoder_config_flags == 6)) {
  178. if (len < (pos+1))
  179. return NULL;
  180. if (len < (pos+1+buf[pos]))
  181. return NULL;
  182. pos += 1 + buf[pos];
  183. }
  184. if (len < pos)
  185. return NULL;
  186. return (struct mpeg_metadata_descriptor*) d;
  187. }
  188. /**
  189. * Accessor for pointer to appid field of an mpeg_metadata_descriptor.
  190. *
  191. * @param d The mpeg_metadata_descriptor structure.
  192. * @return The pointer, or NULL on error.
  193. */
  194. static inline struct mpeg_metadata_descriptor_application_format_identifier*
  195. mpeg_metadata_descriptor_appid(struct mpeg_metadata_descriptor *d)
  196. {
  197. uint8_t *buf = (uint8_t*) d + sizeof(struct mpeg_metadata_descriptor);
  198. if (d->metadata_application_format != 0xffff)
  199. return NULL;
  200. return (struct mpeg_metadata_descriptor_application_format_identifier*) buf;
  201. }
  202. /**
  203. * Accessor for metadata_format field of an mpeg_metadata_descriptor.
  204. *
  205. * @param d The mpeg_metadata_descriptor structure.
  206. * @return The pointer, or NULL on error.
  207. */
  208. static inline uint8_t
  209. mpeg_metadata_descriptor_metadata_format(struct mpeg_metadata_descriptor *d)
  210. {
  211. uint8_t *buf = (uint8_t*) d + sizeof(struct mpeg_metadata_descriptor);
  212. if (d->metadata_application_format == 0xffff)
  213. buf+=4;
  214. return *buf;
  215. }
  216. /**
  217. * Accessor for pointer to formid field of an mpeg_metadata_descriptor.
  218. *
  219. * @param d The mpeg_metadata_descriptor structure.
  220. * @return The pointer, or NULL on error.
  221. */
  222. static inline struct mpeg_metadata_descriptor_format_identifier*
  223. mpeg_metadata_descriptor_formid(struct mpeg_metadata_descriptor *d)
  224. {
  225. uint8_t *buf = (uint8_t*) d + sizeof(struct mpeg_metadata_descriptor);
  226. if (d->metadata_application_format == 0xffff)
  227. buf+=4;
  228. if (*buf != 0xff)
  229. return NULL;
  230. return (struct mpeg_metadata_descriptor_format_identifier*) (buf+1);
  231. }
  232. /**
  233. * Accessor for flags field of an mpeg_metadata_descriptor.
  234. *
  235. * @param d The mpeg_metadata_descriptor structure.
  236. * @return Pointer to the field, or NULL on error.
  237. */
  238. static inline struct mpeg_metadata_descriptor_flags*
  239. mpeg_metadata_descriptor_flags(struct mpeg_metadata_descriptor *d)
  240. {
  241. uint8_t *buf = (uint8_t*) d + sizeof(struct mpeg_metadata_descriptor);
  242. if (d->metadata_application_format == 0xffff)
  243. buf+=4;
  244. if (*buf == 0xff)
  245. buf+=4;
  246. return (struct mpeg_metadata_descriptor_flags*) buf;
  247. }
  248. /**
  249. * Accessor for service_identifier field of an mpeg_metadata_descriptor.
  250. *
  251. * @param flags Pointer to the mpeg_metadata_descriptor_flags.
  252. * @return Pointer to the field, or NULL on error.
  253. */
  254. static inline struct mpeg_metadata_descriptor_service_identifier*
  255. mpeg_metadata_descriptor_sevice_identifier(struct mpeg_metadata_descriptor_flags *flags)
  256. {
  257. uint8_t *buf = (uint8_t*) flags + sizeof(struct mpeg_metadata_descriptor_flags);
  258. if (flags->dsm_cc_flag!=1)
  259. return NULL;
  260. return (struct mpeg_metadata_descriptor_service_identifier *) buf;
  261. }
  262. /**
  263. * Accessor for data field of an mpeg_metadata_descriptor_service_identifier.
  264. *
  265. * @param d The mpeg_metadata_descriptor_service_identifier structure.
  266. * @return Pointer to the field.
  267. */
  268. static inline uint8_t*
  269. mpeg_metadata_descriptor_service_identifier_data(struct mpeg_metadata_descriptor_service_identifier *d)
  270. {
  271. return (uint8_t*) d + sizeof(struct mpeg_metadata_descriptor_service_identifier);
  272. }
  273. /**
  274. * Accessor for decoder_config field of an mpeg_metadata_descriptor.
  275. *
  276. * @param flags Pointer to the mpeg_metadata_descriptor_flags.
  277. * @return Pointer to the field, or NULL on error.
  278. */
  279. static inline struct mpeg_metadata_descriptor_decoder_config*
  280. mpeg_metadata_descriptor_decoder_config(struct mpeg_metadata_descriptor_flags *flags)
  281. {
  282. uint8_t *buf = (uint8_t*) flags + sizeof(struct mpeg_metadata_descriptor_flags);
  283. if (flags->decoder_config_flags != 1)
  284. return NULL;
  285. if (flags->dsm_cc_flag==1)
  286. buf += 1 + buf[1];
  287. return (struct mpeg_metadata_descriptor_decoder_config*) buf;
  288. }
  289. /**
  290. * Accessor for data field of an mpeg_metadata_descriptor_service_identifier.
  291. *
  292. * @param d The mpeg_metadata_descriptor_service_identifier structure.
  293. * @return Pointer to the field.
  294. */
  295. static inline uint8_t*
  296. mpeg_metadata_descriptor_decoder_config_data(struct mpeg_metadata_descriptor_decoder_config *d)
  297. {
  298. return (uint8_t*) d + sizeof(struct mpeg_metadata_descriptor_decoder_config);
  299. }
  300. /**
  301. * Accessor for decoder_config_id_record field of an mpeg_metadata_descriptor.
  302. *
  303. * @param flags Pointer to the mpeg_metadata_descriptor_flags.
  304. * @return Pointer to the field, or NULL on error.
  305. */
  306. static inline struct mpeg_metadata_descriptor_decoder_config_id_record*
  307. mpeg_metadata_descriptor_decoder_config_id_record(struct mpeg_metadata_descriptor_flags *flags)
  308. {
  309. uint8_t *buf = (uint8_t*) flags + sizeof(struct mpeg_metadata_descriptor_flags);
  310. if (flags->decoder_config_flags != 3)
  311. return NULL;
  312. if (flags->dsm_cc_flag==1)
  313. buf += 1 + buf[1];
  314. return (struct mpeg_metadata_descriptor_decoder_config_id_record *) buf;
  315. }
  316. /**
  317. * Accessor for data field of an mpeg_metadata_descriptor_decoder_config_id_record.
  318. *
  319. * @param d The mpeg_metadata_descriptor_decoder_config_id_record structure.
  320. * @return Pointer to the field.
  321. */
  322. static inline uint8_t*
  323. mpeg_metadata_descriptor_decoder_config_id_record_data(struct mpeg_metadata_descriptor_decoder_config_id_record *d)
  324. {
  325. return (uint8_t*) d + sizeof(struct mpeg_metadata_descriptor_decoder_config_id_record);
  326. }
  327. /**
  328. * Accessor for decoder_config_service_id field of an mpeg_metadata_descriptor.
  329. *
  330. * @param flags Pointer to the mpeg_metadata_descriptor_flags.
  331. * @return Pointer to the field, or NULL on error.
  332. */
  333. static inline struct mpeg_metadata_descriptor_decoder_config_service_id*
  334. mpeg_metadata_descriptor_decoder_config_service_id(struct mpeg_metadata_descriptor_flags *flags)
  335. {
  336. uint8_t *buf = (uint8_t*) flags + sizeof(struct mpeg_metadata_descriptor_flags);
  337. if (flags->decoder_config_flags != 4)
  338. return NULL;
  339. if (flags->dsm_cc_flag==1)
  340. buf += 1 + buf[1];
  341. return (struct mpeg_metadata_descriptor_decoder_config_service_id *) buf;
  342. }
  343. /**
  344. * Accessor for decoder_config_reserved field of an mpeg_metadata_descriptor.
  345. *
  346. * @param flags Pointer to the mpeg_metadata_descriptor_flags.
  347. * @return Pointer to the field, or NULL on error.
  348. */
  349. static inline struct mpeg_metadata_descriptor_decoder_config_reserved*
  350. mpeg_metadata_descriptor_decoder_config_reserved(struct mpeg_metadata_descriptor_flags *flags)
  351. {
  352. uint8_t *buf = (uint8_t*) flags + sizeof(struct mpeg_metadata_descriptor_flags);
  353. if ((flags->decoder_config_flags != 5) && (flags->decoder_config_flags != 6))
  354. return NULL;
  355. if (flags->dsm_cc_flag==1)
  356. buf += 1 + buf[1];
  357. return (struct mpeg_metadata_descriptor_decoder_config_reserved *) buf;
  358. }
  359. /**
  360. * Accessor for data field of an mpeg_metadata_descriptor_decoder_config_reserved.
  361. *
  362. * @param d The mpeg_metadata_descriptor_decoder_config_reserved structure.
  363. * @return Pointer to the field.
  364. */
  365. static inline uint8_t*
  366. mpeg_metadata_descriptor_decoder_config_reserved_data(struct mpeg_metadata_descriptor_decoder_config_reserved *d)
  367. {
  368. return (uint8_t*) d + sizeof(struct mpeg_metadata_descriptor_decoder_config_reserved);
  369. }
  370. /**
  371. * Accessor for private_data field of an mpeg_metadata_descriptor.
  372. *
  373. * @param d The mpeg_metadata_descriptor structure.
  374. * @param flags Pointer to the mpeg_metadata_descriptor_flags.
  375. * @param length Where the number of bytes in the field should be stored.
  376. * @return Pointer to the field.
  377. */
  378. static inline uint8_t*
  379. mpeg_metadata_descriptor_private_data(struct mpeg_metadata_descriptor *d,
  380. struct mpeg_metadata_descriptor_flags *flags,
  381. int *length)
  382. {
  383. uint8_t *buf = (uint8_t*) flags + sizeof(struct mpeg_metadata_descriptor_flags);
  384. uint8_t *end = (uint8_t*) d + d->d.len + 2;
  385. if (flags->dsm_cc_flag==1)
  386. buf += 1 + buf[1];
  387. if (flags->decoder_config_flags==1)
  388. buf += 1 + buf[1];
  389. if (flags->decoder_config_flags==3)
  390. buf += 1 + buf[1];
  391. if (flags->decoder_config_flags==4)
  392. buf++;
  393. if ((flags->decoder_config_flags==5)||(flags->decoder_config_flags==6))
  394. buf += 1 + buf[1];
  395. *length = end - buf;
  396. return buf;
  397. }
  398. #ifdef __cplusplus
  399. }
  400. #endif
  401. #endif