1 /*****************************************************************************
2 * dvb.c: LinuxTV channels list
3 *****************************************************************************
4 * Copyright (C) 2005-2012 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_access.h>
37 #include <vlc_charset.h>
41 static int ReadDir(stream_t
*, input_item_node_t
*);
42 static input_item_t
*ParseLine(char *line
);
44 /** Detect dvb-utils zap channels.conf format */
45 int Import_DVB(vlc_object_t
*p_this
)
47 stream_t
*demux
= (stream_t
*)p_this
;
50 if (!stream_HasExtension(demux
, ".conf" ) && !demux
->obj
.force
)
53 /* Check if this really is a channels file */
55 int len
= vlc_stream_Peek(demux
->s
, &peek
, 1023);
59 const uint8_t *eol
= memchr(peek
, '\n', len
);
65 memcpy(line
, peek
, len
);
68 input_item_t
*item
= ParseLine(line
);
71 input_item_Release(item
);
73 msg_Dbg(demux
, "found valid channels.conf file");
74 demux
->pf_control
= access_vaDirectoryControlHelper
;
75 demux
->pf_readdir
= ReadDir
;
80 /** Parses the whole channels.conf file */
81 static int ReadDir(stream_t
*s
, input_item_node_t
*subitems
)
85 while ((line
= vlc_stream_ReadLine(s
->s
)) != NULL
)
87 input_item_t
*item
= ParseLine(line
);
92 input_item_node_AppendItem(subitems
, item
);
93 input_item_Release(item
);
99 static int cmp(const void *k
, const void *e
)
104 static const char *ParseFEC(const char *str
)
106 static const struct fec
111 { "1_2", "1/2" }, { "2_3", "2/3" }, { "3_4", "3/4" },
112 { "4_5", "4/5" }, { "5_6", "5/6" }, { "6_7", "6/7" },
113 { "7_8", "7/8" }, { "8_9", "8/9" }, { "9_10", "9/10" },
114 { "AUTO", "" }, { "NONE", "0" }
117 if (str
== NULL
|| strncmp(str
, "FEC_", 4))
121 const struct fec
*f
= bsearch(str
, tab
, sizeof (tab
) / sizeof(tab
[0]),
122 sizeof (tab
[0]), cmp
);
123 return (f
!= NULL
) ? f
->vlc
: NULL
;
126 static const char *ParseModulation(const char *str
)
128 static const struct mod
133 { "8VSB", "8VSB" }, { "APSK_16", "16APSK" }, { "APSK_32", "32APSK" },
134 { "DQPSK", "DQPSK" }, { "PSK_8", "8PSK" }, { "QPSK", "QPSK" },
135 { "QAM_128", "128QAM" }, { "QAM_16", "16QAM" },
136 { "QAM_256", "256QAM" }, { "QAM_32", "32QAM" },
137 { "QAM_64", "64QAM" }, { "QAM_AUTO", "QAM" },
138 { "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 (str
== NULL
|| 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 const char *fec
= ParseFEC(strsep(&line
, ":"));
250 const char *mod
= ParseModulation(strsep(&line
,":"));
251 if (fec
== NULL
|| mod
== NULL
)
254 if (asprintf(&mrl
, "dvb-c://frequency=%lu:inversion:%d:srate=%lu:"
255 "fec=%s:modulation=%s", freq
, inversion
, rate
, fec
,
261 unsigned bandwidth
= atoi(str
+ 10);
263 const char *hp
= ParseFEC(strsep(&line
, ":"));
264 const char *lp
= ParseFEC(strsep(&line
, ":"));
265 const char *mod
= ParseModulation(strsep(&line
, ":"));
267 if (hp
== NULL
|| lp
== NULL
|| mod
== NULL
)
270 str
= strsep(&line
, ":");
271 if (str
== NULL
|| strncmp(str
, "TRANSMISSION_MODE_", 18))
273 int xmit
= atoi(str
);
275 xmit
= -1; /* AUTO */
277 const char *guard
= ParseGuard(strsep(&line
,":"));
281 str
= strsep(&line
, ":");
282 if (str
== NULL
|| strncmp(str
, "HIERARCHY_", 10))
285 int hierarchy
= atoi(str
);
286 if (!strcmp(str
, "AUTO"))
289 if (asprintf(&mrl
, "dvb-t://frequency=%lu:inversion=%d:"
290 "bandwidth=%u:code-rate-hp=%s:code-rate-lp=%s:"
291 "modulation=%s:transmission=%d:guard=%s:"
292 "hierarchy=%d", freq
, inversion
, bandwidth
, hp
, lp
,
293 mod
, xmit
, guard
, hierarchy
) == -1)
299 const char *mod
= ParseModulation(str
);
303 if (asprintf(&mrl
, "atsc://frequency=%lu:modulation=%s", freq
,
308 if (unlikely(mrl
== NULL
))
311 /* Video PID (TODO? set video track) */
313 /* Audio PID (TODO? set audio track) */
316 str
= strsep(&line
, ":");
322 unsigned long sid
= strtoul(str
, &end
, 10);
323 if (*end
|| sid
> 65535)
329 char sid_opt
[sizeof("program=65535")];
330 snprintf(sid_opt
, sizeof(sid_opt
), "program=%lu", sid
);
332 input_item_t
*item
= input_item_NewCard(mrl
, name
);
335 input_item_AddOption(item
, sid_opt
, 0);