dvb-file.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. * Copyright (c) 2011-2014 - Mauro Carvalho Chehab
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation version 2
  7. * of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #ifndef _DVB_FILE_H
  15. #define _DVB_FILE_H
  16. #include "dvb-fe.h"
  17. /**
  18. * @file dvb-file.h
  19. * @ingroup file
  20. * @brief Provides interfaces to deal with DVB channel and program files.
  21. * @copyright GNU General Public License version 2 (GPLv2)
  22. * @author Mauro Carvalho Chehab
  23. *
  24. * There are basically two types of files used for DVB:
  25. * - files that describe the physical channels (also called as transponders);
  26. * - files that describe the several programs found on a MPEG-TS (also called
  27. * as zap files).
  28. *
  29. * The libdvbv5 library defines an unified type for both types. Other
  30. * applications generally use different formats.
  31. *
  32. * The purpose of the functions and structures defined herein is to provide
  33. * support to read and write to those different formats.
  34. *
  35. * @par Bug Report
  36. * Please submit bug reports and patches to linux-media@vger.kernel.org
  37. */
  38. /*
  39. * DVB structures used to represent all files opened by the libdvbv5 library.
  40. *
  41. * Those structs represents each individual entry on a file, and the file
  42. * as a whole.
  43. */
  44. /**
  45. * @struct dvb_elementary_pid
  46. * @brief associates an elementary stream type with its PID
  47. * @ingroup file
  48. *
  49. * @param type Elementary stream type
  50. * @param pid Elementary stream Program ID
  51. */
  52. struct dvb_elementary_pid {
  53. uint8_t type;
  54. uint16_t pid;
  55. };
  56. /**
  57. * @struct dvb_entry
  58. * @brief Represents one entry on a DTV file
  59. * @ingroup file
  60. *
  61. * @param props A property key/value pair. The keys are the ones
  62. * specified at the DVB API, plus the ones defined
  63. * internally by libdvbv5, at the dvb-v5-std.h
  64. * header file.
  65. * @param next a pointer to the next entry. NULL if this is
  66. * the last one.
  67. * @param service_id Service ID associated with a program inside a
  68. * transponder. Please note that pure "channel"
  69. * files will have this field filled with 0.
  70. * @param video_pid Array with the video program IDs inside a service
  71. * @param audio_pid Array with the audio program IDs inside a service
  72. * @param other_el_pid Array with all non-audio/video program IDs
  73. * inside a service
  74. * @param video_pid_len Size of the video_pid array
  75. * @param audio_pid_len Size of the audio_pid array
  76. * @param other_el_pid_len Size of the other_el_pid array
  77. * @param channel String containing the name of the channel
  78. * @param vchannel String representing the Number of the channel
  79. * @param location String representing the location of the channel
  80. * @param sat_number For satellite streams, this represents the
  81. * number of the satellite dish on a DiSeqC
  82. * arrangement. Should be zero on arrangements
  83. * without DiSeqC.
  84. * @param freq_bpf SCR/Unicable band-pass filter frequency to
  85. * use, in kHz.
  86. * For non SRC/Unicable arrangements, it should
  87. * be zero.
  88. * @param diseqc_wait Extra time to wait for DiSeqC commands to
  89. * complete, in ms. The library will use 15 ms
  90. * as the minimal time,
  91. * plus the time specified on this field.
  92. * @param lnb String with the name of the LNBf to be used for
  93. * satellite tuning. The names should match the
  94. * names provided by dvb_sat_get_lnb() call
  95. * (see dvb-sat.h).
  96. */
  97. struct dvb_entry {
  98. struct dtv_property props[DTV_MAX_COMMAND];
  99. unsigned int n_props;
  100. struct dvb_entry *next;
  101. uint16_t service_id;
  102. uint16_t *video_pid, *audio_pid;
  103. struct dvb_elementary_pid *other_el_pid;
  104. unsigned video_pid_len, audio_pid_len, other_el_pid_len;
  105. char *channel;
  106. char *vchannel;
  107. char *location;
  108. int sat_number;
  109. unsigned freq_bpf;
  110. unsigned diseqc_wait;
  111. char *lnb;
  112. };
  113. /**
  114. * @struct dvb_file
  115. * @brief Describes an entire DVB file opened
  116. *
  117. * @param fname name of the file
  118. * @param n_entries number of the entries read
  119. * @param first_entry entry for the first entry. NULL if the file is empty.
  120. */
  121. struct dvb_file {
  122. char *fname;
  123. int n_entries;
  124. struct dvb_entry *first_entry;
  125. };
  126. /*
  127. * DVB file format tables
  128. *
  129. * The structs below are used to represent oneline formats like the ones
  130. * commonly found on DVB legacy applications.
  131. */
  132. /**
  133. * @struct dvb_parse_table
  134. * @brief Describes the fields to parse on a file
  135. * @ingroup file
  136. *
  137. * @param prop Name of the DVBv5 or libdvbv5 property field
  138. * @param table Name of a translation table for string to
  139. * int conversion
  140. * @param size Size of the translation table
  141. * @param mult_factor Multiply factor - Used, for example, to
  142. * multiply the symbol rate read from a DVB-S
  143. * table by 1000.
  144. * @param has_default_value It is different than zero when the property
  145. * can be optional. In this case, the next field
  146. * should be present
  147. * @param default_value Default value for the optional field
  148. */
  149. struct dvb_parse_table {
  150. unsigned int prop;
  151. const char **table;
  152. unsigned int size;
  153. int mult_factor;
  154. int has_default_value;
  155. int default_value;
  156. };
  157. /**
  158. * @struct dvb_parse_struct
  159. * @brief Describes the format to parse an specific delivery system
  160. * @ingroup file
  161. *
  162. * @param id String that identifies the delivery system on the
  163. * file to be parsed
  164. * @param delsys Delivery system
  165. * @param table the struct dvb_parse_table used to parse for this
  166. * specific delivery system
  167. * @param size Size of the table
  168. */
  169. struct dvb_parse_struct {
  170. char *id;
  171. uint32_t delsys;
  172. const struct dvb_parse_table *table;
  173. unsigned int size;
  174. };
  175. /**
  176. * @struct dvb_parse_file
  177. * @brief Describes an entire file format
  178. *
  179. * @param has_delsys_id A non-zero value indicates that the id field
  180. * at the formats vector should be used
  181. * @param delimiter Delimiters to split entries on the format
  182. * @param formats A struct dvb_parse_struct vector with the
  183. * per delivery system parsers. This table should
  184. * terminate with an empty entry.
  185. */
  186. struct dvb_parse_file {
  187. int has_delsys_id;
  188. char *delimiter;
  189. struct dvb_parse_struct formats[];
  190. };
  191. /**
  192. * @enum dvb_file_formats
  193. * @brief Known file formats
  194. * @ingroup file
  195. *
  196. * @details
  197. * Please notice that the channel format defined here has a few optional
  198. * fields that aren't part of the dvb-apps format, for DVB-S2 and for DVB-T2.
  199. * They're there to match the formats found at dtv-scan-tables package up to
  200. * September, 5 2014.
  201. *
  202. * @var FILE_UNKNOWN
  203. * @brief File format is unknown
  204. * @var FILE_ZAP
  205. * @brief File is at the dvb-apps "dvbzap" format
  206. * @var FILE_CHANNEL
  207. * @brief File is at the dvb-apps output format for dvb-zap
  208. * @var FILE_DVBV5
  209. * @brief File is at libdvbv5 format
  210. * @var FILE_VDR
  211. * @brief File is at DVR format (as supported on version 2.1.6).
  212. * Note: this is only supported as an output format.
  213. */
  214. enum dvb_file_formats {
  215. FILE_UNKNOWN,
  216. FILE_ZAP,
  217. FILE_CHANNEL,
  218. FILE_DVBV5,
  219. FILE_VDR,
  220. };
  221. struct dvb_v5_descriptors;
  222. #ifdef __cplusplus
  223. extern "C" {
  224. #endif
  225. /**
  226. * @brief Deallocates memory associated with a struct dvb_file
  227. * @ingroup file
  228. *
  229. * @param dvb_file dvb_file struct to be deallocated
  230. *
  231. * This function assumes that several functions were dynamically allocated
  232. * by the library file functions.
  233. */
  234. static inline void dvb_file_free(struct dvb_file *dvb_file)
  235. {
  236. struct dvb_entry *entry = dvb_file->first_entry, *next;
  237. while (entry) {
  238. next = entry->next;
  239. if (entry->channel)
  240. free(entry->channel);
  241. if (entry->vchannel)
  242. free(entry->vchannel);
  243. if (entry->location)
  244. free(entry->location);
  245. if (entry->video_pid)
  246. free(entry->video_pid);
  247. if (entry->audio_pid)
  248. free(entry->audio_pid);
  249. if (entry->other_el_pid)
  250. free(entry->other_el_pid);
  251. if (entry->lnb)
  252. free(entry->lnb);
  253. free(entry);
  254. entry = next;
  255. }
  256. free(dvb_file);
  257. }
  258. /*
  259. * File format description structures defined for the several formats that
  260. * the library can read natively.
  261. */
  262. /**
  263. * @brief File format definitions for dvb-apps channel format
  264. * @ingroup file
  265. */
  266. extern const struct dvb_parse_file channel_file_format;
  267. /**
  268. * @brief File format definitions for dvb-apps zap format
  269. * @ingroup file
  270. */
  271. extern const struct dvb_parse_file channel_file_zap_format;
  272. /*
  273. * Prototypes for the several functions defined at dvb-file.c
  274. */
  275. /**
  276. * @brief Read a file at libdvbv5 format
  277. * @ingroup file
  278. *
  279. * @param fname file name
  280. *
  281. * @return It returns a pointer to struct dvb_file describing the entries that
  282. * were read from the file. If it fails, NULL is returned.
  283. */
  284. struct dvb_file *dvb_read_file(const char *fname);
  285. /**
  286. * @brief Write a file at libdvbv5 format
  287. * @ingroup file
  288. *
  289. * @param fname file name
  290. * @param dvb_file contents of the file to be written
  291. *
  292. * @return It returns zero if success, or a positive error number if it fails.
  293. */
  294. int dvb_write_file(const char *fname, struct dvb_file *dvb_file);
  295. /**
  296. * @brief Read a file on any format natively supported by
  297. * the library
  298. * @ingroup file
  299. *
  300. * @param fname file name
  301. * @param delsys Delivery system, as specified by enum fe_delivery_system
  302. * @param format Name of the format to be read
  303. *
  304. * @return It returns a pointer to struct dvb_file describing the entries that
  305. * were read from the file. If it fails, NULL is returned.
  306. */
  307. struct dvb_file *dvb_read_file_format(const char *fname,
  308. uint32_t delsys,
  309. enum dvb_file_formats format);
  310. /**
  311. * @brief Write a file on any format natively supported by
  312. * the library
  313. * @ingroup file
  314. *
  315. * @param fname file name
  316. * @param dvb_file contents of the file to be written
  317. * @param delsys Delivery system, as specified by enum fe_delivery_system
  318. * @param format Name of the format to be read
  319. *
  320. * @return It a pointer to struct dvb_file on success, NULL otherwise.
  321. */
  322. int dvb_write_file_format(const char *fname,
  323. struct dvb_file *dvb_file,
  324. uint32_t delsys,
  325. enum dvb_file_formats format);
  326. /**
  327. * @brief Stores a key/value pair on a DVB file entry
  328. * @ingroup file
  329. *
  330. * @param entry entry to be filled
  331. * @param cmd key for the property to be used. It be one of the DVBv5
  332. * properties, plus the libdvbv5 ones, as defined at dvb-v5-std.h
  333. * @param value value for the property.
  334. *
  335. * This function seeks for a property with the name specified by cmd and
  336. * fills it with value. If the entry doesn't exist, it creates a new key.
  337. *
  338. * @return Returns 0 if success, or, if the entry has already DTV_MAX_COMMAND
  339. * properties, it returns -1.
  340. */
  341. int dvb_store_entry_prop(struct dvb_entry *entry,
  342. uint32_t cmd, uint32_t value);
  343. /**
  344. * @brief Retrieves the value associated witha key on a DVB file entry
  345. * @ingroup file
  346. *
  347. * @param entry entry to be used
  348. * @param cmd key for the property to be found. It be one of the DVBv5
  349. * properties, plus the libdvbv5 ones, as defined at dvb-v5-std.h
  350. * @param value pointer to store the value associated with the property.
  351. *
  352. * This function seeks for a property with the name specified by cmd and
  353. * fills value with its contents.
  354. *
  355. * @return Returns 0 if success, or, -1 if the entry doesn't exist.
  356. */
  357. int dvb_retrieve_entry_prop(struct dvb_entry *entry,
  358. uint32_t cmd, uint32_t *value);
  359. /**
  360. * @brief stored a new scanned channel into a dvb_file struct
  361. * @ingroup file
  362. *
  363. * @param dvb_file file struct to be filled
  364. * @param parms struct dvb_v5_fe_parms used by libdvbv5 frontend
  365. * @param dvb_desc struct dvb_desc as described at descriptors.h, filled
  366. * with the descriptors associated with a DVB channel.
  367. * those descriptors can be filled by calling one of the
  368. * scan functions defined at dvb-sat.h.
  369. * @param get_detected if different than zero, uses the frontend parameters
  370. * obtained from the device driver (such as modulation,
  371. * FEC, etc)
  372. * @param get_nit if true, uses the parameters obtained from the MPEG-TS
  373. * NIT table to add newly detected transponders.
  374. *
  375. * This function should be used to store the services found on a scanned
  376. * transponder. Initially, it copies the same parameters used to set the
  377. * frontend, that came from a file where the Service ID and Elementary Stream
  378. * PIDs are unknown. At tuning time, it is common to set the device to tune
  379. * on auto-detection mode (e. g. using QAM/AUTO, for example, to autodetect
  380. * the QAM modulation). The libdvbv5's logic will be to check the detected
  381. * values. So, the modulation might, for example, have changed to QAM/256.
  382. * In such case, if get_detected is 0, it will store QAM/AUTO at the struct.
  383. * If get_detected is different than zero, it will store QAM/256.
  384. * If get_nit is different than zero, and if the MPEG-TS has info about other
  385. * physical channels/transponders, this function will add newer entries to
  386. * dvb_file, for it to seek for new transponders. This is very useful especially
  387. * for DVB-C, where all transponders belong to the same operator. Knowing one
  388. * frequency is generally enough to get all DVB-C transponders.
  389. *
  390. * @return Returns 0 if success, or, -1 if error.
  391. */
  392. int dvb_store_channel(struct dvb_file **dvb_file,
  393. struct dvb_v5_fe_parms *parms,
  394. struct dvb_v5_descriptors *dvb_desc,
  395. int get_detected, int get_nit);
  396. /**
  397. * @brief Ancillary function that seeks for a delivery system
  398. * @ingroup file
  399. *
  400. * @param name string containing the name of the Delivery System to seek
  401. *
  402. * If the name is found, this function returns the DVBv5 property that
  403. * corresponds to the string given. The function is case-insensitive, and
  404. * it can check for alternate ways to write the name of a Delivery System.
  405. * Currently, it supports: DVB-C, DVB-H, DVB-S, DVB-S2, DVB-T, DVB-T2,
  406. * ISDB-C, ISDB-S, ISDB-T, ATSC-MH, DVBC/ANNEX_A, DVBC/ANNEX_B, DVBT, DSS,
  407. * DVBS, DVBS2, DVBH, ISDBT, ISDBS, ISDBC, ATSC, ATSCMH, DTMB, CMMB, DAB,
  408. * DVBT2, TURBO, DVBC/ANNEX_C.
  409. * Please notice that this doesn't mean that all those standards are properly
  410. * supported by the library.
  411. *
  412. * @return Returns the Delivery System property number if success, -1 if error.
  413. */
  414. int dvb_parse_delsys(const char *name);
  415. /**
  416. * @brief Ancillary function that parses the name of a file format
  417. * @ingroup file
  418. *
  419. * @param name string containing the name of the format
  420. * Current valid names are: ZAP, CHANNEL, VDR and DVBV5.
  421. * The name is case-insensitive.
  422. *
  423. * @return It returns FILE_ZAP, FILE_CHANNEL, FILE_VDR or FILE_DVBV5
  424. * if the name was translated. FILE_UNKNOWN otherwise.
  425. */
  426. enum dvb_file_formats dvb_parse_format(const char *name);
  427. /*
  428. * Routines to read a non-libdvbv5 format. They're called by
  429. * dvb_read_file_format() or dvb_write_file_format()
  430. */
  431. /**
  432. * @brief Read and parses a one line file format
  433. * @ingroup file
  434. *
  435. * @param fname file name
  436. * @param delsys delivery system
  437. * @param parse_file pointer struct dvb_parse_file
  438. *
  439. * @return It a pointer to struct dvb_file on success, NULL otherwise.
  440. *
  441. * This function is called internally by dvb_read_file_format.
  442. */
  443. struct dvb_file *dvb_parse_format_oneline(const char *fname,
  444. uint32_t delsys,
  445. const struct dvb_parse_file *parse_file);
  446. /**
  447. * @brief Writes a file into an one line file format
  448. * @ingroup file
  449. *
  450. * @param fname file name
  451. * @param dvb_file contents of the file to be written
  452. * @param delsys delivery system
  453. * @param parse_file pointer struct dvb_parse_file
  454. *
  455. * @return It returns zero if success, or a positive error number if it fails.
  456. *
  457. * This function is called internally by dvb_write_file_format.
  458. */
  459. int dvb_write_format_oneline(const char *fname,
  460. struct dvb_file *dvb_file,
  461. uint32_t delsys,
  462. const struct dvb_parse_file *parse_file);
  463. /**
  464. * @brief Writes a file into vdr format (compatible up to version 2.1)
  465. * @ingroup file
  466. *
  467. * @param fname file name
  468. * @param dvb_file contents of the file to be written
  469. *
  470. * @return It returns zero if success, or a positive error number if it fails.
  471. *
  472. * This function is called internally by dvb_write_file_format.
  473. */
  474. int dvb_write_format_vdr(const char *fname,
  475. struct dvb_file *dvb_file);
  476. #ifdef __cplusplus
  477. }
  478. #endif
  479. #endif // _DVB_FILE_H