scan.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791
  1. /*
  2. * Simple MPEG parser to achieve network/service information.
  3. *
  4. * refered standards:
  5. *
  6. * ETSI EN 300 468
  7. * ETSI TR 101 211
  8. * ETSI ETR 211
  9. * ITU-T H.222.0
  10. *
  11. * 2005-05-10 - Basic ATSC PSIP parsing support added
  12. * ATSC Standard Revision B (A65/B)
  13. *
  14. * Thanks to Sean Device from Triveni for providing access to ATSC signals
  15. * and to Kevin Fowlks for his independent ATSC scanning tool.
  16. *
  17. * Please contribute: It is possible that some descriptors for ATSC are
  18. * not parsed yet and thus the result won't be complete.
  19. */
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <sys/ioctl.h>
  26. #include <sys/poll.h>
  27. #include <unistd.h>
  28. #include <fcntl.h>
  29. #include <time.h>
  30. #include <errno.h>
  31. #include <signal.h>
  32. #include <assert.h>
  33. #include <glob.h>
  34. #include <ctype.h>
  35. #include <iconv.h>
  36. #include <langinfo.h>
  37. #include <linux/dvb/frontend.h>
  38. #include <linux/dvb/dmx.h>
  39. #include "list.h"
  40. #include "diseqc.h"
  41. #include "dump-zap.h"
  42. #include "dump-vdr.h"
  43. #include "scan.h"
  44. #include "lnb.h"
  45. #include "atsc_psip_section.h"
  46. static char demux_devname[80];
  47. static struct dvb_frontend_info fe_info = {
  48. .type = -1
  49. };
  50. int verbosity = 2;
  51. static int long_timeout;
  52. static int current_tp_only;
  53. static int get_other_nits;
  54. static int vdr_dump_provider;
  55. static int vdr_dump_channum;
  56. static int no_ATSC_PSIP;
  57. static int ATSC_type=1;
  58. static int ca_select = -1;
  59. static int serv_select = 7;
  60. static int vdr_version = 3;
  61. static struct lnb_types_st lnb_type;
  62. static int unique_anon_services;
  63. char *default_charset = "ISO-6937";
  64. char *output_charset;
  65. #define CS_OPTIONS "//TRANSLIT"
  66. static enum fe_spectral_inversion spectral_inversion = INVERSION_AUTO;
  67. enum table_type {
  68. PAT,
  69. PMT,
  70. SDT,
  71. NIT
  72. };
  73. enum format {
  74. OUTPUT_ZAP,
  75. OUTPUT_VDR,
  76. OUTPUT_PIDS
  77. };
  78. static enum format output_format = OUTPUT_ZAP;
  79. static int output_format_set = 0;
  80. enum polarisation {
  81. POLARISATION_HORIZONTAL = 0x00,
  82. POLARISATION_VERTICAL = 0x01,
  83. POLARISATION_CIRCULAR_LEFT = 0x02,
  84. POLARISATION_CIRCULAR_RIGHT = 0x03
  85. };
  86. enum running_mode {
  87. RM_NOT_RUNNING = 0x01,
  88. RM_STARTS_SOON = 0x02,
  89. RM_PAUSING = 0x03,
  90. RM_RUNNING = 0x04
  91. };
  92. #define AUDIO_CHAN_MAX (32)
  93. #define CA_SYSTEM_ID_MAX (16)
  94. struct service {
  95. struct list_head list;
  96. int transport_stream_id;
  97. int service_id;
  98. char *provider_name;
  99. char *service_name;
  100. uint16_t pmt_pid;
  101. uint16_t pcr_pid;
  102. uint16_t video_pid;
  103. uint16_t audio_pid[AUDIO_CHAN_MAX];
  104. char audio_lang[AUDIO_CHAN_MAX][4];
  105. int audio_num;
  106. uint16_t ca_id[CA_SYSTEM_ID_MAX];
  107. int ca_num;
  108. uint16_t teletext_pid;
  109. uint16_t subtitling_pid;
  110. uint16_t ac3_pid;
  111. unsigned int type : 8;
  112. unsigned int scrambled : 1;
  113. enum running_mode running;
  114. void *priv;
  115. int channel_num;
  116. };
  117. struct transponder {
  118. struct list_head list;
  119. struct list_head services;
  120. int network_id;
  121. int original_network_id;
  122. int transport_stream_id;
  123. enum fe_type type;
  124. struct dvb_frontend_parameters param;
  125. enum polarisation polarisation; /* only for DVB-S */
  126. int orbital_pos; /* only for DVB-S */
  127. unsigned int we_flag : 1; /* West/East Flag - only for DVB-S */
  128. unsigned int scan_done : 1;
  129. unsigned int last_tuning_failed : 1;
  130. unsigned int other_frequency_flag : 1; /* DVB-T */
  131. unsigned int wrong_frequency : 1; /* DVB-T with other_frequency_flag */
  132. int n_other_f;
  133. uint32_t *other_f; /* DVB-T freqeuency-list descriptor */
  134. };
  135. struct section_buf {
  136. struct list_head list;
  137. const char *dmx_devname;
  138. unsigned int run_once : 1;
  139. unsigned int segmented : 1; /* segmented by table_id_ext */
  140. int fd;
  141. int pid;
  142. int table_id;
  143. int table_id_ext;
  144. int section_version_number;
  145. uint8_t section_done[32];
  146. int sectionfilter_done;
  147. unsigned char buf[1024];
  148. time_t timeout;
  149. time_t start_time;
  150. time_t running_time;
  151. struct section_buf *next_seg; /* this is used to handle
  152. * segmented tables (like NIT-other)
  153. */
  154. };
  155. static LIST_HEAD(scanned_transponders);
  156. static LIST_HEAD(new_transponders);
  157. static struct transponder *current_tp;
  158. static void dump_dvb_parameters (FILE *f, struct transponder *p);
  159. static void setup_filter (struct section_buf* s, const char *dmx_devname,
  160. int pid, int tid, int tid_ext,
  161. int run_once, int segmented, int timeout);
  162. static void add_filter (struct section_buf *s);
  163. static const char * fe_type2str(fe_type_t t);
  164. /* According to the DVB standards, the combination of network_id and
  165. * transport_stream_id should be unique, but in real life the satellite
  166. * operators and broadcasters don't care enough to coordinate
  167. * the numbering. Thus we identify TPs by frequency (dvbscan handles only
  168. * one satellite at a time). Further complication: Different NITs on
  169. * one satellite sometimes list the same TP with slightly different
  170. * frequencies, so we have to search within some bandwidth.
  171. */
  172. static struct transponder *alloc_transponder(uint32_t frequency)
  173. {
  174. struct transponder *tp = calloc(1, sizeof(*tp));
  175. tp->param.frequency = frequency;
  176. INIT_LIST_HEAD(&tp->list);
  177. INIT_LIST_HEAD(&tp->services);
  178. list_add_tail(&tp->list, &new_transponders);
  179. return tp;
  180. }
  181. static int is_same_transponder(uint32_t f1, uint32_t f2)
  182. {
  183. uint32_t diff;
  184. if (f1 == f2)
  185. return 1;
  186. diff = (f1 > f2) ? (f1 - f2) : (f2 - f1);
  187. //FIXME: use symbolrate etc. to estimate bandwidth
  188. if (diff < 2000) {
  189. debug("f1 = %u is same TP as f2 = %u\n", f1, f2);
  190. return 1;
  191. }
  192. return 0;
  193. }
  194. static struct transponder *find_transponder(uint32_t frequency)
  195. {
  196. struct list_head *pos;
  197. struct transponder *tp;
  198. list_for_each(pos, &scanned_transponders) {
  199. tp = list_entry(pos, struct transponder, list);
  200. if (current_tp_only)
  201. return tp;
  202. if (is_same_transponder(tp->param.frequency, frequency))
  203. return tp;
  204. }
  205. list_for_each(pos, &new_transponders) {
  206. tp = list_entry(pos, struct transponder, list);
  207. if (is_same_transponder(tp->param.frequency, frequency))
  208. return tp;
  209. }
  210. return NULL;
  211. }
  212. static void copy_transponder(struct transponder *d, struct transponder *s)
  213. {
  214. struct list_head *pos;
  215. struct service *service;
  216. if (d->transport_stream_id != s->transport_stream_id) {
  217. /* propagate change to any already allocated services */
  218. list_for_each(pos, &d->services) {
  219. service = list_entry(pos, struct service, list);
  220. service->transport_stream_id = s->transport_stream_id;
  221. }
  222. }
  223. d->network_id = s->network_id;
  224. d->original_network_id = s->original_network_id;
  225. d->transport_stream_id = s->transport_stream_id;
  226. d->type = s->type;
  227. memcpy(&d->param, &s->param, sizeof(d->param));
  228. d->polarisation = s->polarisation;
  229. d->orbital_pos = s->orbital_pos;
  230. d->we_flag = s->we_flag;
  231. d->scan_done = s->scan_done;
  232. d->last_tuning_failed = s->last_tuning_failed;
  233. d->other_frequency_flag = s->other_frequency_flag;
  234. d->n_other_f = s->n_other_f;
  235. if (d->n_other_f) {
  236. d->other_f = calloc(d->n_other_f, sizeof(uint32_t));
  237. memcpy(d->other_f, s->other_f, d->n_other_f * sizeof(uint32_t));
  238. }
  239. else
  240. d->other_f = NULL;
  241. }
  242. /* service_ids are guaranteed to be unique within one TP
  243. * (the DVB standards say theay should be unique within one
  244. * network, but in real life...)
  245. */
  246. static struct service *alloc_service(struct transponder *tp, int service_id)
  247. {
  248. struct service *s = calloc(1, sizeof(*s));
  249. INIT_LIST_HEAD(&s->list);
  250. s->service_id = service_id;
  251. s->transport_stream_id = tp->transport_stream_id;
  252. list_add_tail(&s->list, &tp->services);
  253. return s;
  254. }
  255. static struct service *find_service(struct transponder *tp, int service_id)
  256. {
  257. struct list_head *pos;
  258. struct service *s;
  259. list_for_each(pos, &tp->services) {
  260. s = list_entry(pos, struct service, list);
  261. if (s->service_id == service_id)
  262. return s;
  263. }
  264. return NULL;
  265. }
  266. static void parse_ca_identifier_descriptor (const unsigned char *buf,
  267. struct service *s)
  268. {
  269. unsigned char len = buf [1];
  270. unsigned int i;
  271. buf += 2;
  272. if (len > sizeof(s->ca_id)) {
  273. len = sizeof(s->ca_id);
  274. warning("too many CA system ids\n");
  275. }
  276. memcpy(s->ca_id, buf, len);
  277. for (i = 0; i < len / sizeof(s->ca_id[0]); i++)
  278. moreverbose(" CA ID 0x%04x\n", s->ca_id[i]);
  279. }
  280. static void parse_iso639_language_descriptor (const unsigned char *buf, struct service *s)
  281. {
  282. unsigned char len = buf [1];
  283. buf += 2;
  284. if (len >= 4) {
  285. debug(" LANG=%.3s %d\n", buf, buf[3]);
  286. memcpy(s->audio_lang[s->audio_num], buf, 3);
  287. #if 0
  288. /* seems like the audio_type is wrong all over the place */
  289. //if (buf[3] == 0) -> normal
  290. if (buf[3] == 1)
  291. s->audio_lang[s->audio_num][3] = '!'; /* clean effects (no language) */
  292. else if (buf[3] == 2)
  293. s->audio_lang[s->audio_num][3] = '?'; /* for the hearing impaired */
  294. else if (buf[3] == 3)
  295. s->audio_lang[s->audio_num][3] = '+'; /* visually impaired commentary */
  296. #endif
  297. }
  298. }
  299. static void parse_network_name_descriptor (const unsigned char *buf, void *dummy)
  300. {
  301. (void)dummy;
  302. unsigned char len = buf [1];
  303. info("Network Name '%.*s'\n", len, buf + 2);
  304. }
  305. static void parse_terrestrial_uk_channel_number (const unsigned char *buf, void *dummy)
  306. {
  307. (void)dummy;
  308. int i, n, channel_num, service_id;
  309. struct list_head *p1, *p2;
  310. struct transponder *t;
  311. struct service *s;
  312. // 32 bits per record
  313. n = buf[1] / 4;
  314. if (n < 1)
  315. return;
  316. // desc id, desc len, (service id, service number)
  317. buf += 2;
  318. for (i = 0; i < n; i++) {
  319. service_id = (buf[0]<<8)|(buf[1]&0xff);
  320. channel_num = ((buf[2]&0x03)<<8)|(buf[3]&0xff);
  321. debug("Service ID 0x%x has channel number %d ", service_id, channel_num);
  322. list_for_each(p1, &scanned_transponders) {
  323. t = list_entry(p1, struct transponder, list);
  324. list_for_each(p2, &t->services) {
  325. s = list_entry(p2, struct service, list);
  326. if (s->service_id == service_id)
  327. s->channel_num = channel_num;
  328. }
  329. }
  330. buf += 4;
  331. }
  332. }
  333. static long bcd32_to_cpu (const int b0, const int b1, const int b2, const int b3)
  334. {
  335. return ((b0 >> 4) & 0x0f) * 10000000 + (b0 & 0x0f) * 1000000 +
  336. ((b1 >> 4) & 0x0f) * 100000 + (b1 & 0x0f) * 10000 +
  337. ((b2 >> 4) & 0x0f) * 1000 + (b2 & 0x0f) * 100 +
  338. ((b3 >> 4) & 0x0f) * 10 + (b3 & 0x0f);
  339. }
  340. static const fe_code_rate_t fec_tab [8] = {
  341. FEC_AUTO, FEC_1_2, FEC_2_3, FEC_3_4,
  342. FEC_5_6, FEC_7_8, FEC_NONE, FEC_NONE
  343. };
  344. static const fe_modulation_t qam_tab [6] = {
  345. QAM_AUTO, QAM_16, QAM_32, QAM_64, QAM_128, QAM_256
  346. };
  347. static void parse_cable_delivery_system_descriptor (const unsigned char *buf,
  348. struct transponder *t)
  349. {
  350. if (!t) {
  351. warning("cable_delivery_system_descriptor outside transport stream definition (ignored)\n");
  352. return;
  353. }
  354. t->type = FE_QAM;
  355. t->param.frequency = bcd32_to_cpu (buf[2], buf[3], buf[4], buf[5]);
  356. t->param.frequency *= 100;
  357. t->param.u.qam.fec_inner = fec_tab[buf[12] & 0x07];
  358. t->param.u.qam.symbol_rate = 10 * bcd32_to_cpu (buf[9],
  359. buf[10],
  360. buf[11],
  361. buf[12] & 0xf0);
  362. if ((buf[8] & 0x0f) > 5)
  363. t->param.u.qam.modulation = QAM_AUTO;
  364. else
  365. t->param.u.qam.modulation = qam_tab[buf[8] & 0x0f];
  366. t->param.inversion = spectral_inversion;
  367. if (verbosity >= 5) {
  368. debug("%#04x/%#04x ", t->network_id, t->transport_stream_id);
  369. dump_dvb_parameters (stderr, t);
  370. if (t->scan_done)
  371. dprintf(5, " (done)");
  372. if (t->last_tuning_failed)
  373. dprintf(5, " (tuning failed)");
  374. dprintf(5, "\n");
  375. }
  376. }
  377. static void parse_satellite_delivery_system_descriptor (const unsigned char *buf,
  378. struct transponder *t)
  379. {
  380. if (!t) {
  381. warning("satellite_delivery_system_descriptor outside transport stream definition (ignored)\n");
  382. return;
  383. }
  384. t->type = FE_QPSK;
  385. t->param.frequency = 10 * bcd32_to_cpu (buf[2], buf[3], buf[4], buf[5]);
  386. t->param.u.qpsk.fec_inner = fec_tab[buf[12] & 0x07];
  387. t->param.u.qpsk.symbol_rate = 10 * bcd32_to_cpu (buf[9],
  388. buf[10],
  389. buf[11],
  390. buf[12] & 0xf0);
  391. t->polarisation = (buf[8] >> 5) & 0x03;
  392. t->param.inversion = spectral_inversion;
  393. t->orbital_pos = bcd32_to_cpu (0x00, 0x00, buf[6], buf[7]);
  394. t->we_flag = buf[8] >> 7;
  395. if (verbosity >= 5) {
  396. debug("%#04x/%#04x ", t->network_id, t->transport_stream_id);
  397. dump_dvb_parameters (stderr, t);
  398. if (t->scan_done)
  399. dprintf(5, " (done)");
  400. if (t->last_tuning_failed)
  401. dprintf(5, " (tuning failed)");
  402. dprintf(5, "\n");
  403. }
  404. }
  405. static void parse_terrestrial_delivery_system_descriptor (const unsigned char *buf,
  406. struct transponder *t)
  407. {
  408. static const fe_modulation_t m_tab [] = { QPSK, QAM_16, QAM_64, QAM_AUTO };
  409. static const fe_code_rate_t ofec_tab [8] = { FEC_1_2, FEC_2_3, FEC_3_4,
  410. FEC_5_6, FEC_7_8 };
  411. struct dvb_ofdm_parameters *o;
  412. if (!t) {
  413. warning("terrestrial_delivery_system_descriptor outside transport stream definition (ignored)\n");
  414. return;
  415. }
  416. o = &t->param.u.ofdm;
  417. t->type = FE_OFDM;
  418. t->param.frequency = (buf[2] << 24) | (buf[3] << 16);
  419. t->param.frequency |= (buf[4] << 8) | buf[5];
  420. t->param.frequency *= 10;
  421. t->param.inversion = spectral_inversion;
  422. o->bandwidth = BANDWIDTH_8_MHZ + ((buf[6] >> 5) & 0x3);
  423. o->constellation = m_tab[(buf[7] >> 6) & 0x3];
  424. o->hierarchy_information = HIERARCHY_NONE + ((buf[7] >> 3) & 0x3);
  425. if ((buf[7] & 0x7) > 4)
  426. o->code_rate_HP = FEC_AUTO;
  427. else
  428. o->code_rate_HP = ofec_tab [buf[7] & 0x7];
  429. if (((buf[8] >> 5) & 0x7) > 4)
  430. o->code_rate_LP = FEC_AUTO;
  431. else
  432. o->code_rate_LP = ofec_tab [(buf[8] >> 5) & 0x7];
  433. o->guard_interval = GUARD_INTERVAL_1_32 + ((buf[8] >> 3) & 0x3);
  434. o->transmission_mode = (buf[8] & 0x2) ?
  435. TRANSMISSION_MODE_8K :
  436. TRANSMISSION_MODE_2K;
  437. t->other_frequency_flag = (buf[8] & 0x01);
  438. if (verbosity >= 5) {
  439. debug("%#04x/%#04x ", t->network_id, t->transport_stream_id);
  440. dump_dvb_parameters (stderr, t);
  441. if (t->scan_done)
  442. dprintf(5, " (done)");
  443. if (t->last_tuning_failed)
  444. dprintf(5, " (tuning failed)");
  445. dprintf(5, "\n");
  446. }
  447. }
  448. static void parse_frequency_list_descriptor (const unsigned char *buf,
  449. struct transponder *t)
  450. {
  451. int n, i;
  452. typeof(*t->other_f) f;
  453. if (!t) {
  454. warning("frequency_list_descriptor outside transport stream definition (ignored)\n");
  455. return;
  456. }
  457. if (t->other_f)
  458. return;
  459. n = (buf[1] - 1) / 4;
  460. if (n < 1 || (buf[2] & 0x03) != 3)
  461. return;
  462. t->other_f = calloc(n, sizeof(*t->other_f));
  463. t->n_other_f = n;
  464. buf += 3;
  465. for (i = 0; i < n; i++) {
  466. f = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
  467. t->other_f[i] = f * 10;
  468. buf += 4;
  469. }
  470. }
  471. /*
  472. * handle character set correctly (e.g. via iconv)
  473. * c.f. EN 300 468 annex A
  474. */
  475. struct charset_conv {
  476. unsigned len;
  477. unsigned char data[3];
  478. };
  479. /* This table is the Latin 00 table. Basically ISO-6937 + Euro sign */
  480. struct charset_conv en300468_latin_00_to_utf8[256] = {
  481. [0x00] = { 1, {0x00, } },
  482. [0x01] = { 1, {0x01, } },
  483. [0x02] = { 1, {0x02, } },
  484. [0x03] = { 1, {0x03, } },
  485. [0x04] = { 1, {0x04, } },
  486. [0x05] = { 1, {0x05, } },
  487. [0x06] = { 1, {0x06, } },
  488. [0x07] = { 1, {0x07, } },
  489. [0x08] = { 1, {0x08, } },
  490. [0x09] = { 1, {0x09, } },
  491. [0x0a] = { 1, {0x0a, } },
  492. [0x0b] = { 1, {0x0b, } },
  493. [0x0c] = { 1, {0x0c, } },
  494. [0x0d] = { 1, {0x0d, } },
  495. [0x0e] = { 1, {0x0e, } },
  496. [0x0f] = { 1, {0x0f, } },
  497. [0x10] = { 1, {0x10, } },
  498. [0x11] = { 1, {0x11, } },
  499. [0x12] = { 1, {0x12, } },
  500. [0x13] = { 1, {0x13, } },
  501. [0x14] = { 1, {0x14, } },
  502. [0x15] = { 1, {0x15, } },
  503. [0x16] = { 1, {0x16, } },
  504. [0x17] = { 1, {0x17, } },
  505. [0x18] = { 1, {0x18, } },
  506. [0x19] = { 1, {0x19, } },
  507. [0x1a] = { 1, {0x1a, } },
  508. [0x1b] = { 1, {0x1b, } },
  509. [0x1c] = { 1, {0x1c, } },
  510. [0x1d] = { 1, {0x1d, } },
  511. [0x1e] = { 1, {0x1e, } },
  512. [0x1f] = { 1, {0x1f, } },
  513. [0x20] = { 1, {0x20, } },
  514. [0x21] = { 1, {0x21, } },
  515. [0x22] = { 1, {0x22, } },
  516. [0x23] = { 1, {0x23, } },
  517. [0x24] = { 1, {0x24, } },
  518. [0x25] = { 1, {0x25, } },
  519. [0x26] = { 1, {0x26, } },
  520. [0x27] = { 1, {0x27, } },
  521. [0x28] = { 1, {0x28, } },
  522. [0x29] = { 1, {0x29, } },
  523. [0x2a] = { 1, {0x2a, } },
  524. [0x2b] = { 1, {0x2b, } },
  525. [0x2c] = { 1, {0x2c, } },
  526. [0x2d] = { 1, {0x2d, } },
  527. [0x2e] = { 1, {0x2e, } },
  528. [0x2f] = { 1, {0x2f, } },
  529. [0x30] = { 1, {0x30, } },
  530. [0x31] = { 1, {0x31, } },
  531. [0x32] = { 1, {0x32, } },
  532. [0x33] = { 1, {0x33, } },
  533. [0x34] = { 1, {0x34, } },
  534. [0x35] = { 1, {0x35, } },
  535. [0x36] = { 1, {0x36, } },
  536. [0x37] = { 1, {0x37, } },
  537. [0x38] = { 1, {0x38, } },
  538. [0x39] = { 1, {0x39, } },
  539. [0x3a] = { 1, {0x3a, } },
  540. [0x3b] = { 1, {0x3b, } },
  541. [0x3c] = { 1, {0x3c, } },
  542. [0x3d] = { 1, {0x3d, } },
  543. [0x3e] = { 1, {0x3e, } },
  544. [0x3f] = { 1, {0x3f, } },
  545. [0x40] = { 1, {0x40, } },
  546. [0x41] = { 1, {0x41, } },
  547. [0x42] = { 1, {0x42, } },
  548. [0x43] = { 1, {0x43, } },
  549. [0x44] = { 1, {0x44, } },
  550. [0x45] = { 1, {0x45, } },
  551. [0x46] = { 1, {0x46, } },
  552. [0x47] = { 1, {0x47, } },
  553. [0x48] = { 1, {0x48, } },
  554. [0x49] = { 1, {0x49, } },
  555. [0x4a] = { 1, {0x4a, } },
  556. [0x4b] = { 1, {0x4b, } },
  557. [0x4c] = { 1, {0x4c, } },
  558. [0x4d] = { 1, {0x4d, } },
  559. [0x4e] = { 1, {0x4e, } },
  560. [0x4f] = { 1, {0x4f, } },
  561. [0x50] = { 1, {0x50, } },
  562. [0x51] = { 1, {0x51, } },
  563. [0x52] = { 1, {0x52, } },
  564. [0x53] = { 1, {0x53, } },
  565. [0x54] = { 1, {0x54, } },
  566. [0x55] = { 1, {0x55, } },
  567. [0x56] = { 1, {0x56, } },
  568. [0x57] = { 1, {0x57, } },
  569. [0x58] = { 1, {0x58, } },
  570. [0x59] = { 1, {0x59, } },
  571. [0x5a] = { 1, {0x5a, } },
  572. [0x5b] = { 1, {0x5b, } },
  573. [0x5c] = { 1, {0x5c, } },
  574. [0x5d] = { 1, {0x5d, } },
  575. [0x5e] = { 1, {0x5e, } },
  576. [0x5f] = { 1, {0x5f, } },
  577. [0x60] = { 1, {0x60, } },
  578. [0x61] = { 1, {0x61, } },
  579. [0x62] = { 1, {0x62, } },
  580. [0x63] = { 1, {0x63, } },
  581. [0x64] = { 1, {0x64, } },
  582. [0x65] = { 1, {0x65, } },
  583. [0x66] = { 1, {0x66, } },
  584. [0x67] = { 1, {0x67, } },
  585. [0x68] = { 1, {0x68, } },
  586. [0x69] = { 1, {0x69, } },
  587. [0x6a] = { 1, {0x6a, } },
  588. [0x6b] = { 1, {0x6b, } },
  589. [0x6c] = { 1, {0x6c, } },
  590. [0x6d] = { 1, {0x6d, } },
  591. [0x6e] = { 1, {0x6e, } },
  592. [0x6f] = { 1, {0x6f, } },
  593. [0x70] = { 1, {0x70, } },
  594. [0x71] = { 1, {0x71, } },
  595. [0x72] = { 1, {0x72, } },
  596. [0x73] = { 1, {0x73, } },
  597. [0x74] = { 1, {0x74, } },
  598. [0x75] = { 1, {0x75, } },
  599. [0x76] = { 1, {0x76, } },
  600. [0x77] = { 1, {0x77, } },
  601. [0x78] = { 1, {0x78, } },
  602. [0x79] = { 1, {0x79, } },
  603. [0x7a] = { 1, {0x7a, } },
  604. [0x7b] = { 1, {0x7b, } },
  605. [0x7c] = { 1, {0x7c, } },
  606. [0x7d] = { 1, {0x7d, } },
  607. [0x7e] = { 1, {0x7e, } },
  608. [0x7f] = { 1, {0x7f, } },
  609. [0x80] = { 2, {0xc2, 0x80, } },
  610. [0x81] = { 2, {0xc2, 0x81, } },
  611. [0x82] = { 2, {0xc2, 0x82, } },
  612. [0x83] = { 2, {0xc2, 0x83, } },
  613. [0x84] = { 2, {0xc2, 0x84, } },
  614. [0x85] = { 2, {0xc2, 0x85, } },
  615. [0x86] = { 2, {0xc2, 0x86, } },
  616. [0x87] = { 2, {0xc2, 0x87, } },
  617. [0x88] = { 2, {0xc2, 0x88, } },
  618. [0x89] = { 2, {0xc2, 0x89, } },
  619. [0x8a] = { 2, {0xc2, 0x8a, } },
  620. [0x8b] = { 2, {0xc2, 0x8b, } },
  621. [0x8c] = { 2, {0xc2, 0x8c, } },
  622. [0x8d] = { 2, {0xc2, 0x8d, } },
  623. [0x8e] = { 2, {0xc2, 0x8e, } },
  624. [0x8f] = { 2, {0xc2, 0x8f, } },
  625. [0x90] = { 2, {0xc2, 0x90, } },
  626. [0x91] = { 2, {0xc2, 0x91, } },
  627. [0x92] = { 2, {0xc2, 0x92, } },
  628. [0x93] = { 2, {0xc2, 0x93, } },
  629. [0x94] = { 2, {0xc2, 0x94, } },
  630. [0x95] = { 2, {0xc2, 0x95, } },
  631. [0x96] = { 2, {0xc2, 0x96, } },
  632. [0x97] = { 2, {0xc2, 0x97, } },
  633. [0x98] = { 2, {0xc2, 0x98, } },
  634. [0x99] = { 2, {0xc2, 0x99, } },
  635. [0x9a] = { 2, {0xc2, 0x9a, } },
  636. [0x9b] = { 2, {0xc2, 0x9b, } },
  637. [0x9c] = { 2, {0xc2, 0x9c, } },
  638. [0x9d] = { 2, {0xc2, 0x9d, } },
  639. [0x9e] = { 2, {0xc2, 0x9e, } },
  640. [0x9f] = { 2, {0xc2, 0x9f, } },
  641. [0xa0] = { 2, {0xc2, 0xa0, } },
  642. [0xa1] = { 2, {0xc2, 0xa1, } },
  643. [0xa2] = { 2, {0xc2, 0xa2, } },
  644. [0xa3] = { 2, {0xc2, 0xa3, } },
  645. [0xa4] = { 3, { 0xe2, 0x82, 0xac,} }, /* Euro sign. Addition over the ISO-6937 standard */
  646. [0xa5] = { 2, {0xc2, 0xa5, } },
  647. [0xa6] = { 0, {} },
  648. [0xa7] = { 2, {0xc2, 0xa7, } },
  649. [0xa8] = { 2, {0xc2, 0xa4, } },
  650. [0xa9] = { 3, {0xe2, 0x80, 0x98, } },
  651. [0xaa] = { 3, {0xe2, 0x80, 0x9c, } },
  652. [0xab] = { 2, {0xc2, 0xab, } },
  653. [0xac] = { 3, {0xe2, 0x86, 0x90, } },
  654. [0xad] = { 3, {0xe2, 0x86, 0x91, } },
  655. [0xae] = { 3, {0xe2, 0x86, 0x92, } },
  656. [0xaf] = { 3, {0xe2, 0x86, 0x93, } },
  657. [0xb0] = { 2, {0xc2, 0xb0, } },
  658. [0xb1] = { 2, {0xc2, 0xb1, } },
  659. [0xb2] = { 2, {0xc2, 0xb2, } },
  660. [0xb3] = { 2, {0xc2, 0xb3, } },
  661. [0xb4] = { 2, {0xc3, 0x97, } },
  662. [0xb5] = { 2, {0xc2, 0xb5, } },
  663. [0xb6] = { 2, {0xc2, 0xb6, } },
  664. [0xb7] = { 2, {0xc2, 0xb7, } },
  665. [0xb8] = { 2, {0xc3, 0xb7, } },
  666. [0xb9] = { 3, {0xe2, 0x80, 0x99, } },
  667. [0xba] = { 3, {0xe2, 0x80, 0x9d, } },
  668. [0xbb] = { 2, {0xc2, 0xbb, } },
  669. [0xbc] = { 2, {0xc2, 0xbc, } },
  670. [0xbd] = { 2, {0xc2, 0xbd, } },
  671. [0xbe] = { 2, {0xc2, 0xbe, } },
  672. [0xbf] = { 2, {0xc2, 0xbf, } },
  673. [0xc0] = { 0, {} },
  674. [0xc1] = { 0, {} },
  675. [0xc2] = { 0, {} },
  676. [0xc3] = { 0, {} },
  677. [0xc4] = { 0, {} },
  678. [0xc5] = { 0, {} },
  679. [0xc6] = { 0, {} },
  680. [0xc7] = { 0, {} },
  681. [0xc8] = { 0, {} },
  682. [0xc9] = { 0, {} },
  683. [0xca] = { 0, {} },
  684. [0xcb] = { 0, {} },
  685. [0xcc] = { 0, {} },
  686. [0xcd] = { 0, {} },
  687. [0xce] = { 0, {} },
  688. [0xcf] = { 0, {} },
  689. [0xd0] = { 3, {0xe2, 0x80, 0x94, } },
  690. [0xd1] = { 2, {0xc2, 0xb9, } },
  691. [0xd2] = { 2, {0xc2, 0xae, } },
  692. [0xd3] = { 2, {0xc2, 0xa9, } },
  693. [0xd4] = { 3, {0xe2, 0x84, 0xa2, } },
  694. [0xd5] = { 3, {0xe2, 0x99, 0xaa, } },
  695. [0xd6] = { 2, {0xc2, 0xac, } },
  696. [0xd7] = { 2, {0xc2, 0xa6, } },
  697. [0xd8] = { 0, {} },
  698. [0xd9] = { 0, {} },
  699. [0xda] = { 0, {} },
  700. [0xdb] = { 0, {} },
  701. [0xdc] = { 3, {0xe2, 0x85, 0x9b, } },
  702. [0xdd] = { 3, {0xe2, 0x85, 0x9c, } },
  703. [0xde] = { 3, {0xe2, 0x85, 0x9d, } },
  704. [0xdf] = { 3, {0xe2, 0x85, 0x9e, } },
  705. [0xe0] = { 3, {0xe2, 0x84, 0xa6, } },
  706. [0xe1] = { 2, {0xc3, 0x86, } },
  707. [0xe2] = { 2, {0xc3, 0x90, } },
  708. [0xe3] = { 2, {0xc2, 0xaa, } },
  709. [0xe4] = { 2, {0xc4, 0xa6, } },
  710. [0xe5] = { 0, {} },
  711. [0xe6] = { 2, {0xc4, 0xb2, } },
  712. [0xe7] = { 2, {0xc4, 0xbf, } },
  713. [0xe8] = { 2, {0xc5, 0x81, } },
  714. [0xe9] = { 2, {0xc3, 0x98, } },
  715. [0xea] = { 2, {0xc5, 0x92, } },
  716. [0xeb] = { 2, {0xc2, 0xba, } },
  717. [0xec] = { 2, {0xc3, 0x9e, } },
  718. [0xed] = { 2, {0xc5, 0xa6, } },
  719. [0xee] = { 2, {0xc5, 0x8a, } },
  720. [0xef] = { 2, {0xc5, 0x89, } },
  721. [0xf0] = { 2, {0xc4, 0xb8, } },
  722. [0xf1] = { 2, {0xc3, 0xa6, } },
  723. [0xf2] = { 2, {0xc4, 0x91, } },
  724. [0xf3] = { 2, {0xc3, 0xb0, } },
  725. [0xf4] = { 2, {0xc4, 0xa7, } },
  726. [0xf5] = { 2, {0xc4, 0xb1, } },
  727. [0xf6] = { 2, {0xc4, 0xb3, } },
  728. [0xf7] = { 2, {0xc5, 0x80, } },
  729. [0xf8] = { 2, {0xc5, 0x82, } },
  730. [0xf9] = { 2, {0xc3, 0xb8, } },
  731. [0xfa] = { 2, {0xc5, 0x93, } },
  732. [0xfb] = { 2, {0xc3, 0x9f, } },
  733. [0xfc] = { 2, {0xc3, 0xbe, } },
  734. [0xfd] = { 2, {0xc5, 0xa7, } },
  735. [0xfe] = { 2, {0xc5, 0x8b, } },
  736. [0xff] = { 2, {0xc2, 0xad, } },
  737. };
  738. static void descriptorcpy(char **dest, const unsigned char *src, size_t len)
  739. {
  740. size_t destlen, i;
  741. char *p, *type = default_charset;
  742. unsigned char *tmp = NULL;
  743. const unsigned char *s;
  744. int emphasis = 0, need_conversion = 1;
  745. if (*dest) {
  746. free (*dest);
  747. *dest = NULL;
  748. }
  749. if (!len)
  750. return;
  751. if (*src < 0x20) {
  752. switch (*src) {
  753. case 0x00: type = "ISO-6937"; break;
  754. case 0x01: type = "ISO-8859-5"; break;
  755. case 0x02: type = "ISO-8859-6"; break;
  756. case 0x03: type = "ISO-8859-7"; break;
  757. case 0x04: type = "ISO-8859-8"; break;
  758. case 0x05: type = "ISO-8859-9"; break;
  759. case 0x06: type = "ISO-8859-10"; break;
  760. case 0x07: type = "ISO-8859-11"; break;
  761. case 0x09: type = "ISO-8859-13"; break;
  762. case 0x0a: type = "ISO-8859-14"; break;
  763. case 0x0b: type = "ISO-8859-15"; break;
  764. case 0x11: type = "ISO-10646"; break;
  765. case 0x12: type = "ISO-2022-KR"; break;
  766. case 0x13: type = "GB2312"; break;
  767. case 0x14: type = "BIG5"; break;
  768. case 0x15: type = "ISO-10646/UTF-8"; break;
  769. case 0x10: /* ISO8859 */
  770. if ((*(src + 1) != 0) || *(src + 2) > 0x0f)
  771. break;
  772. src+=2;
  773. len-=2;
  774. switch(*src) {
  775. case 0x01: type = "ISO-8859-1"; break;
  776. case 0x02: type = "ISO-8859-2"; break;
  777. case 0x03: type = "ISO-8859-3"; break;
  778. case 0x04: type = "ISO-8859-4"; break;
  779. case 0x05: type = "ISO-8859-5"; break;
  780. case 0x06: type = "ISO-8859-6"; break;
  781. case 0x07: type = "ISO-8859-7"; break;
  782. case 0x08: type = "ISO-8859-8"; break;
  783. case 0x09: type = "ISO-8859-9"; break;
  784. case 0x0a: type = "ISO-8859-10"; break;
  785. case 0x0b: type = "ISO-8859-11"; break;
  786. case 0x0d: type = "ISO-8859-13"; break;
  787. case 0x0e: type = "ISO-8859-14"; break;
  788. case 0x0f: type = "ISO-8859-15"; break;
  789. }
  790. }
  791. src++;
  792. len--;
  793. }
  794. /*
  795. * Destination length should be bigger. As the worse case seems to
  796. * use 3 chars for one code, use it for destlen
  797. */
  798. destlen = len * 3;
  799. *dest = malloc(destlen + 1);
  800. /* Remove special chars */
  801. if (!strncasecmp(type, "ISO-8859", 8) || !strcasecmp(type, "ISO-6937")) {
  802. /*
  803. * Handles the ISO/IEC 10646 1-byte control codes
  804. * (EN 300 468 v1.11.1 Table A.1)
  805. * Emphasis will be represented as: *emphased*
  806. */
  807. tmp = malloc(len + 2);
  808. p = (char *)tmp;
  809. s = src;
  810. for (i = 0; i < len; i++, s++) {
  811. if (*s >= 0x20 && (*s < 0x80 || *s > 0x9f))
  812. *p++ = *s;
  813. else if (*s == 0x86) {
  814. *p++ = '*';
  815. emphasis = 1;
  816. } else if (*s == 0x87 && emphasis) {
  817. *p++ = '*';
  818. emphasis = 0;
  819. }
  820. }
  821. if (emphasis)
  822. *p++ = '*';
  823. *p = '\0';
  824. } else {
  825. /*
  826. * FIXME: need to handle the ISO/IEC 10646 2-byte control codes
  827. * (EN 300 468 v1.11.1 Table A.2)
  828. */
  829. }
  830. if (tmp)
  831. s = tmp;
  832. else
  833. s = src;
  834. p = *dest;
  835. if (!strcasecmp(type, "ISO-6937")) {
  836. unsigned char *p1, *p2;
  837. /* Convert charset to UTF-8 using Code table 00 - Latin */
  838. for (p1 = (unsigned char *)s; p1 < s + len; p1++)
  839. for (p2 = en300468_latin_00_to_utf8[*p1].data;
  840. p2 < en300468_latin_00_to_utf8[*p1].data + en300468_latin_00_to_utf8[*p1].len;
  841. p2++)
  842. *p++ = *p2;
  843. *p = '\0';
  844. /* If desired charset is not UTF-8, prepare for conversion */
  845. if (strcasecmp(output_charset, "UTF-8")) {
  846. if (tmp)
  847. free(tmp);
  848. tmp = (unsigned char *)*dest;
  849. len = p - *dest;
  850. *dest = malloc(destlen + 1);
  851. type = "UTF-8";
  852. s = tmp;
  853. } else
  854. need_conversion = 0;
  855. }
  856. /* Convert from original charset to the desired one */
  857. if (need_conversion) {
  858. char out_cs[strlen(output_charset) + 1 + sizeof(CS_OPTIONS)];
  859. p = *dest;
  860. strcpy(out_cs, output_charset);
  861. strcat(out_cs, CS_OPTIONS);
  862. iconv_t cd = iconv_open(out_cs, type);
  863. if (cd == (iconv_t)(-1)) {
  864. memcpy(p, s, len);
  865. p[len] = '\0';
  866. warning("Conversion from %s to %s not supported\n",
  867. type, output_charset);
  868. } else {
  869. iconv(cd, (char **)&s, &len, &p, &destlen);
  870. iconv_close(cd);
  871. *p = '\0';
  872. }
  873. }
  874. if (tmp)
  875. free(tmp);
  876. }
  877. static void parse_service_descriptor (const unsigned char *buf, struct service *s)
  878. {
  879. unsigned char len;
  880. s->type = buf[2];
  881. buf += 3;
  882. len = *buf;
  883. buf++;
  884. descriptorcpy(&s->provider_name, buf, len);
  885. buf += len;
  886. len = *buf;
  887. buf++;
  888. descriptorcpy(&s->service_name, buf, len);
  889. info("0x%04x 0x%04x: pmt_pid 0x%04x %s -- %s (%s%s)\n",
  890. s->transport_stream_id,
  891. s->service_id,
  892. s->pmt_pid,
  893. s->provider_name, s->service_name,
  894. s->running == RM_NOT_RUNNING ? "not running" :
  895. s->running == RM_STARTS_SOON ? "starts soon" :
  896. s->running == RM_PAUSING ? "pausing" :
  897. s->running == RM_RUNNING ? "running" : "???",
  898. s->scrambled ? ", scrambled" : "");
  899. }
  900. static int find_descriptor(uint8_t tag, const unsigned char *buf,
  901. int descriptors_loop_len,
  902. const unsigned char **desc, int *desc_len)
  903. {
  904. while (descriptors_loop_len > 0) {
  905. unsigned char descriptor_tag = buf[0];
  906. unsigned char descriptor_len = buf[1] + 2;
  907. if (!descriptor_len) {
  908. warning("descriptor_tag == 0x%02x, len is 0\n", descriptor_tag);
  909. break;
  910. }
  911. if (tag == descriptor_tag) {
  912. if (desc)
  913. *desc = buf;
  914. if (desc_len)
  915. *desc_len = descriptor_len;
  916. return 1;
  917. }
  918. buf += descriptor_len;
  919. descriptors_loop_len -= descriptor_len;
  920. }
  921. return 0;
  922. }
  923. static void parse_descriptors(enum table_type t, const unsigned char *buf,
  924. int descriptors_loop_len, void *data)
  925. {
  926. while (descriptors_loop_len > 0) {
  927. unsigned char descriptor_tag = buf[0];
  928. unsigned char descriptor_len = buf[1] + 2;
  929. if (!descriptor_len) {
  930. warning("descriptor_tag == 0x%02x, len is 0\n", descriptor_tag);
  931. break;
  932. }
  933. switch (descriptor_tag) {
  934. case 0x0a:
  935. if (t == PMT)
  936. parse_iso639_language_descriptor (buf, data);
  937. break;
  938. case 0x40:
  939. if (t == NIT)
  940. parse_network_name_descriptor (buf, data);
  941. break;
  942. case 0x43:
  943. if (t == NIT)
  944. parse_satellite_delivery_system_descriptor (buf, data);
  945. break;
  946. case 0x44:
  947. if (t == NIT)
  948. parse_cable_delivery_system_descriptor (buf, data);
  949. break;
  950. case 0x48:
  951. if (t == SDT)
  952. parse_service_descriptor (buf, data);
  953. break;
  954. case 0x53:
  955. if (t == SDT)
  956. parse_ca_identifier_descriptor (buf, data);
  957. break;
  958. case 0x5a:
  959. if (t == NIT)
  960. parse_terrestrial_delivery_system_descriptor (buf, data);
  961. break;
  962. case 0x62:
  963. if (t == NIT)
  964. parse_frequency_list_descriptor (buf, data);
  965. break;
  966. case 0x83:
  967. /* 0x83 is in the privately defined range of descriptor tags,
  968. * so we parse this only if the user says so to avoid
  969. * problems when 0x83 is something entirely different... */
  970. if (t == NIT && vdr_dump_channum)
  971. parse_terrestrial_uk_channel_number (buf, data);
  972. break;
  973. default:
  974. verbosedebug("skip descriptor 0x%02x\n", descriptor_tag);
  975. };
  976. buf += descriptor_len;
  977. descriptors_loop_len -= descriptor_len;
  978. }
  979. }
  980. static void parse_pat(const unsigned char *buf, int section_length,
  981. int transport_stream_id)
  982. {
  983. (void)transport_stream_id;
  984. while (section_length > 0) {
  985. struct service *s;
  986. int service_id = (buf[0] << 8) | buf[1];
  987. if (service_id == 0)
  988. goto skip; /* nit pid entry */
  989. /* SDT might have been parsed first... */
  990. s = find_service(current_tp, service_id);
  991. if (!s)
  992. s = alloc_service(current_tp, service_id);
  993. s->pmt_pid = ((buf[2] & 0x1f) << 8) | buf[3];
  994. if (!s->priv && s->pmt_pid) {
  995. s->priv = malloc(sizeof(struct section_buf));
  996. setup_filter(s->priv, demux_devname,
  997. s->pmt_pid, 0x02, s->service_id, 1, 0, 5);
  998. add_filter (s->priv);
  999. }
  1000. skip:
  1001. buf += 4;
  1002. section_length -= 4;
  1003. };
  1004. }
  1005. static void parse_pmt (const unsigned char *buf, int section_length, int service_id)
  1006. {
  1007. int program_info_len;
  1008. struct service *s;
  1009. char msg_buf[14 * AUDIO_CHAN_MAX + 1];
  1010. char *tmp;
  1011. int i;
  1012. s = find_service (current_tp, service_id);
  1013. if (!s) {
  1014. error("PMT for serivce_id 0x%04x was not in PAT\n", service_id);
  1015. return;
  1016. }
  1017. s->pcr_pid = ((buf[0] & 0x1f) << 8) | buf[1];
  1018. program_info_len = ((buf[2] & 0x0f) << 8) | buf[3];
  1019. buf += program_info_len + 4;
  1020. section_length -= program_info_len + 4;
  1021. while (section_length >= 5) {
  1022. int ES_info_len = ((buf[3] & 0x0f) << 8) | buf[4];
  1023. int elementary_pid = ((buf[1] & 0x1f) << 8) | buf[2];
  1024. switch (buf[0]) {
  1025. case 0x01:
  1026. case 0x02:
  1027. case 0x1b: /* H.264 video stream */
  1028. moreverbose(" VIDEO : PID 0x%04x\n", elementary_pid);
  1029. if (s->video_pid == 0)
  1030. s->video_pid = elementary_pid;
  1031. break;
  1032. case 0x03:
  1033. case 0x81: /* Audio per ATSC A/53B [2] Annex B */
  1034. case 0x0f: /* ADTS Audio Stream - usually AAC */
  1035. case 0x11: /* ISO/IEC 14496-3 Audio with LATM transport */
  1036. case 0x04:
  1037. moreverbose(" AUDIO : PID 0x%04x\n", elementary_pid);
  1038. if (s->audio_num < AUDIO_CHAN_MAX) {
  1039. s->audio_pid[s->audio_num] = elementary_pid;
  1040. parse_descriptors (PMT, buf + 5, ES_info_len, s);
  1041. s->audio_num++;
  1042. }
  1043. else
  1044. warning("more than %i audio channels, truncating\n",
  1045. AUDIO_CHAN_MAX);
  1046. break;
  1047. case 0x07:
  1048. moreverbose(" MHEG : PID 0x%04x\n", elementary_pid);
  1049. break;
  1050. case 0x0B:
  1051. moreverbose(" DSM-CC : PID 0x%04x\n", elementary_pid);
  1052. break;
  1053. case 0x06:
  1054. if (find_descriptor(0x56, buf + 5, ES_info_len, NULL, NULL)) {
  1055. moreverbose(" TELETEXT : PID 0x%04x\n", elementary_pid);
  1056. s->teletext_pid = elementary_pid;
  1057. break;
  1058. }
  1059. else if (find_descriptor(0x59, buf + 5, ES_info_len, NULL, NULL)) {
  1060. /* Note: The subtitling descriptor can also signal
  1061. * teletext subtitling, but then the teletext descriptor
  1062. * will also be present; so we can be quite confident
  1063. * that we catch DVB subtitling streams only here, w/o
  1064. * parsing the descriptor. */
  1065. moreverbose(" SUBTITLING: PID 0x%04x\n", elementary_pid);
  1066. s->subtitling_pid = elementary_pid;
  1067. break;
  1068. }
  1069. else if (find_descriptor(0x6a, buf + 5, ES_info_len, NULL, NULL)) {
  1070. moreverbose(" AC3 : PID 0x%04x\n", elementary_pid);
  1071. s->ac3_pid = elementary_pid;
  1072. break;
  1073. }
  1074. /* fall through */
  1075. default:
  1076. moreverbose(" OTHER : PID 0x%04x TYPE 0x%02x\n", elementary_pid, buf[0]);
  1077. };
  1078. buf += ES_info_len + 5;
  1079. section_length -= ES_info_len + 5;
  1080. };
  1081. tmp = msg_buf;
  1082. tmp += sprintf(tmp, "0x%04x (%.4s)", s->audio_pid[0], s->audio_lang[0]);
  1083. if (s->audio_num > AUDIO_CHAN_MAX) {
  1084. warning("more than %i audio channels: %i, truncating to %i\n",
  1085. AUDIO_CHAN_MAX, s->audio_num, AUDIO_CHAN_MAX);
  1086. s->audio_num = AUDIO_CHAN_MAX;
  1087. }
  1088. for (i=1; i<s->audio_num; i++)
  1089. tmp += sprintf(tmp, ", 0x%04x (%.4s)", s->audio_pid[i], s->audio_lang[i]);
  1090. debug("0x%04x 0x%04x: %s -- %s, pmt_pid 0x%04x, vpid 0x%04x, apid %s\n",
  1091. s->transport_stream_id,
  1092. s->service_id,
  1093. s->provider_name, s->service_name,
  1094. s->pmt_pid, s->video_pid, msg_buf);
  1095. }
  1096. static void parse_nit (const unsigned char *buf, int section_length, int network_id)
  1097. {
  1098. int descriptors_loop_len = ((buf[0] & 0x0f) << 8) | buf[1];
  1099. if (section_length < descriptors_loop_len + 4)
  1100. {
  1101. warning("section too short: network_id == 0x%04x, section_length == %i, "
  1102. "descriptors_loop_len == %i\n",
  1103. network_id, section_length, descriptors_loop_len);
  1104. return;
  1105. }
  1106. parse_descriptors (NIT, buf + 2, descriptors_loop_len, NULL);
  1107. section_length -= descriptors_loop_len + 4;
  1108. buf += descriptors_loop_len + 4;
  1109. while (section_length > 6) {
  1110. int transport_stream_id = (buf[0] << 8) | buf[1];
  1111. struct transponder *t, tn;
  1112. descriptors_loop_len = ((buf[4] & 0x0f) << 8) | buf[5];
  1113. if (section_length < descriptors_loop_len + 4)
  1114. {
  1115. warning("section too short: transport_stream_id == 0x%04x, "
  1116. "section_length == %i, descriptors_loop_len == %i\n",
  1117. transport_stream_id, section_length,
  1118. descriptors_loop_len);
  1119. break;
  1120. }
  1121. debug("transport_stream_id 0x%04x\n", transport_stream_id);
  1122. memset(&tn, 0, sizeof(tn));
  1123. tn.type = -1;
  1124. tn.network_id = network_id;
  1125. tn.original_network_id = (buf[2] << 8) | buf[3];
  1126. tn.transport_stream_id = transport_stream_id;
  1127. parse_descriptors (NIT, buf + 6, descriptors_loop_len, &tn);
  1128. if (tn.type == fe_info.type) {
  1129. /* only add if develivery_descriptor matches FE type */
  1130. t = find_transponder(tn.param.frequency);
  1131. if (!t)
  1132. t = alloc_transponder(tn.param.frequency);
  1133. copy_transponder(t, &tn);
  1134. }
  1135. section_length -= descriptors_loop_len + 6;
  1136. buf += descriptors_loop_len + 6;
  1137. }
  1138. }
  1139. static void parse_sdt (const unsigned char *buf, int section_length,
  1140. int transport_stream_id)
  1141. {
  1142. (void)transport_stream_id;
  1143. buf += 3; /* skip original network id + reserved field */
  1144. while (section_length >= 5) {
  1145. int service_id = (buf[0] << 8) | buf[1];
  1146. int descriptors_loop_len = ((buf[3] & 0x0f) << 8) | buf[4];
  1147. struct service *s;
  1148. if (section_length < descriptors_loop_len)
  1149. {
  1150. warning("section too short: service_id == 0x%02x, section_length == %i, "
  1151. "descriptors_loop_len == %i\n",
  1152. service_id, section_length,
  1153. descriptors_loop_len);
  1154. break;
  1155. }
  1156. s = find_service(current_tp, service_id);
  1157. if (!s)
  1158. /* maybe PAT has not yet been parsed... */
  1159. s = alloc_service(current_tp, service_id);
  1160. s->running = (buf[3] >> 5) & 0x7;
  1161. s->scrambled = (buf[3] >> 4) & 1;
  1162. parse_descriptors (SDT, buf + 5, descriptors_loop_len, s);
  1163. section_length -= descriptors_loop_len + 5;
  1164. buf += descriptors_loop_len + 5;
  1165. };
  1166. }
  1167. /* ATSC PSIP VCT */
  1168. static void parse_atsc_service_loc_desc(struct service *s,const unsigned char *buf)
  1169. {
  1170. struct ATSC_service_location_descriptor d = read_ATSC_service_location_descriptor(buf);
  1171. int i;
  1172. unsigned char *b = (unsigned char *) buf+5;
  1173. s->pcr_pid = d.PCR_PID;
  1174. for (i=0; i < d.number_elements; i++) {
  1175. struct ATSC_service_location_element e = read_ATSC_service_location_element(b);
  1176. switch (e.stream_type) {
  1177. case 0x02: /* video */
  1178. s->video_pid = e.elementary_PID;
  1179. moreverbose(" VIDEO : PID 0x%04x\n", e.elementary_PID);
  1180. break;
  1181. case 0x81: /* ATSC audio */
  1182. if (s->audio_num < AUDIO_CHAN_MAX) {
  1183. s->audio_pid[s->audio_num] = e.elementary_PID;
  1184. s->audio_lang[s->audio_num][0] = (e.ISO_639_language_code >> 16) & 0xff;
  1185. s->audio_lang[s->audio_num][1] = (e.ISO_639_language_code >> 8) & 0xff;
  1186. s->audio_lang[s->audio_num][2] = e.ISO_639_language_code & 0xff;
  1187. s->audio_num++;
  1188. }
  1189. moreverbose(" AUDIO : PID 0x%04x lang: %s\n",e.elementary_PID,s->audio_lang[s->audio_num-1]);
  1190. break;
  1191. default:
  1192. warning("unhandled stream_type: %x\n",e.stream_type);
  1193. break;
  1194. };
  1195. b += 6;
  1196. }
  1197. }
  1198. static void parse_atsc_ext_chan_name_desc(struct service *s,const unsigned char *buf)
  1199. {
  1200. unsigned char *b = (unsigned char *) buf+2;
  1201. int i,j;
  1202. int num_str = b[0];
  1203. b++;
  1204. for (i = 0; i < num_str; i++) {
  1205. int num_seg = b[3];
  1206. b += 4; /* skip lang code */
  1207. for (j = 0; j < num_seg; j++) {
  1208. int comp_type = b[0],/* mode = b[1],*/ num_bytes = b[2];
  1209. switch (comp_type) {
  1210. case 0x00:
  1211. if (s->service_name)
  1212. free(s->service_name);
  1213. s->service_name = malloc(num_bytes * sizeof(char) + 1);
  1214. memcpy(s->service_name,&b[3],num_bytes);
  1215. s->service_name[num_bytes] = '\0';
  1216. break;
  1217. default:
  1218. warning("compressed strings are not supported yet\n");
  1219. break;
  1220. }
  1221. b += 3 + num_bytes;
  1222. }
  1223. }
  1224. }
  1225. static void parse_psip_descriptors(struct service *s,const unsigned char *buf,int len)
  1226. {
  1227. unsigned char *b = (unsigned char *) buf;
  1228. int desc_len;
  1229. while (len > 0) {
  1230. desc_len = b[1];
  1231. switch (b[0]) {
  1232. case ATSC_SERVICE_LOCATION_DESCRIPTOR_ID:
  1233. parse_atsc_service_loc_desc(s,b);
  1234. break;
  1235. case ATSC_EXTENDED_CHANNEL_NAME_DESCRIPTOR_ID:
  1236. parse_atsc_ext_chan_name_desc(s,b);
  1237. break;
  1238. default:
  1239. warning("unhandled psip descriptor: %02x\n",b[0]);
  1240. break;
  1241. }
  1242. b += 2 + desc_len;
  1243. len -= 2 + desc_len;
  1244. }
  1245. }
  1246. static void parse_psip_vct (const unsigned char *buf, int section_length,
  1247. int table_id, int transport_stream_id)
  1248. {
  1249. (void)section_length;
  1250. (void)table_id;
  1251. (void)transport_stream_id;
  1252. /* int protocol_version = buf[0];*/
  1253. int num_channels_in_section = buf[1];
  1254. int i;
  1255. int pseudo_id = 0xffff;
  1256. unsigned char *b = (unsigned char *) buf + 2;
  1257. for (i = 0; i < num_channels_in_section; i++) {
  1258. struct service *s;
  1259. struct tvct_channel ch = read_tvct_channel(b);
  1260. switch (ch.service_type) {
  1261. case 0x01:
  1262. info("analog channels won't be put info channels.conf\n");
  1263. break;
  1264. case 0x02: /* ATSC TV */
  1265. case 0x03: /* ATSC Radio */
  1266. break;
  1267. case 0x04: /* ATSC Data */
  1268. default:
  1269. continue;
  1270. }
  1271. if (ch.program_number == 0)
  1272. ch.program_number = --pseudo_id;
  1273. s = find_service(current_tp, ch.program_number);
  1274. if (!s)
  1275. s = alloc_service(current_tp, ch.program_number);
  1276. if (s->service_name)
  1277. free(s->service_name);
  1278. s->service_name = malloc(7*sizeof(unsigned char));
  1279. /* TODO find a better solution to convert UTF-16 */
  1280. s->service_name[0] = ch.short_name0;
  1281. s->service_name[1] = ch.short_name1;
  1282. s->service_name[2] = ch.short_name2;
  1283. s->service_name[3] = ch.short_name3;
  1284. s->service_name[4] = ch.short_name4;
  1285. s->service_name[5] = ch.short_name5;
  1286. s->service_name[6] = ch.short_name6;
  1287. parse_psip_descriptors(s,&b[32],ch.descriptors_length);
  1288. s->channel_num = ch.major_channel_number << 10 | ch.minor_channel_number;
  1289. if (ch.hidden) {
  1290. s->running = RM_NOT_RUNNING;
  1291. info("service is not running, pseudo program_number.");
  1292. } else {
  1293. s->running = RM_RUNNING;
  1294. info("service is running.");
  1295. }
  1296. info(" Channel number: %d:%d. Name: '%s'\n",
  1297. ch.major_channel_number, ch.minor_channel_number,s->service_name);
  1298. b += 32 + ch.descriptors_length;
  1299. }
  1300. }
  1301. static int get_bit (uint8_t *bitfield, int bit)
  1302. {
  1303. return (bitfield[bit/8] >> (bit % 8)) & 1;
  1304. }
  1305. static void set_bit (uint8_t *bitfield, int bit)
  1306. {
  1307. bitfield[bit/8] |= 1 << (bit % 8);
  1308. }
  1309. /**
  1310. * returns 0 when more sections are expected
  1311. * 1 when all sections are read on this pid
  1312. * -1 on invalid table id
  1313. */
  1314. static int parse_section (struct section_buf *s)
  1315. {
  1316. const unsigned char *buf = s->buf;
  1317. int table_id;
  1318. int section_length;
  1319. int table_id_ext;
  1320. int section_version_number;
  1321. int section_number;
  1322. int last_section_number;
  1323. int i;
  1324. table_id = buf[0];
  1325. if (s->table_id != table_id)
  1326. return -1;
  1327. section_length = ((buf[1] & 0x0f) << 8) | buf[2];
  1328. table_id_ext = (buf[3] << 8) | buf[4];
  1329. section_version_number = (buf[5] >> 1) & 0x1f;
  1330. section_number = buf[6];
  1331. last_section_number = buf[7];
  1332. if (s->segmented && s->table_id_ext != -1 && s->table_id_ext != table_id_ext) {
  1333. /* find or allocate actual section_buf matching table_id_ext */
  1334. while (s->next_seg) {
  1335. s = s->next_seg;
  1336. if (s->table_id_ext == table_id_ext)
  1337. break;
  1338. }
  1339. if (s->table_id_ext != table_id_ext) {
  1340. assert(s->next_seg == NULL);
  1341. s->next_seg = calloc(1, sizeof(struct section_buf));
  1342. s->next_seg->segmented = s->segmented;
  1343. s->next_seg->run_once = s->run_once;
  1344. s->next_seg->timeout = s->timeout;
  1345. s = s->next_seg;
  1346. s->table_id = table_id;
  1347. s->table_id_ext = table_id_ext;
  1348. s->section_version_number = section_version_number;
  1349. }
  1350. }
  1351. if (s->section_version_number != section_version_number ||
  1352. s->table_id_ext != table_id_ext) {
  1353. struct section_buf *next_seg = s->next_seg;
  1354. if (s->section_version_number != -1 && s->table_id_ext != -1)
  1355. debug("section version_number or table_id_ext changed "
  1356. "%d -> %d / %04x -> %04x\n",
  1357. s->section_version_number, section_version_number,
  1358. s->table_id_ext, table_id_ext);
  1359. s->table_id_ext = table_id_ext;
  1360. s->section_version_number = section_version_number;
  1361. s->sectionfilter_done = 0;
  1362. memset (s->section_done, 0, sizeof(s->section_done));
  1363. s->next_seg = next_seg;
  1364. }
  1365. buf += 8; /* past generic table header */
  1366. section_length -= 5 + 4; /* header + crc */
  1367. if (section_length < 0) {
  1368. warning("truncated section (PID 0x%04x, lenght %d)",
  1369. s->pid, section_length + 9);
  1370. return 0;
  1371. }
  1372. if (!get_bit(s->section_done, section_number)) {
  1373. set_bit (s->section_done, section_number);
  1374. debug("pid 0x%02x tid 0x%02x table_id_ext 0x%04x, "
  1375. "%i/%i (version %i)\n",
  1376. s->pid, table_id, table_id_ext, section_number,
  1377. last_section_number, section_version_number);
  1378. switch (table_id) {
  1379. case 0x00:
  1380. verbose("PAT\n");
  1381. parse_pat (buf, section_length, table_id_ext);
  1382. break;
  1383. case 0x02:
  1384. verbose("PMT 0x%04x for service 0x%04x\n", s->pid, table_id_ext);
  1385. parse_pmt (buf, section_length, table_id_ext);
  1386. break;
  1387. case 0x41:
  1388. verbose("////////////////////////////////////////////// NIT other\n");
  1389. case 0x40:
  1390. verbose("NIT (%s TS)\n", table_id == 0x40 ? "actual":"other");
  1391. parse_nit (buf, section_length, table_id_ext);
  1392. break;
  1393. case 0x42:
  1394. case 0x46:
  1395. verbose("SDT (%s TS)\n", table_id == 0x42 ? "actual":"other");
  1396. parse_sdt (buf, section_length, table_id_ext);
  1397. break;
  1398. case 0xc8:
  1399. case 0xc9:
  1400. verbose("ATSC VCT\n");
  1401. parse_psip_vct(buf, section_length, table_id, table_id_ext);
  1402. break;
  1403. default:
  1404. ;
  1405. };
  1406. for (i = 0; i <= last_section_number; i++)
  1407. if (get_bit (s->section_done, i) == 0)
  1408. break;
  1409. if (i > last_section_number)
  1410. s->sectionfilter_done = 1;
  1411. }
  1412. if (s->segmented) {
  1413. /* always wait for timeout; this is because we don't now how
  1414. * many segments there are
  1415. */
  1416. return 0;
  1417. }
  1418. else if (s->sectionfilter_done)
  1419. return 1;
  1420. return 0;
  1421. }
  1422. static int read_sections (struct section_buf *s)
  1423. {
  1424. int section_length, count;
  1425. if (s->sectionfilter_done && !s->segmented)
  1426. return 1;
  1427. /* the section filter API guarantess that we get one full section
  1428. * per read(), provided that the buffer is large enough (it is)
  1429. */
  1430. if (((count = read (s->fd, s->buf, sizeof(s->buf))) < 0) && errno == EOVERFLOW)
  1431. count = read (s->fd, s->buf, sizeof(s->buf));
  1432. if (count < 0) {
  1433. errorn("read_sections: read error");
  1434. return -1;
  1435. }
  1436. if (count < 4)
  1437. return -1;
  1438. section_length = ((s->buf[1] & 0x0f) << 8) | s->buf[2];
  1439. if (count != section_length + 3)
  1440. return -1;
  1441. if (parse_section(s) == 1)
  1442. return 1;
  1443. return 0;
  1444. }
  1445. static LIST_HEAD(running_filters);
  1446. static LIST_HEAD(waiting_filters);
  1447. static int n_running;
  1448. #define MAX_RUNNING 27
  1449. static struct pollfd poll_fds[MAX_RUNNING];
  1450. static struct section_buf* poll_section_bufs[MAX_RUNNING];
  1451. static void setup_filter (struct section_buf* s, const char *dmx_devname,
  1452. int pid, int tid, int tid_ext,
  1453. int run_once, int segmented, int timeout)
  1454. {
  1455. memset (s, 0, sizeof(struct section_buf));
  1456. s->fd = -1;
  1457. s->dmx_devname = dmx_devname;
  1458. s->pid = pid;
  1459. s->table_id = tid;
  1460. s->run_once = run_once;
  1461. s->segmented = segmented;
  1462. if (long_timeout)
  1463. s->timeout = 5 * timeout;
  1464. else
  1465. s->timeout = timeout;
  1466. s->table_id_ext = tid_ext;
  1467. s->section_version_number = -1;
  1468. INIT_LIST_HEAD (&s->list);
  1469. }
  1470. static void update_poll_fds(void)
  1471. {
  1472. struct list_head *p;
  1473. struct section_buf* s;
  1474. int i;
  1475. memset(poll_section_bufs, 0, sizeof(poll_section_bufs));
  1476. for (i = 0; i < MAX_RUNNING; i++)
  1477. poll_fds[i].fd = -1;
  1478. i = 0;
  1479. list_for_each (p, &running_filters) {
  1480. if (i >= MAX_RUNNING)
  1481. fatal("too many poll_fds\n");
  1482. s = list_entry (p, struct section_buf, list);
  1483. if (s->fd == -1)
  1484. fatal("s->fd == -1 on running_filters\n");
  1485. verbosedebug("poll fd %d\n", s->fd);
  1486. poll_fds[i].fd = s->fd;
  1487. poll_fds[i].events = POLLIN;
  1488. poll_fds[i].revents = 0;
  1489. poll_section_bufs[i] = s;
  1490. i++;
  1491. }
  1492. if (i != n_running)
  1493. fatal("n_running is hosed\n");
  1494. }
  1495. static int start_filter (struct section_buf* s)
  1496. {
  1497. struct dmx_sct_filter_params f;
  1498. if (n_running >= MAX_RUNNING)
  1499. goto err0;
  1500. if ((s->fd = open (s->dmx_devname, O_RDWR | O_NONBLOCK)) < 0)
  1501. goto err0;
  1502. verbosedebug("start filter pid 0x%04x table_id 0x%02x\n", s->pid, s->table_id);
  1503. memset(&f, 0, sizeof(f));
  1504. f.pid = (uint16_t) s->pid;
  1505. if (s->table_id < 0x100 && s->table_id > 0) {
  1506. f.filter.filter[0] = (uint8_t) s->table_id;
  1507. f.filter.mask[0] = 0xff;
  1508. }
  1509. if (s->table_id_ext < 0x10000 && s->table_id_ext > 0) {
  1510. f.filter.filter[1] = (uint8_t) ((s->table_id_ext >> 8) & 0xff);
  1511. f.filter.filter[2] = (uint8_t) (s->table_id_ext & 0xff);
  1512. f.filter.mask[1] = 0xff;
  1513. f.filter.mask[2] = 0xff;
  1514. }
  1515. f.timeout = 0;
  1516. f.flags = DMX_IMMEDIATE_START | DMX_CHECK_CRC;
  1517. if (ioctl(s->fd, DMX_SET_FILTER, &f) == -1) {
  1518. errorn ("ioctl DMX_SET_FILTER failed");
  1519. goto err1;
  1520. }
  1521. s->sectionfilter_done = 0;
  1522. time(&s->start_time);
  1523. list_del_init (&s->list); /* might be in waiting filter list */
  1524. list_add (&s->list, &running_filters);
  1525. n_running++;
  1526. update_poll_fds();
  1527. return 0;
  1528. err1:
  1529. ioctl (s->fd, DMX_STOP);
  1530. close (s->fd);
  1531. err0:
  1532. return -1;
  1533. }
  1534. static void stop_filter (struct section_buf *s)
  1535. {
  1536. verbosedebug("stop filter pid 0x%04x\n", s->pid);
  1537. ioctl (s->fd, DMX_STOP);
  1538. close (s->fd);
  1539. s->fd = -1;
  1540. list_del (&s->list);
  1541. s->running_time += time(NULL) - s->start_time;
  1542. n_running--;
  1543. update_poll_fds();
  1544. }
  1545. static void add_filter (struct section_buf *s)
  1546. {
  1547. verbosedebug("add filter pid 0x%04x\n", s->pid);
  1548. if (start_filter (s))
  1549. list_add_tail (&s->list, &waiting_filters);
  1550. }
  1551. static void remove_filter (struct section_buf *s)
  1552. {
  1553. verbosedebug("remove filter pid 0x%04x\n", s->pid);
  1554. stop_filter (s);
  1555. while (!list_empty(&waiting_filters)) {
  1556. struct list_head *next = waiting_filters.next;
  1557. s = list_entry (next, struct section_buf, list);
  1558. if (start_filter (s))
  1559. break;
  1560. };
  1561. }
  1562. static void read_filters (void)
  1563. {
  1564. struct section_buf *s;
  1565. int i, n, done;
  1566. n = poll(poll_fds, n_running, 1000);
  1567. if (n == -1)
  1568. errorn("poll");
  1569. for (i = 0; i < n_running; i++) {
  1570. s = poll_section_bufs[i];
  1571. if (!s)
  1572. fatal("poll_section_bufs[%d] is NULL\n", i);
  1573. if (poll_fds[i].revents)
  1574. done = read_sections (s) == 1;
  1575. else
  1576. done = 0; /* timeout */
  1577. if (done || time(NULL) > s->start_time + s->timeout) {
  1578. if (s->run_once) {
  1579. if (done)
  1580. verbosedebug("filter done pid 0x%04x\n", s->pid);
  1581. else
  1582. warning("filter timeout pid 0x%04x\n", s->pid);
  1583. remove_filter (s);
  1584. }
  1585. }
  1586. }
  1587. }
  1588. static int mem_is_zero (const void *mem, int size)
  1589. {
  1590. const char *p = mem;
  1591. int i;
  1592. for (i=0; i<size; i++) {
  1593. if (p[i] != 0x00)
  1594. return 0;
  1595. }
  1596. return 1;
  1597. }
  1598. static int switch_pos = 0;
  1599. static int __tune_to_transponder (int frontend_fd, struct transponder *t)
  1600. {
  1601. struct dvb_frontend_parameters p;
  1602. fe_status_t s;
  1603. int i;
  1604. current_tp = t;
  1605. if (mem_is_zero (&t->param, sizeof(struct dvb_frontend_parameters)))
  1606. return -1;
  1607. memcpy (&p, &t->param, sizeof(struct dvb_frontend_parameters));
  1608. if (verbosity >= 1) {
  1609. dprintf(1, ">>> tune to: ");
  1610. dump_dvb_parameters (stderr, t);
  1611. if (t->last_tuning_failed)
  1612. dprintf(1, " (tuning failed)");
  1613. dprintf(1, "\n");
  1614. }
  1615. if (t->type == FE_QPSK) {
  1616. if (lnb_type.high_val) {
  1617. if (lnb_type.switch_val) {
  1618. /* Voltage-controlled switch */
  1619. int hiband = 0;
  1620. if (p.frequency >= lnb_type.switch_val)
  1621. hiband = 1;
  1622. setup_switch (frontend_fd,
  1623. switch_pos,
  1624. t->polarisation == POLARISATION_VERTICAL ? 0 : 1,
  1625. hiband);
  1626. usleep(50000);
  1627. if (hiband)
  1628. p.frequency = abs(p.frequency - lnb_type.high_val);
  1629. else
  1630. p.frequency = abs(p.frequency - lnb_type.low_val);
  1631. } else {
  1632. /* C-Band Multipoint LNBf */
  1633. p.frequency = abs(p.frequency - (t->polarisation == POLARISATION_VERTICAL ?
  1634. lnb_type.low_val: lnb_type.high_val));
  1635. }
  1636. } else {
  1637. /* Monopoint LNBf without switch */
  1638. p.frequency = abs(p.frequency - lnb_type.low_val);
  1639. }
  1640. if (verbosity >= 2)
  1641. dprintf(1,"DVB-S IF freq is %d\n",p.frequency);
  1642. }
  1643. if (ioctl(frontend_fd, FE_SET_FRONTEND, &p) == -1) {
  1644. errorn("Setting frontend parameters failed");
  1645. return -1;
  1646. }
  1647. for (i = 0; i < 10; i++) {
  1648. usleep (200000);
  1649. if (ioctl(frontend_fd, FE_READ_STATUS, &s) == -1) {
  1650. errorn("FE_READ_STATUS failed");
  1651. return -1;
  1652. }
  1653. verbose(">>> tuning status == 0x%02x\n", s);
  1654. if (s & FE_HAS_LOCK) {
  1655. t->last_tuning_failed = 0;
  1656. return 0;
  1657. }
  1658. }
  1659. warning(">>> tuning failed!!!\n");
  1660. t->last_tuning_failed = 1;
  1661. return -1;
  1662. }
  1663. static int set_delivery_system(int fd, unsigned type)
  1664. {
  1665. struct dtv_properties props;
  1666. struct dtv_property dvb_prop[1];
  1667. unsigned delsys;
  1668. switch (type) {
  1669. case FE_QPSK:
  1670. delsys = SYS_DVBS;
  1671. break;
  1672. case FE_QAM:
  1673. delsys = SYS_DVBC_ANNEX_AC;
  1674. break;
  1675. case FE_OFDM:
  1676. delsys = SYS_DVBT;
  1677. break;
  1678. case FE_ATSC:
  1679. delsys = SYS_ATSC;
  1680. break;
  1681. default:
  1682. return -1;
  1683. }
  1684. dvb_prop[0].cmd = DTV_DELIVERY_SYSTEM;
  1685. dvb_prop[0].u.data = delsys;
  1686. props.num = 1;
  1687. props.props = dvb_prop;
  1688. if (ioctl(fd, FE_SET_PROPERTY, &props) >= 0)
  1689. return 0;
  1690. return errno;
  1691. }
  1692. static int tune_to_transponder (int frontend_fd, struct transponder *t)
  1693. {
  1694. int rc;
  1695. /* move TP from "new" to "scanned" list */
  1696. list_del_init(&t->list);
  1697. list_add_tail(&t->list, &scanned_transponders);
  1698. t->scan_done = 1;
  1699. if (t->type != fe_info.type) {
  1700. rc = set_delivery_system(frontend_fd, t->type);
  1701. if (!rc)
  1702. fe_info.type = t->type;
  1703. }
  1704. if (t->type != fe_info.type) {
  1705. warning("frontend type (%s) is not compatible with requested tuning type (%s)\n",
  1706. fe_type2str(fe_info.type),fe_type2str(t->type));
  1707. /* ignore cable descriptors in sat NIT and vice versa */
  1708. t->last_tuning_failed = 1;
  1709. return -1;
  1710. }
  1711. if (__tune_to_transponder (frontend_fd, t) == 0)
  1712. return 0;
  1713. return __tune_to_transponder (frontend_fd, t);
  1714. }
  1715. static int tune_to_next_transponder (int frontend_fd)
  1716. {
  1717. struct list_head *pos, *tmp;
  1718. struct transponder *t, *to;
  1719. uint32_t freq;
  1720. list_for_each_safe(pos, tmp, &new_transponders) {
  1721. t = list_entry (pos, struct transponder, list);
  1722. retry:
  1723. if (tune_to_transponder (frontend_fd, t) == 0)
  1724. return 0;
  1725. next:
  1726. if (t->other_frequency_flag && t->other_f && t->n_other_f) {
  1727. /* check if the alternate freqeuncy is really new to us */
  1728. freq = t->other_f[t->n_other_f - 1];
  1729. t->n_other_f--;
  1730. if (find_transponder(freq))
  1731. goto next;
  1732. /* remember tuning to the old frequency failed */
  1733. to = calloc(1, sizeof(*to));
  1734. to->param.frequency = t->param.frequency;
  1735. to->wrong_frequency = 1;
  1736. INIT_LIST_HEAD(&to->list);
  1737. INIT_LIST_HEAD(&to->services);
  1738. list_add_tail(&to->list, &scanned_transponders);
  1739. copy_transponder(to, t);
  1740. t->param.frequency = freq;
  1741. info("retrying with f=%d\n", t->param.frequency);
  1742. goto retry;
  1743. }
  1744. }
  1745. return -1;
  1746. }
  1747. struct strtab {
  1748. const char *str;
  1749. int val;
  1750. };
  1751. static int str2enum(const char *str, const struct strtab *tab, int deflt)
  1752. {
  1753. while (tab->str) {
  1754. if (!strcmp(tab->str, str))
  1755. return tab->val;
  1756. tab++;
  1757. }
  1758. error("invalid enum value '%s'\n", str);
  1759. return deflt;
  1760. }
  1761. static const char * enum2str(int v, const struct strtab *tab, const char *deflt)
  1762. {
  1763. while (tab->str) {
  1764. if (v == tab->val)
  1765. return tab->str;
  1766. tab++;
  1767. }
  1768. error("invalid enum value '%d'\n", v);
  1769. return deflt;
  1770. }
  1771. static enum fe_code_rate str2fec(const char *fec)
  1772. {
  1773. struct strtab fectab[] = {
  1774. { "NONE", FEC_NONE },
  1775. { "1/2", FEC_1_2 },
  1776. { "2/3", FEC_2_3 },
  1777. { "3/4", FEC_3_4 },
  1778. { "4/5", FEC_4_5 },
  1779. { "5/6", FEC_5_6 },
  1780. { "6/7", FEC_6_7 },
  1781. { "7/8", FEC_7_8 },
  1782. { "8/9", FEC_8_9 },
  1783. { "AUTO", FEC_AUTO },
  1784. { NULL, 0 }
  1785. };
  1786. return str2enum(fec, fectab, FEC_AUTO);
  1787. }
  1788. static enum fe_modulation str2qam(const char *qam)
  1789. {
  1790. struct strtab qamtab[] = {
  1791. { "QPSK", QPSK },
  1792. { "QAM16", QAM_16 },
  1793. { "QAM32", QAM_32 },
  1794. { "QAM64", QAM_64 },
  1795. { "QAM128", QAM_128 },
  1796. { "QAM256", QAM_256 },
  1797. { "AUTO", QAM_AUTO },
  1798. { "8VSB", VSB_8 },
  1799. { "16VSB", VSB_16 },
  1800. { NULL, 0 }
  1801. };
  1802. return str2enum(qam, qamtab, QAM_AUTO);
  1803. }
  1804. static enum fe_bandwidth str2bandwidth(const char *bw)
  1805. {
  1806. struct strtab bwtab[] = {
  1807. { "8MHz", BANDWIDTH_8_MHZ },
  1808. { "7MHz", BANDWIDTH_7_MHZ },
  1809. { "6MHz", BANDWIDTH_6_MHZ },
  1810. { "AUTO", BANDWIDTH_AUTO },
  1811. { NULL, 0 }
  1812. };
  1813. return str2enum(bw, bwtab, BANDWIDTH_AUTO);
  1814. }
  1815. static enum fe_transmit_mode str2mode(const char *mode)
  1816. {
  1817. struct strtab modetab[] = {
  1818. { "2k", TRANSMISSION_MODE_2K },
  1819. { "8k", TRANSMISSION_MODE_8K },
  1820. { "AUTO", TRANSMISSION_MODE_AUTO },
  1821. { NULL, 0 }
  1822. };
  1823. return str2enum(mode, modetab, TRANSMISSION_MODE_AUTO);
  1824. }
  1825. static enum fe_guard_interval str2guard(const char *guard)
  1826. {
  1827. struct strtab guardtab[] = {
  1828. { "1/32", GUARD_INTERVAL_1_32 },
  1829. { "1/16", GUARD_INTERVAL_1_16 },
  1830. { "1/8", GUARD_INTERVAL_1_8 },
  1831. { "1/4", GUARD_INTERVAL_1_4 },
  1832. { "AUTO", GUARD_INTERVAL_AUTO },
  1833. { NULL, 0 }
  1834. };
  1835. return str2enum(guard, guardtab, GUARD_INTERVAL_AUTO);
  1836. }
  1837. static enum fe_hierarchy str2hier(const char *hier)
  1838. {
  1839. struct strtab hiertab[] = {
  1840. { "NONE", HIERARCHY_NONE },
  1841. { "1", HIERARCHY_1 },
  1842. { "2", HIERARCHY_2 },
  1843. { "4", HIERARCHY_4 },
  1844. { "AUTO", HIERARCHY_AUTO },
  1845. { NULL, 0 }
  1846. };
  1847. return str2enum(hier, hiertab, HIERARCHY_AUTO);
  1848. }
  1849. static const char * fe_type2str(fe_type_t t)
  1850. {
  1851. struct strtab typetab[] = {
  1852. { "QPSK", FE_QPSK,},
  1853. { "QAM", FE_QAM, },
  1854. { "OFDM", FE_OFDM,},
  1855. { "ATSC", FE_ATSC,},
  1856. { NULL, 0 }
  1857. };
  1858. return enum2str(t, typetab, "UNK");
  1859. }
  1860. static int tune_initial (int frontend_fd, const char *initial)
  1861. {
  1862. FILE *inif;
  1863. unsigned int f, sr;
  1864. char buf[200];
  1865. char pol[20], fec[20], qam[20], bw[20], fec2[20], mode[20], guard[20], hier[20];
  1866. struct transponder *t;
  1867. inif = fopen(initial, "r");
  1868. if (!inif) {
  1869. error("cannot open '%s': %d %m\n", initial, errno);
  1870. return -1;
  1871. }
  1872. while (fgets(buf, sizeof(buf), inif)) {
  1873. if (buf[0] == '#' || buf[0] == '\n')
  1874. ;
  1875. else if (sscanf(buf, "S %u %1[HVLR] %u %4s\n", &f, pol, &sr, fec) == 4) {
  1876. t = alloc_transponder(f);
  1877. t->type = FE_QPSK;
  1878. switch(pol[0]) {
  1879. case 'H':
  1880. case 'L':
  1881. t->polarisation = POLARISATION_HORIZONTAL;
  1882. break;
  1883. default:
  1884. t->polarisation = POLARISATION_VERTICAL;;
  1885. break;
  1886. }
  1887. t->param.inversion = spectral_inversion;
  1888. t->param.u.qpsk.symbol_rate = sr;
  1889. t->param.u.qpsk.fec_inner = str2fec(fec);
  1890. info("initial transponder %u %c %u %d\n",
  1891. t->param.frequency,
  1892. pol[0], sr,
  1893. t->param.u.qpsk.fec_inner);
  1894. }
  1895. else if (sscanf(buf, "C %u %u %4s %6s\n", &f, &sr, fec, qam) == 4) {
  1896. t = alloc_transponder(f);
  1897. t->type = FE_QAM;
  1898. t->param.inversion = spectral_inversion;
  1899. t->param.u.qam.symbol_rate = sr;
  1900. t->param.u.qam.fec_inner = str2fec(fec);
  1901. t->param.u.qam.modulation = str2qam(qam);
  1902. info("initial transponder %u %u %d %d\n",
  1903. t->param.frequency,
  1904. sr,
  1905. t->param.u.qam.fec_inner,
  1906. t->param.u.qam.modulation);
  1907. }
  1908. else if (sscanf(buf, "T %u %4s %4s %4s %7s %4s %4s %4s\n",
  1909. &f, bw, fec, fec2, qam, mode, guard, hier) == 8) {
  1910. t = alloc_transponder(f);
  1911. t->type = FE_OFDM;
  1912. t->param.inversion = spectral_inversion;
  1913. t->param.u.ofdm.bandwidth = str2bandwidth(bw);
  1914. t->param.u.ofdm.code_rate_HP = str2fec(fec);
  1915. if (t->param.u.ofdm.code_rate_HP == FEC_NONE)
  1916. t->param.u.ofdm.code_rate_HP = FEC_AUTO;
  1917. t->param.u.ofdm.code_rate_LP = str2fec(fec2);
  1918. if (t->param.u.ofdm.code_rate_LP == FEC_NONE)
  1919. t->param.u.ofdm.code_rate_LP = FEC_AUTO;
  1920. t->param.u.ofdm.constellation = str2qam(qam);
  1921. t->param.u.ofdm.transmission_mode = str2mode(mode);
  1922. t->param.u.ofdm.guard_interval = str2guard(guard);
  1923. t->param.u.ofdm.hierarchy_information = str2hier(hier);
  1924. info("initial transponder %u %d %d %d %d %d %d %d\n",
  1925. t->param.frequency,
  1926. t->param.u.ofdm.bandwidth,
  1927. t->param.u.ofdm.code_rate_HP,
  1928. t->param.u.ofdm.code_rate_LP,
  1929. t->param.u.ofdm.constellation,
  1930. t->param.u.ofdm.transmission_mode,
  1931. t->param.u.ofdm.guard_interval,
  1932. t->param.u.ofdm.hierarchy_information);
  1933. }
  1934. else if (sscanf(buf, "A %u %7s\n",
  1935. &f,qam) == 2) {
  1936. t = alloc_transponder(f);
  1937. t->type = FE_ATSC;
  1938. t->param.u.vsb.modulation = str2qam(qam);
  1939. } else
  1940. error("cannot parse'%s'\n", buf);
  1941. }
  1942. fclose(inif);
  1943. return tune_to_next_transponder(frontend_fd);
  1944. }
  1945. static void scan_tp_atsc(void)
  1946. {
  1947. struct section_buf s0,s1,s2;
  1948. if (no_ATSC_PSIP) {
  1949. setup_filter(&s0, demux_devname, 0x00, 0x00, -1, 1, 0, 5); /* PAT */
  1950. add_filter(&s0);
  1951. } else {
  1952. if (ATSC_type & 0x1) {
  1953. setup_filter(&s0, demux_devname, 0x1ffb, 0xc8, -1, 1, 0, 5); /* terrestrial VCT */
  1954. add_filter(&s0);
  1955. }
  1956. if (ATSC_type & 0x2) {
  1957. setup_filter(&s1, demux_devname, 0x1ffb, 0xc9, -1, 1, 0, 5); /* cable VCT */
  1958. add_filter(&s1);
  1959. }
  1960. setup_filter(&s2, demux_devname, 0x00, 0x00, -1, 1, 0, 5); /* PAT */
  1961. add_filter(&s2);
  1962. }
  1963. do {
  1964. read_filters ();
  1965. } while (!(list_empty(&running_filters) &&
  1966. list_empty(&waiting_filters)));
  1967. }
  1968. static void scan_tp_dvb (void)
  1969. {
  1970. struct section_buf s0;
  1971. struct section_buf s1;
  1972. struct section_buf s2;
  1973. struct section_buf s3;
  1974. /**
  1975. * filter timeouts > min repetition rates specified in ETR211
  1976. */
  1977. setup_filter (&s0, demux_devname, 0x00, 0x00, -1, 1, 0, 5); /* PAT */
  1978. setup_filter (&s1, demux_devname, 0x11, 0x42, -1, 1, 0, 5); /* SDT */
  1979. add_filter (&s0);
  1980. add_filter (&s1);
  1981. if (!current_tp_only || output_format != OUTPUT_PIDS) {
  1982. setup_filter (&s2, demux_devname, 0x10, 0x40, -1, 1, 0, 15); /* NIT */
  1983. add_filter (&s2);
  1984. if (get_other_nits) {
  1985. /* get NIT-others
  1986. * Note: There is more than one NIT-other: one per
  1987. * network, separated by the network_id.
  1988. */
  1989. setup_filter (&s3, demux_devname, 0x10, 0x41, -1, 1, 1, 15);
  1990. add_filter (&s3);
  1991. }
  1992. }
  1993. do {
  1994. read_filters ();
  1995. } while (!(list_empty(&running_filters) &&
  1996. list_empty(&waiting_filters)));
  1997. }
  1998. static void scan_tp(void)
  1999. {
  2000. switch(fe_info.type) {
  2001. case FE_QPSK:
  2002. case FE_QAM:
  2003. case FE_OFDM:
  2004. scan_tp_dvb();
  2005. break;
  2006. case FE_ATSC:
  2007. scan_tp_atsc();
  2008. break;
  2009. default:
  2010. break;
  2011. }
  2012. }
  2013. static void scan_network (int frontend_fd, const char *initial)
  2014. {
  2015. if (tune_initial (frontend_fd, initial) < 0) {
  2016. error("initial tuning failed\n");
  2017. return;
  2018. }
  2019. do {
  2020. scan_tp();
  2021. } while (tune_to_next_transponder(frontend_fd) == 0);
  2022. }
  2023. static void pids_dump_service_parameter_set(FILE *f, struct service *s)
  2024. {
  2025. int i;
  2026. fprintf(f, "%-24.24s (0x%04x) %02x: ", s->service_name, s->service_id, s->type);
  2027. if (!s->pcr_pid || (s->type > 2))
  2028. fprintf(f, " ");
  2029. else if (s->pcr_pid == s->video_pid)
  2030. fprintf(f, "PCR == V ");
  2031. else if ((s->audio_num == 1) && (s->pcr_pid == s->audio_pid[0]))
  2032. fprintf(f, "PCR == A ");
  2033. else
  2034. fprintf(f, "PCR 0x%04x ", s->pcr_pid);
  2035. if (s->video_pid)
  2036. fprintf(f, "V 0x%04x", s->video_pid);
  2037. else
  2038. fprintf(f, " ");
  2039. if (s->audio_num)
  2040. fprintf(f, " A");
  2041. for (i = 0; i < s->audio_num; i++) {
  2042. fprintf(f, " 0x%04x", s->audio_pid[i]);
  2043. if (s->audio_lang[i][0])
  2044. fprintf(f, " (%.3s)", s->audio_lang[i]);
  2045. else if (s->audio_num == 1)
  2046. fprintf(f, " ");
  2047. }
  2048. if (s->teletext_pid)
  2049. fprintf(f, " TT 0x%04x", s->teletext_pid);
  2050. if (s->ac3_pid)
  2051. fprintf(f, " AC3 0x%04x", s->ac3_pid);
  2052. if (s->subtitling_pid)
  2053. fprintf(f, " SUB 0x%04x", s->subtitling_pid);
  2054. fprintf(f, "\n");
  2055. }
  2056. static char sat_polarisation (struct transponder *t)
  2057. {
  2058. return t->polarisation == POLARISATION_VERTICAL ? 'v' : 'h';
  2059. }
  2060. static int sat_number (struct transponder *t)
  2061. {
  2062. (void) t;
  2063. return switch_pos;
  2064. }
  2065. static void dump_lists (void)
  2066. {
  2067. struct list_head *p1, *p2;
  2068. struct transponder *t;
  2069. struct service *s;
  2070. int n = 0, i;
  2071. char sn[20];
  2072. int anon_services = 0;
  2073. list_for_each(p1, &scanned_transponders) {
  2074. t = list_entry(p1, struct transponder, list);
  2075. if (t->wrong_frequency)
  2076. continue;
  2077. list_for_each(p2, &t->services) {
  2078. n++;
  2079. }
  2080. }
  2081. info("dumping lists (%d services)\n", n);
  2082. list_for_each(p1, &scanned_transponders) {
  2083. t = list_entry(p1, struct transponder, list);
  2084. if (t->wrong_frequency)
  2085. continue;
  2086. list_for_each(p2, &t->services) {
  2087. s = list_entry(p2, struct service, list);
  2088. if (!s->service_name) {
  2089. /* not in SDT */
  2090. if (unique_anon_services)
  2091. snprintf(sn, sizeof(sn), "[%03x-%04x]",
  2092. anon_services, s->service_id);
  2093. else
  2094. snprintf(sn, sizeof(sn), "[%04x]",
  2095. s->service_id);
  2096. s->service_name = strdup(sn);
  2097. anon_services++;
  2098. }
  2099. /* ':' is field separator in szap and vdr service lists */
  2100. for (i = 0; s->service_name[i]; i++) {
  2101. if (s->service_name[i] == ':')
  2102. s->service_name[i] = ' ';
  2103. }
  2104. for (i = 0; s->provider_name && s->provider_name[i]; i++) {
  2105. if (s->provider_name[i] == ':')
  2106. s->provider_name[i] = ' ';
  2107. }
  2108. if (s->video_pid && !(serv_select & 1))
  2109. continue; /* no TV services */
  2110. if (!s->video_pid && s->audio_num && !(serv_select & 2))
  2111. continue; /* no radio services */
  2112. if (!s->video_pid && !s->audio_num && !(serv_select & 4))
  2113. continue; /* no data/other services */
  2114. if (s->scrambled && !ca_select)
  2115. continue; /* FTA only */
  2116. switch (output_format)
  2117. {
  2118. case OUTPUT_PIDS:
  2119. pids_dump_service_parameter_set (stdout, s);
  2120. break;
  2121. case OUTPUT_VDR:
  2122. vdr_dump_service_parameter_set (stdout,
  2123. s->service_name,
  2124. s->provider_name,
  2125. t->type,
  2126. &t->param,
  2127. sat_polarisation(t),
  2128. s->video_pid,
  2129. s->pcr_pid,
  2130. s->audio_pid,
  2131. s->audio_lang,
  2132. s->audio_num,
  2133. s->teletext_pid,
  2134. s->scrambled,
  2135. //FIXME: s->subtitling_pid
  2136. s->ac3_pid,
  2137. s->service_id,
  2138. t->original_network_id,
  2139. s->transport_stream_id,
  2140. t->orbital_pos,
  2141. t->we_flag,
  2142. vdr_dump_provider,
  2143. ca_select,
  2144. vdr_version,
  2145. vdr_dump_channum,
  2146. s->channel_num);
  2147. break;
  2148. case OUTPUT_ZAP:
  2149. zap_dump_service_parameter_set (stdout,
  2150. s->service_name,
  2151. t->type,
  2152. &t->param,
  2153. sat_polarisation(t),
  2154. sat_number(t),
  2155. s->video_pid,
  2156. s->audio_pid,
  2157. s->service_id);
  2158. default:
  2159. break;
  2160. }
  2161. }
  2162. }
  2163. info("Done.\n");
  2164. }
  2165. static void show_existing_tuning_data_files(void)
  2166. {
  2167. #ifndef DATADIR
  2168. #define DATADIR "/usr/local/share"
  2169. #endif
  2170. static const char* prefixlist[] = { DATADIR "/dvb", "/etc/dvb",
  2171. DATADIR "/doc/packages/dvb", 0 };
  2172. unsigned int i;
  2173. const char **prefix;
  2174. fprintf(stderr, "initial tuning data files:\n");
  2175. for (prefix = prefixlist; *prefix; prefix++) {
  2176. glob_t globbuf;
  2177. char* globspec = malloc (strlen(*prefix)+9);
  2178. strcpy (globspec, *prefix); strcat (globspec, "/dvb-?/*");
  2179. if (! glob (globspec, 0, 0, &globbuf)) {
  2180. for (i=0; i < globbuf.gl_pathc; i++)
  2181. fprintf(stderr, " file: %s\n", globbuf.gl_pathv[i]);
  2182. }
  2183. free (globspec);
  2184. globfree (&globbuf);
  2185. }
  2186. }
  2187. static void handle_sigint(int sig)
  2188. {
  2189. (void)sig;
  2190. error("interrupted by SIGINT, dumping partial result...\n");
  2191. dump_lists();
  2192. exit(2);
  2193. }
  2194. static const char *usage = "\n"
  2195. "usage: %s [options...] [-c | initial-tuning-data-file]\n"
  2196. " atsc/dvbscan doesn't do frequency scans, hence it needs initial\n"
  2197. " tuning data for at least one transponder/channel.\n"
  2198. " -c scan on currently tuned transponder only\n"
  2199. " -v verbose (repeat for more)\n"
  2200. " -q quiet (repeat for less)\n"
  2201. " -a N use DVB /dev/dvb/adapterN/\n"
  2202. " -f N use DVB /dev/dvb/adapter?/frontendN\n"
  2203. " -d N use DVB /dev/dvb/adapter?/demuxN\n"
  2204. " -s N use DiSEqC switch position N (DVB-S only)\n"
  2205. " -i N spectral inversion setting (0: off, 1: on, 2: auto [default])\n"
  2206. " -n evaluate NIT-other for full network scan (slow!)\n"
  2207. " -5 multiply all filter timeouts by factor 5\n"
  2208. " for non-DVB-compliant section repitition rates\n"
  2209. " -o fmt output format: 'zap' (default), 'vdr' or 'pids' (default with -c)\n"
  2210. " -x N Conditional Access, (default -1)\n"
  2211. " N=0 gets only FTA channels\n"
  2212. " N=-1 gets all channels\n"
  2213. " N=xxx sets ca field in vdr output to :xxx:\n"
  2214. " -t N Service select, Combined bitfield parameter.\n"
  2215. " 1 = TV, 2 = Radio, 4 = Other, (default 7)\n"
  2216. " -p for vdr output format: dump provider name\n"
  2217. " -e N VDR version, default 3 for VDR-1.3.x and newer\n"
  2218. " value 2 sets NIT and TID to zero\n"
  2219. " Vdr version 1.3.x and up implies -p.\n"
  2220. " -l lnb-type (DVB-S Only) (use -l help to print types) or \n"
  2221. " -l low[,high[,switch]] in Mhz\n"
  2222. " -u UK DVB-T Freeview channel numbering for VDR\n\n"
  2223. " -P do not use ATSC PSIP tables for scanning\n"
  2224. " (but only PAT and PMT) (applies for ATSC only)\n"
  2225. " -A N check for ATSC 1=Terrestrial [default], 2=Cable or 3=both\n"
  2226. " -U Uniquely name unknown services\n"
  2227. " -C cs Override default charset for service name/provider (default = ISO-6937)\n"
  2228. " -D cs Output charset (default = %s)\n"
  2229. "Supported charsets by -C/-D parameters can be obtained via 'iconv -l' command\n";
  2230. void
  2231. bad_usage(char *pname, int problem)
  2232. {
  2233. int i;
  2234. struct lnb_types_st *lnbp;
  2235. char **cp;
  2236. switch (problem) {
  2237. default:
  2238. case 0:
  2239. fprintf (stderr, usage, pname, output_charset);
  2240. break;
  2241. case 1:
  2242. i = 0;
  2243. fprintf(stderr, "-l <lnb-type> or -l low[,high[,switch]] in Mhz\n"
  2244. "where <lnb-type> is:\n");
  2245. while(NULL != (lnbp = lnb_enum(i))) {
  2246. fprintf (stderr, "%s\n", lnbp->name);
  2247. for (cp = lnbp->desc; *cp ; cp++) {
  2248. fprintf (stderr, " %s\n", *cp);
  2249. }
  2250. i++;
  2251. }
  2252. break;
  2253. case 2:
  2254. show_existing_tuning_data_files();
  2255. fprintf (stderr, usage, pname);
  2256. }
  2257. }
  2258. int main (int argc, char **argv)
  2259. {
  2260. char frontend_devname [80];
  2261. int adapter = 0, frontend = 0, demux = 0;
  2262. int opt, i;
  2263. int frontend_fd;
  2264. int fe_open_mode;
  2265. const char *initial = NULL;
  2266. char *charset;
  2267. if (argc <= 1) {
  2268. bad_usage(argv[0], 2);
  2269. return -1;
  2270. }
  2271. /*
  2272. * Get the environment charset, and use it as the default
  2273. * output charset. In thesis, using nl_langinfo should be
  2274. * enough, but, in my tests, it is not as reliable as checking
  2275. * the environment vars directly.
  2276. */
  2277. if ((charset = getenv("LC_ALL")) ||
  2278. (charset = getenv("LC_CTYPE")) ||
  2279. (charset = getenv ("LANG"))) {
  2280. while (*charset != '.' && *charset)
  2281. charset++;
  2282. if (*charset == '.')
  2283. charset++;
  2284. if (*charset)
  2285. output_charset = charset;
  2286. else
  2287. output_charset = nl_langinfo(CODESET);
  2288. } else
  2289. output_charset = nl_langinfo(CODESET);
  2290. /* start with default lnb type */
  2291. lnb_type = *lnb_enum(0);
  2292. while ((opt = getopt(argc, argv, "5cnpa:f:d:s:o:x:e:t:i:l:vquPA:UC:D:")) != -1) {
  2293. switch (opt) {
  2294. case 'a':
  2295. adapter = strtoul(optarg, NULL, 0);
  2296. break;
  2297. case 'c':
  2298. current_tp_only = 1;
  2299. if (!output_format_set)
  2300. output_format = OUTPUT_PIDS;
  2301. break;
  2302. case 'n':
  2303. get_other_nits = 1;
  2304. break;
  2305. case 'd':
  2306. demux = strtoul(optarg, NULL, 0);
  2307. break;
  2308. case 'f':
  2309. frontend = strtoul(optarg, NULL, 0);
  2310. break;
  2311. case 'p':
  2312. vdr_dump_provider = 1;
  2313. break;
  2314. case 's':
  2315. switch_pos = strtoul(optarg, NULL, 0);
  2316. break;
  2317. case 'o':
  2318. if (strcmp(optarg, "zap") == 0) output_format = OUTPUT_ZAP;
  2319. else if (strcmp(optarg, "vdr") == 0) output_format = OUTPUT_VDR;
  2320. else if (strcmp(optarg, "pids") == 0) output_format = OUTPUT_PIDS;
  2321. else {
  2322. bad_usage(argv[0], 0);
  2323. return -1;
  2324. }
  2325. output_format_set = 1;
  2326. break;
  2327. case '5':
  2328. long_timeout = 1;
  2329. break;
  2330. case 'x':
  2331. ca_select = strtoul(optarg, NULL, 0);
  2332. break;
  2333. case 'e':
  2334. vdr_version = strtoul(optarg, NULL, 0);
  2335. break;
  2336. case 't':
  2337. serv_select = strtoul(optarg, NULL, 0);
  2338. break;
  2339. case 'i':
  2340. spectral_inversion = strtoul(optarg, NULL, 0);
  2341. break;
  2342. case 'l':
  2343. if (lnb_decode(optarg, &lnb_type) < 0) {
  2344. bad_usage(argv[0], 1);
  2345. return -1;
  2346. }
  2347. break;
  2348. case 'v':
  2349. verbosity++;
  2350. break;
  2351. case 'q':
  2352. if (--verbosity < 0)
  2353. verbosity = 0;
  2354. break;
  2355. case 'u':
  2356. vdr_dump_channum = 1;
  2357. break;
  2358. case 'P':
  2359. no_ATSC_PSIP = 1;
  2360. break;
  2361. case 'A':
  2362. ATSC_type = strtoul(optarg,NULL,0);
  2363. if (ATSC_type == 0 || ATSC_type > 3) {
  2364. bad_usage(argv[0], 1);
  2365. return -1;
  2366. }
  2367. break;
  2368. case 'U':
  2369. unique_anon_services = 1;
  2370. break;
  2371. case 'C':
  2372. default_charset = optarg;
  2373. break;
  2374. case 'D':
  2375. output_charset = optarg;
  2376. break;
  2377. default:
  2378. bad_usage(argv[0], 0);
  2379. return -1;
  2380. };
  2381. }
  2382. if (optind < argc)
  2383. initial = argv[optind];
  2384. if ((!initial && !current_tp_only) || (initial && current_tp_only) ||
  2385. (spectral_inversion > 2)) {
  2386. bad_usage(argv[0], 0);
  2387. return -1;
  2388. }
  2389. lnb_type.low_val *= 1000; /* convert to kiloherz */
  2390. lnb_type.high_val *= 1000; /* convert to kiloherz */
  2391. lnb_type.switch_val *= 1000; /* convert to kiloherz */
  2392. if (switch_pos >= 4) {
  2393. fprintf (stderr, "switch position needs to be < 4!\n");
  2394. return -1;
  2395. }
  2396. if (initial)
  2397. info("scanning %s\n", initial);
  2398. snprintf (frontend_devname, sizeof(frontend_devname),
  2399. "/dev/dvb/adapter%i/frontend%i", adapter, frontend);
  2400. snprintf (demux_devname, sizeof(demux_devname),
  2401. "/dev/dvb/adapter%i/demux%i", adapter, demux);
  2402. info("using '%s' and '%s'\n", frontend_devname, demux_devname);
  2403. for (i = 0; i < MAX_RUNNING; i++)
  2404. poll_fds[i].fd = -1;
  2405. fe_open_mode = current_tp_only ? O_RDONLY : O_RDWR;
  2406. if ((frontend_fd = open (frontend_devname, fe_open_mode)) < 0)
  2407. fatal("failed to open '%s': %d %m\n", frontend_devname, errno);
  2408. /* determine FE type and caps */
  2409. if (ioctl(frontend_fd, FE_GET_INFO, &fe_info) == -1)
  2410. fatal("FE_GET_INFO failed: %d %m\n", errno);
  2411. if ((spectral_inversion == INVERSION_AUTO ) &&
  2412. !(fe_info.caps & FE_CAN_INVERSION_AUTO)) {
  2413. info("Frontend can not do INVERSION_AUTO, trying INVERSION_OFF instead\n");
  2414. spectral_inversion = INVERSION_OFF;
  2415. }
  2416. signal(SIGINT, handle_sigint);
  2417. if (current_tp_only) {
  2418. current_tp = alloc_transponder(0); /* dummy */
  2419. /* move TP from "new" to "scanned" list */
  2420. list_del_init(&current_tp->list);
  2421. list_add_tail(&current_tp->list, &scanned_transponders);
  2422. current_tp->scan_done = 1;
  2423. scan_tp ();
  2424. }
  2425. else
  2426. scan_network (frontend_fd, initial);
  2427. close (frontend_fd);
  2428. dump_lists ();
  2429. return 0;
  2430. }
  2431. static void dump_dvb_parameters (FILE *f, struct transponder *t)
  2432. {
  2433. switch (output_format) {
  2434. case OUTPUT_PIDS:
  2435. case OUTPUT_VDR:
  2436. vdr_dump_dvb_parameters(f, t->type, &t->param,
  2437. sat_polarisation (t), t->orbital_pos, t->we_flag);
  2438. break;
  2439. case OUTPUT_ZAP:
  2440. zap_dump_dvb_parameters (f, t->type, &t->param,
  2441. sat_polarisation (t), sat_number (t));
  2442. break;
  2443. default:
  2444. break;
  2445. }
  2446. }