dvb.c 9.77 KB
Newer Older
1
/*****************************************************************************
2
 * dvb.c: LinuxTV channels list
3
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL    
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2005-20009 VLC authors and VideoLAN
5
6
7
8
 * $Id$
 *
 * Authors: Gildas Bazin <gbazin@videolan.org>
 *
Jean-Baptiste Kempf's avatar
LGPL    
Jean-Baptiste Kempf committed
9
10
11
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
12
13
14
15
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
LGPL    
Jean-Baptiste Kempf committed
16
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
18
 *
Jean-Baptiste Kempf's avatar
LGPL    
Jean-Baptiste Kempf committed
19
20
21
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22
23
 *****************************************************************************/

24
25
26
27
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

28
29
30
31
32
33
34
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
#include <assert.h>

35
#include <vlc_common.h>
zorglub's avatar
zorglub committed
36
37
#include <vlc_demux.h>
#include <vlc_charset.h>
38
39
40

#include "playlist.h"

41
42
static int Demux(demux_t *);
static input_item_t *ParseLine(char *line);
43

44
/** Detect dvb-utils zap channels.conf format */
45
int Import_DVB(vlc_object_t *p_this)
46
{
47
    demux_t *demux = (demux_t *)p_this;
48

49
    CHECK_FILE();
50
    if (!demux_IsPathExtension(demux, ".conf" ) && !demux->b_force )
51
        return VLC_EGENERIC;
52
53

    /* Check if this really is a channels file */
54
55
56
57
    const uint8_t *peek;
    int len = stream_Peek(demux->s, &peek, 1023);
    if (len <= 0)
        return VLC_EGENERIC;
58

59
60
61
62
    const uint8_t *eol = memchr(peek, '\n', len);
    if (eol == NULL)
        return VLC_EGENERIC;
    len = eol - peek;
63

64
65
66
    char line[len + 1];
    memcpy(line, peek, len);
    line[len] = '\0';
67

68
69
70
71
    input_item_t *item = ParseLine(line);
    if (item == NULL)
        return VLC_EGENERIC;
    vlc_gc_decref(item);
72

73
74
75
    msg_Dbg(demux, "found valid channels.conf file");
    demux->pf_control = Control;
    demux->pf_demux = Demux;
76
77
78
79

    return VLC_SUCCESS;
}

80
81
/** Parses the whole channels.conf file */
static int Demux(demux_t *demux)
82
{
83
84
85
    input_item_t *input = GetCurrentItem(demux);
    input_item_node_t *subitems = input_item_node_Create(input);
    char *line;
86

87
    while ((line = stream_ReadLine(demux->s)) != NULL)
88
    {
89
        input_item_t *item = ParseLine(line);
François Cartegnie's avatar
François Cartegnie committed
90
        free(line);
91
        if (item == NULL)
92
            continue;
93

94
95
        input_item_node_AppendItem(subitems, item);
        vlc_gc_decref(item);
96
97
    }

98
99
    input_item_node_PostAndDelete(subitems);
    vlc_gc_decref(input);
100

101
    return 0; /* Needed for correct operation of go back */
102
103
}

104
static int cmp(const void *k, const void *e)
105
{
106
107
    return strcmp(k, e);
}
108

109
static const char *ParseFEC(const char *str)
110
{
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
     static const struct fec
     {
         char dvb[5];
         char vlc[5];
     } tab[] = {
         { "1_2", "1/2" }, { "2_3", "2/3" }, { "3_4", "3/4" },
         { "4_5", "4/5" }, { "5_6", "5/6" }, { "6_7", "6/7" },
         { "7_8", "7/8" }, { "8_9", "8/9" }, { "9_10", "9/10" },
         { "AUTO", "" },   { "NONE", "0" }
     };

     if (strncmp(str, "FEC_", 4))
         return NULL;
     str += 4;

     const struct fec *f = bsearch(str, tab, sizeof (tab) / sizeof(tab[0]),
                                   sizeof (tab[0]), cmp);
     return (f != NULL) ? f->vlc : NULL;
}
130

131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
static const char *ParseModulation(const char *str)
{
     static const struct mod
     {
         char dvb[9];
         char vlc[7];
     } tab[] = {
         { "APSK_16", "16APSK" }, { "APSK_32", "32APSK" },
         { "DQPSK", "DQPSK" }, { "PSK_8", "8PSK" }, { "QPSK", "QPSK" },
         { "QAM_128", "128QAM" }, { "QAM_16", "16QAM" },
         { "QAM_256", "256QAM" }, { "QAM_32", "32QAM" },
         { "QAM_64", "64QAM" }, { "QAM_AUTO", "QAM" },
         { "VSB_16", "16VSB" }, { "VSB_8", "8VSB" }
     };

     const struct mod *m = bsearch(str, tab, sizeof (tab) / sizeof(tab[0]),
                                   sizeof (tab[0]), cmp);
     return (m != NULL) ? m->vlc : NULL;
}
150

151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
static const char *ParseGuard(const char *str)
{
     static const struct guard
     {
         char dvb[7];
         char vlc[7];
     } tab[] = {
         { "19_128", "19/128" }, { "19_256", "19/256" }, { "1_128", "1/128" },
         { "1_16", "1/16" }, { "1_32", "1/32" }, { "1_4", "1/4" },
         { "1_8", "1/8" }, { "AUTO", "" },
     };

     if (strncmp(str, "GUARD_INTERVAL_", 15))
         return NULL;
     str += 15;

     const struct guard *g = bsearch(str, tab, sizeof (tab) / sizeof(tab[0]),
                                     sizeof (tab[0]), cmp);
     return (g != NULL) ? g->vlc : NULL;
}
171

