libv4l2rds.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Copyright 2012 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  3. * Author: Konke Radlow <koradlow@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
  18. */
  19. #ifndef __LIBV4L2RDS
  20. #define __LIBV4L2RDS
  21. #include <stdbool.h>
  22. #include <stdint.h>
  23. #include <linux/videodev2.h>
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif /* __cplusplus */
  27. #if HAVE_VISIBILITY
  28. #define LIBV4L_PUBLIC __attribute__ ((visibility("default")))
  29. #else
  30. #define LIBV4L_PUBLIC
  31. #endif
  32. /* used to define the current version (version field) of the v4l2_rds struct */
  33. #define V4L2_RDS_VERSION (2)
  34. /* Constants used to define the size of arrays used to store RDS information */
  35. #define MAX_ODA_CNT 18 /* there are 16 groups each with type a or b. Of these
  36. * 32 distinct groups, 18 can be used for ODA purposes */
  37. #define MAX_AF_CNT 25 /* AF Method A allows a maximum of 25 AFs to be defined
  38. * AF Method B does not impose a limit on the number of AFs
  39. * but it is not fully supported at the moment and will
  40. * not receive more than 25 AFs */
  41. #define MAX_TMC_ADDITIONAL 28 /* 28 is the maximal possible number of fields.
  42. * Additional data is limited to 112 bit, and the smallest
  43. * optional tuple has a size of 4 bit (4 bit identifier +
  44. * 0 bits of data) */
  45. #define MAX_TMC_ALT_STATIONS 32 /* defined by ISO 14819-1:2003, 7.5.3.3 */
  46. #define MAX_TMC_AF_CNT 4 /* limit for the numbers of AFs stored per alternative TMC
  47. * station. This value is not defined by the standard, but based on observation
  48. * of real-world RDS-TMC streams. The maximum encountered number of AFs per
  49. * station during testing was 2 */
  50. #define MAX_EON_CNT 20 /* Maximal number of entries in the EON table (for storing
  51. * information about other radio stations, broadcasted by the current station).
  52. * This value is not defined by the standard, but based on observation
  53. * of real-world RDS-TMC streams. EON doesn't seem to be a widely used feature
  54. * and the maximum number of EON encountered during testing was 8 */
  55. /* Define Constants for the possible types of RDS information
  56. * used to address the relevant bit in the valid_fields bitmask */
  57. #define V4L2_RDS_PI 0x01 /* Program Identification */
  58. #define V4L2_RDS_PTY 0x02 /* Program Type */
  59. #define V4L2_RDS_TP 0x04 /* Traffic Program */
  60. #define V4L2_RDS_PS 0x08 /* Program Service Name */
  61. #define V4L2_RDS_TA 0x10 /* Traffic Announcement */
  62. #define V4L2_RDS_DI 0x20 /* Decoder Information */
  63. #define V4L2_RDS_MS 0x40 /* Music / Speech flag */
  64. #define V4L2_RDS_PTYN 0x80 /* Program Type Name */
  65. #define V4L2_RDS_RT 0x100 /* Radio-Text */
  66. #define V4L2_RDS_TIME 0x200 /* Date and Time information */
  67. #define V4L2_RDS_TMC 0x400 /* TMC availability */
  68. #define V4L2_RDS_AF 0x800 /* AF (alternative freq) available */
  69. #define V4L2_RDS_ECC 0x1000 /* Extended County Code */
  70. #define V4L2_RDS_LC 0x2000 /* Language Code */
  71. #define V4L2_RDS_TMC_SG 0x4000 /* RDS-TMC single group */
  72. #define V4L2_RDS_TMC_MG 0x8000 /* RDS-TMC multi group */
  73. #define V4L2_RDS_TMC_SYS 0x10000 /* RDS-TMC system information */
  74. #define V4L2_RDS_EON 0x20000 /* Enhanced Other Network Info */
  75. #define V4L2_RDS_LSF 0x40000 /* Linkage information */
  76. #define V4L2_RDS_TMC_TUNING 0x80000 /* RDS-TMC tuning information */
  77. /* Define Constants for the state of the RDS decoding process
  78. * used to address the relevant bit in the decode_information bitmask */
  79. #define V4L2_RDS_GROUP_NEW 0x01 /* New group received */
  80. #define V4L2_RDS_ODA 0x02 /* Open Data Group announced */
  81. /* Decoder Information (DI) codes
  82. * used to decode the DI information according to the RDS standard */
  83. #define V4L2_RDS_FLAG_STEREO 0x01
  84. #define V4L2_RDS_FLAG_ARTIFICIAL_HEAD 0x02
  85. #define V4L2_RDS_FLAG_COMPRESSED 0x04
  86. #define V4L2_RDS_FLAG_DYNAMIC_PTY 0x08
  87. /* TMC related codes
  88. * used to extract TMC fields from RDS-TMC groups
  89. * see ISO 14819-1:2003, Figure 2 - RDS-TMC single-grp full message structure */
  90. #define V4L2_TMC_TUNING_INFO 0x10 /* Bit 4 indicates Tuning Info / User msg */
  91. #define V4L2_TMC_SINGLE_GROUP 0x08 /* Bit 3 indicates Single / Multi-group msg */
  92. /* struct to encapsulate one complete RDS group */
  93. /* This structure is used internally to store data until a complete RDS
  94. * group was received and group id dependent decoding can be done.
  95. * It is also used to provide external access to uninterpreted RDS groups
  96. * when manual decoding is required (e.g. special ODA types) */
  97. struct v4l2_rds_group {
  98. uint16_t pi; /* Program Identification */
  99. char group_version; /* group version ('A' / 'B') */
  100. uint8_t group_id; /* group number (0..16) */
  101. /* uninterpreted data blocks for decoding (e.g. ODA) */
  102. uint8_t data_b_lsb;
  103. uint8_t data_c_msb;
  104. uint8_t data_c_lsb;
  105. uint8_t data_d_msb;
  106. uint8_t data_d_lsb;
  107. };
  108. /* struct to encapsulate some statistical information about the decoding process */
  109. struct v4l2_rds_statistics {
  110. uint32_t block_cnt; /* total amount of received blocks */
  111. uint32_t group_cnt; /* total amount of successfully
  112. * decoded groups */
  113. uint32_t block_error_cnt; /* blocks that were marked as erroneous
  114. * and had to be dropped */
  115. uint32_t group_error_cnt; /* group decoding processes that had to be
  116. * aborted because of erroneous blocks
  117. * or wrong order of blocks */
  118. uint32_t block_corrected_cnt; /* blocks that contained 1-bit errors
  119. * which were corrected */
  120. uint32_t group_type_cnt[16]; /* number of occurrence for each
  121. * defined RDS group */
  122. };
  123. /* struct to encapsulate the definition of one ODA (Open Data Application) type */
  124. struct v4l2_rds_oda {
  125. uint8_t group_id; /* RDS group used to broadcast this ODA */
  126. char group_version; /* group version (A / B) for this ODA */
  127. uint16_t aid; /* Application Identification for this ODA,
  128. * AIDs are centrally administered by the
  129. * RDS Registration Office (rds.org.uk) */
  130. };
  131. /* struct to encapsulate an array of all defined ODA types for a channel */
  132. /* This structure will grow with ODA announcements broadcasted in type 3A
  133. * groups, that were verified not to be no duplicates or redefinitions */
  134. struct v4l2_rds_oda_set {
  135. uint8_t size; /* number of ODAs defined by this channel */
  136. struct v4l2_rds_oda oda[MAX_ODA_CNT];
  137. };
  138. /* struct to encapsulate an array of Alternative Frequencies for a channel */
  139. /* Every channel can send out AFs for his program. The number of AFs that
  140. * will be broadcasted is announced by the channel */
  141. struct v4l2_rds_af_set {
  142. uint8_t size; /* size of the set (might be smaller
  143. * than the announced size) */
  144. uint8_t announced_af; /* number of announced AF */
  145. uint32_t af[MAX_AF_CNT]; /* AFs defined in Hz */
  146. };
  147. /* struct to encapsulate one entry in the EON table (Enhanced Other Network) */
  148. struct v4l2_rds_eon {
  149. uint32_t valid_fields;
  150. uint16_t pi;
  151. uint8_t ps[9];
  152. uint8_t pty;
  153. bool ta;
  154. bool tp;
  155. uint16_t lsf; /* Linkage Set Number */
  156. struct v4l2_rds_af_set af;
  157. };
  158. /* struct to encapsulate a table of EON information */
  159. struct v4l2_rds_eon_set {
  160. uint8_t size; /* size of the table */
  161. uint8_t index; /* current position in the table */
  162. struct v4l2_rds_eon eon[MAX_EON_CNT]; /* Information about other
  163. * radio channels */
  164. };
  165. /* struct to encapsulate alternative frequencies (AFs) for RDS-TMC stations.
  166. * AFs listed in af[] can be used unconditionally.
  167. * AFs listed in mapped_af[n] should only be used if the current
  168. * tuner frequency matches the value in mapped_af_tuning[n] */
  169. struct v4l2_tmc_alt_freq {
  170. uint8_t af_size; /* number of known AFs */
  171. uint8_t af_index;
  172. uint8_t mapped_af_size; /* number of mapped AFs */
  173. uint8_t mapped_af_index;
  174. uint32_t af[MAX_TMC_AF_CNT]; /* AFs defined in Hz */
  175. uint32_t mapped_af[MAX_TMC_AF_CNT]; /* mapped AFs defined in Hz */
  176. uint32_t mapped_af_tuning[MAX_TMC_AF_CNT]; /* mapped AFs defined in Hz */
  177. };
  178. /* struct to encapsulate information about stations carrying RDS-TMC services */
  179. struct v4l2_tmc_station {
  180. uint16_t pi;
  181. uint8_t ltn; /* database-ID of ON */
  182. uint8_t msg; /* msg parameters of ON */
  183. uint8_t sid; /* service-ID of ON */
  184. struct v4l2_tmc_alt_freq afi;
  185. };
  186. /* struct to encapsulate tuning information for TMC */
  187. struct v4l2_tmc_tuning {
  188. uint8_t station_cnt; /* number of announced alternative stations */
  189. uint8_t index;
  190. struct v4l2_tmc_station station[MAX_TMC_ALT_STATIONS]; /* information
  191. * about other stations carrying the same RDS-TMC service */
  192. };
  193. /* struct to encapsulate an additional data field in a TMC message */
  194. struct v4l2_tmc_additional {
  195. uint8_t label;
  196. uint16_t data;
  197. };
  198. /* struct to encapsulate an arbitrary number of additional data fields
  199. * belonging to one TMC message */
  200. struct v4l2_tmc_additional_set {
  201. uint8_t size;
  202. struct v4l2_tmc_additional fields[MAX_TMC_ADDITIONAL];
  203. };
  204. /* struct to encapsulate a decoded TMC message with optional additional
  205. * data field (in case of a multi-group TMC message) */
  206. struct v4l2_rds_tmc_msg {
  207. uint8_t length; /* length of multi-group message (0..4) */
  208. uint8_t sid; /* service identifier at time of reception */
  209. uint8_t extent;
  210. uint8_t dp; /* duration and persistence */
  211. uint16_t event; /* TMC event code */
  212. uint16_t location; /* TMC event location */
  213. bool follow_diversion; /* indicates if the driver is adviced to
  214. * follow the diversion */
  215. bool neg_direction; /* indicates negative / positive direction */
  216. /* decoded additional information (only available in multi-group
  217. * messages) */
  218. struct v4l2_tmc_additional_set additional;
  219. };
  220. /* struct to encapsulate all TMC related information, including TMC System
  221. * Information, TMC Tuning information and a buffer for the last decoded
  222. * TMC messages */
  223. struct v4l2_rds_tmc {
  224. uint8_t ltn; /* location_table_number */
  225. bool afi; /* alternative frequency indicator */
  226. bool enhanced_mode; /* mode of transmission,
  227. * if false -> basic => gaps between tmc groups
  228. * gap defines timing behavior
  229. * if true -> enhanced => t_a, t_w and t_d
  230. * define timing behavior of tmc groups */
  231. uint8_t mgs; /* message geographical scope */
  232. uint8_t sid; /* service identifier (unique ID on national level) */
  233. uint8_t gap; /* Gap parameters */
  234. uint8_t t_a; /* activity time (only if mode = enhanced) */
  235. uint8_t t_w; /* window time (only if mode = enhanced */
  236. uint8_t t_d; /* delay time (only if mode = enhanced */
  237. uint8_t spn[9]; /* service provider name */
  238. struct v4l2_rds_tmc_msg tmc_msg;
  239. /* tuning information for alternative service providers */
  240. struct v4l2_tmc_tuning tuning;
  241. };
  242. /* struct to encapsulate state and RDS information for current decoding process */
  243. /* This is the structure that will be used by external applications, to
  244. * communicate with the library and get access to RDS data */
  245. struct v4l2_rds {
  246. /** state information **/
  247. uint32_t decode_information; /* state of decoding process */
  248. uint32_t valid_fields; /* currently valid info fields
  249. * of this structure */
  250. /** RDS info fields **/
  251. bool is_rbds; /* use RBDS standard version of LUTs */
  252. uint16_t pi; /* Program Identification */
  253. uint8_t ps[9]; /* Program Service Name, UTF-8 encoding,
  254. * '\0' terminated */
  255. uint8_t pty; /* Program Type */
  256. uint8_t ptyn[9]; /* Program Type Name, UTF-8 encoding,
  257. * '\0' terminated */
  258. bool ptyn_ab_flag; /* PTYN A/B flag (toggled), to signal
  259. * change of PTYN */
  260. uint8_t rt_length; /* length of RT string */
  261. uint8_t rt[65]; /* Radio-Text string, UTF-8 encoding,
  262. * '\0' terminated */
  263. bool rt_ab_flag; /* RT A/B flag (toggled), to signal
  264. * transmission of new RT */
  265. bool ta; /* Traffic Announcement */
  266. bool tp; /* Traffic Program */
  267. bool ms; /* Music / Speech flag */
  268. uint8_t di; /* Decoder Information */
  269. uint8_t ecc; /* Extended Country Code */
  270. uint8_t lc; /* Language Code */
  271. time_t time; /* local time and date of transmission */
  272. struct v4l2_rds_statistics rds_statistics;
  273. struct v4l2_rds_oda_set rds_oda; /* Open Data Services */
  274. struct v4l2_rds_af_set rds_af; /* Alternative Frequencies */
  275. struct v4l2_rds_eon_set rds_eon; /* EON information */
  276. struct v4l2_rds_tmc tmc; /* TMC information */
  277. };
  278. /* v4l2_rds_init() - initializes a new decoding process
  279. * @is_rbds: defines which standard is used: true=RBDS, false=RDS
  280. *
  281. * initialize a new instance of the RDS-decoding struct and return
  282. * a handle containing state and RDS information, used to interact
  283. * with the library functions */
  284. LIBV4L_PUBLIC struct v4l2_rds *v4l2_rds_create(bool is_rbds);
  285. /* frees all memory allocated for the RDS-decoding struct */
  286. LIBV4L_PUBLIC void v4l2_rds_destroy(struct v4l2_rds *handle);
  287. /* resets the RDS information in the handle to initial values
  288. * e.g. can be used when radio channel is changed
  289. * @reset_statistics: true = set all statistic values to 0, false = keep them untouched */
  290. LIBV4L_PUBLIC void v4l2_rds_reset(struct v4l2_rds *handle, bool reset_statistics);
  291. /* adds a raw RDS block to decode it into RDS groups
  292. * @return: bitmask with with updated fields set to 1
  293. * @rds_data: 3 bytes of raw RDS data, obtained by calling read()
  294. * on RDS capable V4L2 devices */
  295. LIBV4L_PUBLIC uint32_t v4l2_rds_add(struct v4l2_rds *handle, struct v4l2_rds_data *rds_data);
  296. /*
  297. * group of functions to translate numerical RDS data into strings
  298. *
  299. * return program description string defined in the RDS/RBDS Standard
  300. * ! return value depends on selected Standard !*/
  301. LIBV4L_PUBLIC const char *v4l2_rds_get_pty_str(const struct v4l2_rds *handle);
  302. LIBV4L_PUBLIC const char *v4l2_rds_get_language_str(const struct v4l2_rds *handle);
  303. LIBV4L_PUBLIC const char *v4l2_rds_get_country_str(const struct v4l2_rds *handle);
  304. LIBV4L_PUBLIC const char *v4l2_rds_get_coverage_str(const struct v4l2_rds *handle);
  305. /* returns a pointer to the last decoded RDS group, in order to give raw
  306. * access to RDS data if it is required (e.g. ODA decoding) */
  307. LIBV4L_PUBLIC const struct v4l2_rds_group *v4l2_rds_get_group
  308. (const struct v4l2_rds *handle);
  309. #ifdef __cplusplus
  310. }
  311. #endif /* __cplusplus */
  312. #endif