stream: replace input_thread_t by input_item_t
[vlc.git] / modules / demux / playlist / dvb.c
blob5d5ea507c49d73720b7bea63a6d10c9e0211cb75
1 /*****************************************************************************
2 * dvb.c: LinuxTV channels list
3 *****************************************************************************
4 * Copyright (C) 2005-2012 VLC authors and VideoLAN
5 * $Id$
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 *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <ctype.h>
32 #include <limits.h>
33 #include <assert.h>
35 #include <vlc_common.h>
36 #include <vlc_access.h>
37 #include <vlc_charset.h>
39 #include "playlist.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;
49 CHECK_FILE(demux);
50 if (!stream_HasExtension(demux, ".conf" ) && !demux->obj.force )
51 return VLC_EGENERIC;
53 /* Check if this really is a channels file */
54 const uint8_t *peek;
55 int len = vlc_stream_Peek(demux->s, &peek, 1023);
56 if (len <= 0)
57 return VLC_EGENERIC;
59 const uint8_t *eol = memchr(peek, '\n', len);
60 if (eol == NULL)
61 return VLC_EGENERIC;
62 len = eol - peek;
64 char line[len + 1];
65 memcpy(line, peek, len);
66 line[len] = '\0';
68 input_item_t *item = ParseLine(line);
69 if (item == NULL)
70 return VLC_EGENERIC;
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;
77 return VLC_SUCCESS;
80 /** Parses the whole channels.conf file */
81 static int ReadDir(stream_t *s, input_item_node_t *subitems)
83 char *line;
85 while ((line = vlc_stream_ReadLine(s->s)) != NULL)
87 input_item_t *item = ParseLine(line);
88 free(line);
89 if (item == NULL)
90 continue;
92 input_item_node_AppendItem(subitems, item);
93 input_item_Release(item);
96 return VLC_SUCCESS;
99 static int cmp(const void *k, const void *e)
101 return strcmp(k, e);
104 static const char *ParseFEC(const char *str)
106 static const struct fec
108 char dvb[5];
109 char vlc[5];
110 } tab[] = {
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))
118 return NULL;
119 str += 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
130 char dvb[9];
131 char vlc[7];
132 } tab[] = {
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" }
141 if( str == NULL )
142 return NULL;
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
153 char dvb[7];
154 char vlc[7];
155 } tab[] = {
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))
162 return NULL;
163 str += 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)
174 char *str, *end;
176 line += strspn(line, " \t\r"); /* skip leading white spaces */
177 if (*line == '#')
178 return NULL; /* skip comments */
180 /* Extract channel cute name */
181 char *name = strsep(&line, ":");
182 assert(name != NULL);
183 EnsureUTF8(name);
185 /* Extract central frequency */
186 str = strsep(&line, ":");
187 if (str == NULL)
188 return NULL;
189 unsigned long freq = strtoul(str, &end, 10);
190 if (*end)
191 return NULL;
193 /* Extract tuning parameters */
194 str = strsep(&line, ":");
195 if (str == NULL)
196 return NULL;
198 char *mrl;
200 if (!strcmp(str, "h") || !strcmp(str, "v"))
201 { /* DVB-S */
202 char polarization = toupper(*str);
204 /* TODO: sat no. */
205 str = strsep(&line, ":");
206 if (str == NULL)
207 return NULL;
209 /* baud rate */
210 str = strsep(&line, ":");
211 if (str == NULL)
212 return NULL;
214 unsigned long rate = strtoul(str, &end, 10);
215 if (*end || rate > (ULONG_MAX / 1000u))
216 return NULL;
217 rate *= 1000;
219 if (asprintf(&mrl,
220 "dvb-s://frequency=%"PRIu64":polarization=%c:srate=%lu",
221 freq * UINT64_C(1000000), polarization, rate) == -1)
222 mrl = NULL;
224 else
225 if (!strncmp(str, "INVERSION_", 10))
226 { /* DVB-C or DVB-T */
227 int inversion;
229 str += 10;
230 if (strcmp(str, "AUTO"))
231 inversion = -1;
232 else if (strcmp(str, "OFF"))
233 inversion = 0;
234 else if (strcmp(str, "ON"))
235 inversion = 1;
236 else
237 return NULL;
239 str = strsep(&line, ":");
240 if (str == NULL)
241 return NULL;
243 if (strncmp(str, "BANDWIDTH_", 10))
244 { /* DVB-C */
245 unsigned long rate = strtoul(str, &end, 10);
246 if (*end)
247 return NULL;
249 const char *fec = ParseFEC(strsep(&line, ":"));
250 const char *mod = ParseModulation(strsep(&line,":"));
251 if (fec == NULL || mod == NULL)
252 return NULL;
254 if (asprintf(&mrl, "dvb-c://frequency=%lu:inversion:%d:srate=%lu:"
255 "fec=%s:modulation=%s", freq, inversion, rate, fec,
256 mod) == -1)
257 mrl = NULL;
259 else
260 { /* DVB-T */
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)
268 return NULL;
270 str = strsep(&line, ":");
271 if (str == NULL || strncmp(str, "TRANSMISSION_MODE_", 18))
272 return NULL;
273 int xmit = atoi(str);
274 if (xmit == 0)
275 xmit = -1; /* AUTO */
277 const char *guard = ParseGuard(strsep(&line,":"));
278 if (guard == NULL)
279 return NULL;
281 str = strsep(&line, ":");
282 if (str == NULL || strncmp(str, "HIERARCHY_", 10))
283 return NULL;
284 str += 10;
285 int hierarchy = atoi(str);
286 if (!strcmp(str, "AUTO"))
287 hierarchy = -1;
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)
294 mrl = NULL;
297 else
298 { /* ATSC */
299 const char *mod = ParseModulation(str);
300 if (mod == NULL)
301 return NULL;
303 if (asprintf(&mrl, "atsc://frequency=%lu:modulation=%s", freq,
304 mod) == -1)
305 mrl = NULL;
308 if (unlikely(mrl == NULL))
309 return NULL;
311 /* Video PID (TODO? set video track) */
312 strsep(&line, ":");
313 /* Audio PID (TODO? set audio track) */
314 strsep(&line, ":");
315 /* Extract SID */
316 str = strsep(&line, ":");
317 if (str == NULL)
319 free(mrl);
320 return NULL;
322 unsigned long sid = strtoul(str, &end, 10);
323 if (*end || sid > 65535)
325 free(mrl);
326 return NULL;
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);
333 free(mrl);
334 if (item != NULL)
335 input_item_AddOption(item, sid_opt, 0);
336 return item;