172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/* http://www.linuxtv.org/vdrwiki/index.php/Syntax_of_channels.conf or not...
 * Read the dvb-apps source code for reference. */
static input_item_t *ParseLine(char *line)
{
    char *str, *end;

    line += strspn(line, " \t\r"); /* skip leading white spaces */
    if (*line == '#')
        return NULL; /* skip comments */

    /* Extract channel cute name */
    char *name = strsep(&line, ":");
    assert(name != NULL);
    EnsureUTF8(name);

    /* Extract central frequency */
    str = strsep(&line, ":");
    if (str == NULL)
        return NULL;
    unsigned long freq = strtoul(str, &end, 10);
    if (*end)
        return NULL;

    /* Extract tuning parameters */
    str = strsep(&line, ":");
    if (str == NULL)
        return NULL;

    char *mrl;

    if (!strcmp(str, "h") || !strcmp(str, "v"))
    {   /* DVB-S */
        char polarization = toupper(*str);

        /* TODO: sat no. */
        str = strsep(&line, ":");
        if (str == NULL)
            return NULL;

        /* baud rate */
        str = strsep(&line, ":");
        if (str == NULL)
            return NULL;

        unsigned long rate = strtoul(str, &end, 10);
        if (*end || rate > (ULONG_MAX / 1000u))
            return NULL;
        rate *= 1000;

        if (asprintf(&mrl,
                     "dvb-s://frequency=%"PRIu64":polarization=%c:srate=%lu",
                     freq * UINT64_C(1000000), polarization, rate) == -1)
            mrl = NULL;
    }
    else
    if (!strncmp(str, "INVERSION_", 10))
    {   /* DVB-C or DVB-T */
        int inversion;

        str += 10;
        if (strcmp(str, "AUTO"))
            inversion = -1;
        else if (strcmp(str, "OFF"))
            inversion = 0;
        else if (strcmp(str, "ON"))
            inversion = 1;
238
        else
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
            return NULL;

        str = strsep(&line, ":");
        if (str == NULL)
            return NULL;

        if (strncmp(str, "BANDWIDTH_", 10))
        {   /* DVB-C */
            unsigned long rate = strtoul(str, &end, 10);
            if (*end)
                return NULL;

            str = strsep(&line, ":");
            const char *fec = ParseFEC(str);
            str = strsep(&line, ":");
            const char *mod = ParseModulation(str);
            if (fec == NULL || mod == NULL)
                return NULL;

            if (asprintf(&mrl, "dvb-c://frequency=%lu:inversion:%d:srate=%lu:"
                         "fec=%s:modulation=%s", freq, inversion, rate, fec,
                         mod) == -1)
                mrl = NULL;
262
        }
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
        else
        {   /* DVB-T */
            unsigned bandwidth = atoi(str + 10);

            str = strsep(&line, ":");
            const char *hp = ParseFEC(str);
            str = strsep(&line, ":");
            const char *lp = ParseFEC(str);
            str = strsep(&line, ":");
            const char *mod = ParseModulation(str);
            if (hp == NULL || lp == NULL || mod == NULL)
                return NULL;

            str = strsep(&line, ":");
            if (str == NULL || strncmp(str, "TRANSMISSION_MODE_", 18))
                return NULL;
            int xmit = atoi(str);
            if (xmit == 0)
                xmit = -1; /* AUTO */

            str = strsep(&line, ":");
            const char *guard = ParseGuard(str);
            if (guard == NULL)
                return NULL;

            str = strsep(&line, ":");
            if (str == NULL || strncmp(str, "HIERARCHY_", 10))
                return NULL;
            str += 10;
            int hierarchy = atoi(str);
            if (!strcmp(str, "AUTO"))
                hierarchy = -1;

            if (asprintf(&mrl, "dvb-t://frequency=%lu:inversion=%d:"
                         "bandwidth=%u:code-rate-hp=%s:code-rate-lp=%s:"
                         "modulation=%s:transmission=%d:guard=%s:"
                         "hierarchy=%d", freq, inversion, bandwidth, hp, lp,
                         mod, xmit, guard, hierarchy) == -1)
                mrl = NULL;
302
303
        }
    }
304
305
306
307
308
309
310
311
312
    else
    {   /* ATSC */
        const char *mod = ParseModulation(str);
        if (mod == NULL)
            return NULL;

        if (asprintf(&mrl, "atsc://frequency=%lu:modulation=%s", freq,
                     mod) == -1)
            mrl = NULL;
313
314
    }

315
316
    if (unlikely(mrl == NULL))
        return NULL;
317

318
319
320
321
322
323
324
    /* Video PID (TODO? set video track) */
    strsep(&line, ":");
    /* Audio PID (TODO? set audio track) */
    strsep(&line, ":");
    /* Extract SID */
    str = strsep(&line, ":");
    if (str == NULL)
325
    {
326
327
        free(mrl);
        return NULL;
328
    }
329
330
    unsigned long sid = strtoul(str, &end, 10);
    if (*end || sid > 65535)
331
    {
332
333
        free(mrl);
        return NULL;
334
    }
335

336
337
338
339
340
341
342
343
344
    char sid_opt[sizeof("program=65535")];
    snprintf(sid_opt, sizeof(sid_opt), "program=%lu", sid);

    const char *opts[] = { sid_opt };

    input_item_t *item = input_item_NewWithType(mrl, name, 1, opts, 0, -1,
                                                ITEM_TYPE_CARD);
    free(mrl);
    return item;
345
}