1 /*****************************************************************************
2 * dvb.c: LinuxTV channels list
3 *****************************************************************************
4 * Copyright (C) 2005-20009 VLC authors and VideoLAN
7 * Authors: Gildas Bazin <gbazin@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
35 #include <vlc_common.h>
36 #include <vlc_demux.h>
37 #include <vlc_charset.h>
41 static int Demux(demux_t *);
42 static input_item_t *ParseLine(char *line);
44 /** Detect dvb-utils zap channels.conf format */
45 int Import_DVB(vlc_object_t *obj)
47 demux_t *demux = (demux_t *)obj;
49 if (!demux_IsPathExtension(demux, ".conf" ) && !demux->b_force )
52 /* Check if this really is a channels file */
54 int len = stream_Peek(demux->s, &peek, 1023);
58 const uint8_t *eol = memchr(peek, '\n', len);
64 memcpy(line, peek, len);
67 input_item_t *item = ParseLine(line);
72 msg_Dbg(demux, "found valid channels.conf file");
73 demux->pf_control = Control;
74 demux->pf_demux = Demux;
79 /** Parses the whole channels.conf file */
80 static int Demux(demux_t *demux)
82 input_item_t *input = GetCurrentItem(demux);
83 input_item_node_t *subitems = input_item_node_Create(input);
86 while ((line = stream_ReadLine(demux->s)) != NULL)
88 input_item_t *item = ParseLine(line);
92 input_item_node_AppendItem(subitems, item);
96 input_item_node_PostAndDelete(subitems);
99 return 0; /* Needed for correct operation of go back */
102 static int cmp(const void *k, const void *e)
107 static const char *ParseFEC(const char *str)
109 static const struct fec
114 { "1_2", "1/2" }, { "2_3", "2/3" }, { "3_4", "3/4" },
115 { "4_5", "4/5" }, { "5_6", "5/6" }, { "6_7", "6/7" },
116 { "7_8", "7/8" }, { "8_9", "8/9" }, { "9_10", "9/10" },
117 { "AUTO", "" }, { "NONE", "0" }
120 if (strncmp(str, "FEC_", 4))
124 const struct fec *f = bsearch(str, tab, sizeof (tab) / sizeof(tab[0]),
125 sizeof (tab[0]), cmp);
126 return (f != NULL) ? f->vlc : NULL;
129 static const char *ParseModulation(const char *str)
131 static const struct mod
136 { "APSK_16", "16APSK" }, { "APSK_32", "32APSK" },
137 { "DQPSK", "DQPSK" }, { "PSK_8", "8PSK" }, { "QPSK", "QPSK" },
138 { "QAM_128", "128QAM" }, { "QAM_16", "16QAM" },
139 { "QAM_256", "256QAM" }, { "QAM_32", "32QAM" },
140 { "QAM_64", "64QAM" }, { "QAM_AUTO", "QAM" },
141 { "VSB_16", "16VSB" }, { "VSB_8", "8VSB" }
144 const struct mod *m = bsearch(str, tab, sizeof (tab) / sizeof(tab[0]),
145 sizeof (tab[0]), cmp);
146 return (m != NULL) ? m->vlc : NULL;
149 static const char *ParseGuard(const char *str)
151 static const struct guard
156 { "19_128", "19/128" }, { "19_256", "19/256" }, { "1_128", "1/128" },
157 { "1_16", "1/16" }, { "1_32", "1/32" }, { "1_4", "1/4" },
158 { "1_8", "1/8" }, { "AUTO", "" },
161 if (strncmp(str, "GUARD_INTERVAL_", 15))
165 const struct guard *g = bsearch(str, tab, sizeof (tab) / sizeof(tab[0]),
166 sizeof (tab[0]), cmp);
167 return (g != NULL) ? g->vlc : NULL;
170 /* http://www.linuxtv.org/vdrwiki/index.php/Syntax_of_channels.conf or not...
171 * Read the dvb-apps source code for reference. */
172 static input_item_t *ParseLine(char *line)
176 line += strspn(line, " \t\r"); /* skip leading white spaces */
178 return NULL; /* skip comments */
180 /* Extract channel cute name */
181 char *name = strsep(&line, ":");
182 assert(name != NULL);
185 /* Extract central frequency */
186 str = strsep(&line, ":");
189 unsigned long freq = strtoul(str, &end, 10);
193 /* Extract tuning parameters */
194 str = strsep(&line, ":");
200 if (!strcmp(str, "h") || !strcmp(str, "v"))
202 char polarization = toupper(*str);
205 str = strsep(&line, ":");
210 str = strsep(&line, ":");
214 unsigned long rate = strtoul(str, &end, 10);
215 if (*end || rate > (ULONG_MAX / 1000u))
220 "dvb-s://frequency=%"PRIu64":polarization=%c:srate=%lu",
221 freq * UINT64_C(1000000), polarization, rate) == -1)
225 if (!strncmp(str, "INVERSION_", 10))
226 { /* DVB-C or DVB-T */
230 if (strcmp(str, "AUTO"))
232 else if (strcmp(str, "OFF"))
234 else if (strcmp(str, "ON"))
239 str = strsep(&line, ":");
243 if (strncmp(str, "BANDWIDTH_", 10))
245 unsigned long rate = strtoul(str, &end, 10);
249 str = strsep(&line, ":");
250 const char *fec = ParseFEC(str);
251 str = strsep(&line, ":");
252 const char *mod = ParseModulation(str);
253 if (fec == NULL || mod == NULL)
256 if (asprintf(&mrl, "dvb-c://frequency=%lu:inversion:%d:srate=%lu:"
257 "fec=%s:modulation=%s", freq, inversion, rate, fec,
263 unsigned bandwidth = atoi(str + 10);
265 str = strsep(&line, ":");
266 const char *hp = ParseFEC(str);
267 str = strsep(&line, ":");
268 const char *lp = ParseFEC(str);
269 str = strsep(&line, ":");
270 const char *mod = ParseModulation(str);
271 if (hp == NULL || lp == NULL || mod == NULL)
274 str = strsep(&line, ":");
275 if (str == NULL || strncmp(str, "TRANSMISSION_MODE_", 18))
277 int xmit = atoi(str);
279 xmit = -1; /* AUTO */
281 str = strsep(&line, ":");
282 const char *guard = ParseGuard(str);
286 str = strsep(&line, ":");
287 if (str == NULL || strncmp(str, "HIERARCHY_", 10))
290 int hierarchy = atoi(str);
291 if (!strcmp(str, "AUTO"))
294 if (asprintf(&mrl, "dvb-t://frequency=%lu:inversion=%d:"
295 "bandwidth=%u:code-rate-hp=%s:code-rate-lp=%s:"
296 "modulation=%s:transmission=%d:guard=%s:"
297 "hierarchy=%d", freq, inversion, bandwidth, hp, lp,
298 mod, xmit, guard, hierarchy) == -1)
304 const char *mod = ParseModulation(str);
308 if (asprintf(&mrl, "atsc://frequency=%lu:modulation=%s", freq,
313 if (unlikely(mrl == NULL))
316 /* Video PID (TODO? set video track) */
318 /* Audio PID (TODO? set audio track) */
321 str = strsep(&line, ":");
327 unsigned long sid = strtoul(str, &end, 10);
328 if (*end || sid > 65535)
334 char sid_opt[sizeof("program=65535")];
335 snprintf(sid_opt, sizeof(sid_opt), "program=%lu", sid);
337 const char *opts[] = { sid_opt };
339 input_item_t *item = input_item_NewWithType(mrl, name, 1, opts, 0, -1,