]> git.sesse.net Git - decode_sdt/blob - decode_sdt.c
Support parsing of LCN (Logical Channel Number) lists.
[decode_sdt] / decode_sdt.c
1 /*
2  * Decode SDT and NIT from a TS stream (for instance dvr0), and output in a
3  * relatively machine-readable format.
4  *
5  * Based on the decode_sdt.c example from libdvbpsi, and uses some source code
6  * from MuMuDVB.
7  *
8  * Copyright (C):
9  *    2001-2011 VideoLAN
10  *    Dave Chapman <dave@dchapman.com> 2001, 2002.
11  *    2004-2011 Brice DUBOST 
12  *    2013 Steinar H. Gunderson
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27  */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdbool.h>
33 #include <ctype.h>
34 #include <unistd.h>
35 #include <fcntl.h>
36 #include <iconv.h>
37
38 #include <stdint.h>
39
40 #include <dvbpsi/dvbpsi.h>
41 #include <dvbpsi/psi.h>
42 #include <dvbpsi/demux.h>
43 #include <dvbpsi/descriptor.h>
44 #include <dvbpsi/sdt.h>
45 #include <dvbpsi/nit.h>
46
47 static int packet_num = 0;
48 static int last_used_packet_num = 0;
49
50 /**
51 @brief The different encodings that can be used
52 Cf EN 300 468 Annex A (I used v1.9.1)
53  */
54 static const char *encodings_en300468[] ={
55         "ISO8859-1",
56         "ISO8859-2",
57         "ISO8859-3",
58         "ISO8859-4",
59         "ISO8859-5",
60         "ISO8859-6",
61         "ISO8859-7",
62         "ISO8859-8",
63         "ISO8859-9",
64         "ISO8859-10",
65         "ISO8859-11",
66         "ISO8859-12",
67         "ISO8859-13",
68         "ISO8859-14",
69         "ISO8859-15",
70         "ISO-10646", //control char 0x11
71         "GB2312",    //control char 0x13
72         "BIG5",      //control char 0x14
73         "ISO-10646/UTF8",      //control char 0x15
74 };
75
76 // Convert text according to EN 300 468 annex A.
77 static int convert_en300468_string(char *string, int max_len)
78 {
79         int encoding_control_char = 8; //cf encodings_en300468
80         char *dest;
81         char *tempdest, *tempbuf;
82         unsigned char *src;
83         /* remove control characters and convert to UTF-8 the channel name */
84         //If no channel encoding is specified, it seems that most of the broadcasters
85         //uses ISO/IEC 8859-9. But the norm (EN 300 468) said that it should be Latin-1 (ISO/IEC 6937 + euro)
86
87         //temporary buffers allocation
88         tempdest = tempbuf = (char *)malloc(sizeof(char)*2*strlen(string));
89         if (tempdest==NULL) {
90                 return -1;
91         }
92
93         int len = 0;
94         for (src = (unsigned char *)string; *src; src++) {
95                 if (*src >= 0x20 && (*src < 0x80 || *src > 0x9f)) {
96                         //We copy non-control characters
97                         *tempdest++ = *src;
98                         len++;
99                 } else if (*src <= 0x20) {
100                         //control character recognition based on EN 300 468 v1.9.1 Annex A
101                         if (*src <= 0x0b) {
102                                 encoding_control_char=(int) *src+4-1;
103                         } else if(*src == 0x10) { //ISO/IEC 8859 : See table A.4
104                                 src++;//we skip the current byte
105                                 src++;//This one is always set to 0
106                                 if (*src >= 0x01 && *src <=0x0f)
107                                         encoding_control_char=(int) *src-1;
108                         }
109                         else if (*src==0x11) // ISO/IEC 10646 : Basic Multilingual Plane
110                                 encoding_control_char = 15;
111                         else if (*src==0x13) // GB-2312-1980 : Simplified Chinese Character
112                                 encoding_control_char = 16;
113                         else if (*src==0x14) // Big5 subset of ISO/IEC 10646 : Traditional Chinese
114                                 encoding_control_char = 17;
115                         else if (*src==0x15) // UTF-8 encoding of ISO/IEC 10646 : Basic Multilingual Plane
116                                 encoding_control_char = 18;
117                 } 
118         }
119         //Conversion to utf8
120         iconv_t cd;
121         //we open the conversion table
122         cd = iconv_open("UTF8", encodings_en300468[encoding_control_char]);
123
124         size_t inSize, outSize = max_len;
125         inSize = len;
126         //pointers initialisation because iconv change them, we store
127         dest = string;
128         tempdest = tempbuf;
129         //conversion
130         iconv(cd, &tempdest, &inSize, &dest, &outSize);
131         *dest = '\0';
132         free(tempbuf);
133         iconv_close(cd);
134
135         return encoding_control_char;
136 }
137
138
139 static bool ReadPacket(int i_fd, uint8_t * p_dst)
140 {
141         static uint8_t buf[188 * 100];
142         static int in_buf_start = 0;
143         static int in_buf_end = 0;
144         
145 has_stuff:
146         // Skip to the first 0x47 byte.
147         while (in_buf_end > in_buf_start && buf[in_buf_start] != 0x47) {
148                 ++in_buf_start;
149         }
150
151         if (in_buf_end - in_buf_start >= 188) {
152                 memcpy(p_dst, buf + in_buf_start, 188);
153                 in_buf_start += 188;
154                 return true;
155         }
156
157         // Check if there's a partial packet in the buffer.
158         int partial_len = in_buf_end - in_buf_start;
159         if (partial_len == 0) {
160                 in_buf_start = in_buf_end = 0;
161         } else if (partial_len > 0) {
162                 memmove(buf, buf + in_buf_start, partial_len);
163                 in_buf_start = 0;
164                 in_buf_end = partial_len;
165         }
166
167         int ret = read(i_fd, buf + in_buf_start, sizeof(buf) - in_buf_end);
168         if (ret == -1) {
169                 perror("read");
170                 return false;
171         }
172         if (ret == 0) {
173                 return false;
174         }
175
176         in_buf_end += ret;
177
178         goto has_stuff;
179 }
180
181
182 static void ParseSDT(void *p_zero, dvbpsi_sdt_t * p_sdt)
183 {
184         last_used_packet_num = packet_num;
185         dvbpsi_sdt_service_t *p_service = p_sdt->p_first_service;
186         for (dvbpsi_sdt_service_t * p_service = p_sdt->p_first_service;
187              p_service != NULL;
188              p_service = p_service->p_next) {
189                 int service_type = -1;
190                 char provider_name[256] = { 0 };
191                 char channel_name[256] = { 0 };
192                 dvbpsi_descriptor_t *p_descriptor =
193                     p_service->p_first_descriptor;
194
195                 // Search for a Service Descriptor (0x48), which contains the provider and channel names.
196                 for (dvbpsi_descriptor_t * p_descriptor = p_service->p_first_descriptor;
197                      p_descriptor != NULL;
198                      p_descriptor = p_descriptor->p_next) {
199                         uint8_t *ptr;
200                         int i, len;
201                         if (p_descriptor->i_tag != 0x48) {
202                                 continue;
203                         }
204                         service_type = p_descriptor->p_data[0];
205                         ptr = p_descriptor->p_data + 1;
206
207                         len = *ptr++;
208                         memcpy(provider_name, ptr, len);
209                         provider_name[len] = 0;
210                         ptr += len;
211
212                         len = *ptr++;
213                         memcpy(channel_name, ptr, len);
214                         channel_name[len] = 0;
215
216                         convert_en300468_string(provider_name, sizeof(provider_name));
217                         convert_en300468_string(channel_name, sizeof(channel_name));
218                 }
219
220                 // Show television types only.
221                 if (service_type == 0x01 || service_type == 0x11 || service_type == 0x16 ||
222                     service_type == 0x19 || service_type == 0x1c) {
223                         printf("sid %d/%d: ts_id=%d, provider_name=\"%s\", channel_name=\"%s\"\n",
224                              p_sdt->i_network_id, p_service->i_service_id,
225                              p_sdt->i_extension, provider_name,
226                              channel_name);
227                 }
228         }
229         dvbpsi_sdt_delete(p_sdt);
230 }
231
232 // little endian assumed
233 typedef struct {
234         uint8_t frequency_4;
235         uint8_t frequency_3;
236         uint8_t frequency_2;
237         uint8_t frequency_1;
238         uint16_t orbital_position;      // big-endian
239         uint8_t modulation_type : 2;
240         uint8_t modulation_system : 1;
241         uint8_t roll_off : 2;
242         uint8_t polarization : 2;
243         uint8_t west_east_flag : 1;
244         uint8_t symbol_rate_12;
245         uint8_t symbol_rate_34;
246         uint8_t symbol_rate_56;
247         uint8_t FEC_inner : 4;
248         uint8_t symbol_rate_7 : 4;
249 } descr_sat_delivery_t;
250
251 // Canal Digital stores their LCN in the NIT (Wikipedia says some do it in
252 // the BAT). I haven't been able to find any format for this, even in NorDIG's
253 // specification, so this is done by visual inspection.
254 static void ParseLCN(int network_id, const dvbpsi_descriptor_t *p_descriptor)
255 {
256         uint8_t *ptr = p_descriptor->p_data;
257         int channel_list_id = *ptr++;
258
259         // Skip the name and the three-letter country code.
260         int len = *ptr++;
261         ptr += len + 3;
262
263         // The LCN itself.
264         len = *ptr++;
265         for (int i = 0; i < len; i += 4) {
266                 int sid = (ptr[i] << 8) | ptr[i + 1];
267                 int lcn = ((ptr[i + 2] << 8) | ptr[i + 3]) & 0x3fff;
268                 printf("lcn %d/%d/%d: lcn=%d\n", network_id, channel_list_id, sid, lcn);
269         }
270 }
271
272 static void ParseDSD(const dvbpsi_nit_t *p_nit, const dvbpsi_nit_ts_t *p_ts, const dvbpsi_descriptor_t *p_descriptor)
273 {
274         char buf[256], *ptr;
275         int i, len;
276         descr_sat_delivery_t *d =
277                 (descr_sat_delivery_t *) p_descriptor->p_data;
278         printf("ts %d/%d: ", p_nit->i_network_id, p_ts->i_ts_id);
279         printf("freq=%x%02x%02x, ", d->frequency_4,
280                         d->frequency_3, d->frequency_2,
281                         d->frequency_1);
282         printf("pol=%c, ", "HVLR"[d->polarization]);
283         if (d->modulation_system) {
284                 printf("delivery_system=DVBS2, ");
285         } else {
286                 printf("delivery_system=DVBS, ");
287         }
288         if (d->modulation_type == 0) {
289                 printf("modulation=QAMAUTO, ");
290         } else if (d->modulation_type == 1) {
291                 printf("modulation=QPSK, ");
292         } else if (d->modulation_type == 2) {
293                 printf("modulation=8PSK, ");
294         } else if (d->modulation_type == 3) {
295                 printf("modulation=QAM64, ");
296         }
297         printf("srate=%x%02x%02x, ", d->symbol_rate_12, d->symbol_rate_34, d->symbol_rate_56);
298         if (d->FEC_inner == 0) {
299                 printf("coderate=auto");
300         } else if (d->FEC_inner == 1) {
301                 printf("coderate=1/2");
302         } else if (d->FEC_inner == 2) {
303                 printf("coderate=2/3");
304         } else if (d->FEC_inner == 3) {
305                 printf("coderate=3/4");
306         } else if (d->FEC_inner == 4) {
307                 printf("coderate=5/6");
308         } else if (d->FEC_inner == 5) {
309                 printf("coderate=7/8");
310         } else if (d->FEC_inner == 6) {
311                 printf("coderate=8/9");
312         } else if (d->FEC_inner == 7) {
313                 printf("coderate=3/5");
314         } else if (d->FEC_inner == 8) {
315                 printf("coderate=4/5");
316         } else if (d->FEC_inner == 9) {
317                 printf("coderate=9/10");
318         } else {
319                 printf("coderate=none");
320         }
321         printf("\n");
322 }
323
324 static void ParseNIT(void *p_zero, dvbpsi_nit_t * p_nit)
325 {
326         last_used_packet_num = packet_num;
327         for (dvbpsi_nit_ts_t *p_ts = p_nit->p_first_ts;
328              p_ts != NULL;
329              p_ts = p_ts->p_next) {
330                 // Search through the descriptors until we find one of type 0x43
331                 // (Delivery System Descriptor).
332                 for (dvbpsi_descriptor_t *p_descriptor = p_ts->p_first_descriptor;
333                      p_descriptor != NULL;
334                      p_descriptor = p_descriptor->p_next) {
335                         if (p_descriptor->i_tag == 0x87) {
336                                 ParseLCN(p_nit->i_network_id, p_descriptor);
337                         }
338                         if (p_descriptor->i_tag == 0x43) {
339                                 ParseDSD(p_nit, p_ts, p_descriptor);
340                         }
341                 }
342         }
343         dvbpsi_nit_delete(p_nit);
344 }
345
346 /*****************************************************************************
347  * DVBPSI messaging callback
348  *****************************************************************************/
349 static void message(dvbpsi_t * handle, const dvbpsi_msg_level_t level,
350                     const char *msg)
351 {
352         switch (level) {
353         case DVBPSI_MSG_ERROR:
354                 fprintf(stderr, "Error: ");
355                 break;
356         case DVBPSI_MSG_WARN:
357                 fprintf(stderr, "Warning: ");
358                 break;
359         case DVBPSI_MSG_DEBUG:
360                 fprintf(stderr, "Debug: ");
361                 break;
362         default:                /* do nothing */
363                 return;
364         }
365         fprintf(stderr, "%s\n", msg);
366 }
367
368 /*****************************************************************************
369  * NewSubtable
370  *****************************************************************************/
371 static void NewSubtableNIT(dvbpsi_t * p_dvbpsi, uint8_t i_table_id,
372                            uint16_t i_extension, void *p_zero)
373 {
374         if (i_table_id == 0x40) {
375                 if (!dvbpsi_nit_attach(p_dvbpsi, i_table_id, i_extension, ParseNIT, NULL))
376                         fprintf(stderr, "Failed to attach PSI subdecoder\n");
377         }
378 }
379
380 /*****************************************************************************
381  * NewSubtable
382  *****************************************************************************/
383 static void NewSubtableSDT(dvbpsi_t * p_dvbpsi, uint8_t i_table_id,
384                            uint16_t i_extension, void *p_zero)
385 {
386         if (i_table_id == 0x42 || i_table_id == 0x46) {
387                 if (!dvbpsi_sdt_attach(p_dvbpsi, i_table_id, i_extension, ParseSDT, NULL))
388                         fprintf(stderr, "Failed to attach SDT subdecoder\n");
389         }
390 }
391
392 /*****************************************************************************
393  * main
394  *****************************************************************************/
395 int main(int i_argc, char *pa_argv[])
396 {
397         int i_fd;
398         uint8_t data[188];
399         dvbpsi_t *p_dvbpsi_sdt, *p_dvbpsi_nit;
400         bool b_ok;
401
402         if (i_argc != 2)
403                 return 1;
404
405         i_fd = open(pa_argv[1], 0);
406         if (i_fd < 0)
407                 return 1;
408
409         p_dvbpsi_sdt = dvbpsi_new(&message, DVBPSI_MSG_ERROR);
410         if (p_dvbpsi_sdt == NULL)
411                 goto out;
412
413         p_dvbpsi_nit = dvbpsi_new(&message, DVBPSI_MSG_ERROR);
414         if (p_dvbpsi_nit == NULL)
415                 goto out;
416
417         if (!dvbpsi_AttachDemux(p_dvbpsi_sdt, NewSubtableSDT, NULL))
418                 goto out;
419
420         if (!dvbpsi_AttachDemux(p_dvbpsi_nit, NewSubtableNIT, NULL))
421                 goto out;
422
423         while (ReadPacket(i_fd, data)) {
424                 uint16_t i_pid = ((uint16_t)(data[1] & 0x1f) << 8) + data[2];
425                 if (i_pid == 0x10)
426                         dvbpsi_packet_push(p_dvbpsi_nit, data);
427                 if (i_pid == 0x11)
428                         dvbpsi_packet_push(p_dvbpsi_sdt, data);
429
430                 // After 100k packets with no new information, we assume we are done.
431                 ++packet_num;
432                 if ((packet_num - last_used_packet_num) >= 100000)
433                         break;
434         }
435
436 out:
437         if (p_dvbpsi_sdt) {
438                 dvbpsi_DetachDemux(p_dvbpsi_sdt);
439                 dvbpsi_delete(p_dvbpsi_sdt);
440         }
441         if (p_dvbpsi_nit) {
442                 dvbpsi_DetachDemux(p_dvbpsi_nit);
443                 dvbpsi_delete(p_dvbpsi_nit);
444         }
445         close(i_fd);
446         return 0;
447 }