dvb_print_si.c 39.6 KB
Newer Older
1 2 3
/*****************************************************************************
 * dvb_print_si.c: Prints tables from a TS file
 *****************************************************************************
Christophe Massiot's avatar
Christophe Massiot committed
4
 * Copyright (C) 2010-2015 VideoLAN
5 6 7
 *
 * Authors: Christophe Massiot <massiot@via.ecp.fr>
 *
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject
 * to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 27 28 29 30
 *****************************************************************************/

#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
31
#include <inttypes.h>
32 33
#include <string.h>
#include <stdio.h>
34
#include <unistd.h>
35
#include <stdarg.h>
36
#include <getopt.h>
37
#include <iconv.h>
38 39 40 41

#include <bitstream/mpeg/ts.h>
#include <bitstream/mpeg/psi.h>
#include <bitstream/dvb/si.h>
42
#include <bitstream/dvb/si_print.h>
43
#include <bitstream/mpeg/psi_print.h>
Christophe Massiot's avatar
Christophe Massiot committed
44 45
#include <bitstream/scte/35.h>
#include <bitstream/scte/35_print.h>
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

/*****************************************************************************
 * Local declarations
 *****************************************************************************/
#define MAX_PIDS    8192
#define READ_ONCE   7

typedef struct ts_pid_t {
    int i_psi_refcount;
    int8_t i_last_cc;

    /* biTStream PSI section gathering */
    uint8_t *p_psi_buffer;
    uint16_t i_psi_buffer_used;
} ts_pid_t;

62 63
static ts_pid_t p_pids[MAX_PIDS];

64 65 66
typedef struct sid_t {
    uint16_t i_sid, i_pmt_pid;
    uint8_t *p_current_pmt;
67
    PSI_TABLE_DECLARE(pp_eit_sections);
68 69 70 71 72
} sid_t;

static sid_t **pp_sids = NULL;
static int i_nb_sids = 0;

73 74 75 76 77 78 79 80 81
typedef struct bouquet_t {
    uint16_t i_bouquet;
    PSI_TABLE_DECLARE(pp_current_bat_sections);
    PSI_TABLE_DECLARE(pp_next_bat_sections);
} bouquet_t;

static bouquet_t **pp_bouquets = NULL;
static int i_nb_bouquets = 0;

82 83
static PSI_TABLE_DECLARE(pp_current_pat_sections);
static PSI_TABLE_DECLARE(pp_next_pat_sections);
84 85
static PSI_TABLE_DECLARE(pp_current_cat_sections);
static PSI_TABLE_DECLARE(pp_next_cat_sections);
86 87
static PSI_TABLE_DECLARE(pp_current_tsdt_sections);
static PSI_TABLE_DECLARE(pp_next_tsdt_sections);
88 89 90 91 92
static PSI_TABLE_DECLARE(pp_current_nit_sections);
static PSI_TABLE_DECLARE(pp_next_nit_sections);
static PSI_TABLE_DECLARE(pp_current_sdt_sections);
static PSI_TABLE_DECLARE(pp_next_sdt_sections);

93 94 95
static const char *psz_native_encoding = "UTF-8";
static const char *psz_current_encoding = "";
static iconv_t iconv_handle = (iconv_t)-1;
96
static print_type_t i_print_type = PRINT_TEXT;
97

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
/* Please keep those two in sync */
enum tables_t {
    TABLE_PAT = 0,
    TABLE_CAT,
    TABLE_TSDT,
    TABLE_NIT,
    TABLE_BAT,
    TABLE_SDT,
    TABLE_EIT,
    TABLE_TOT,
    TABLE_TDT,
    TABLE_RST,
    TABLE_DIT,
    TABLE_SIT,
    TABLE_PMT,
Christophe Massiot's avatar
Christophe Massiot committed
113
    TABLE_SCTE35,
114 115 116 117
    TABLE_END
};
static const char * const ppsz_all_tables[TABLE_END] = {
    "pat", "cat", "tsdt", "nit", "bat", "sdt", "eit", "tot", "tdt", "rst",
Christophe Massiot's avatar
Christophe Massiot committed
118
    "dit", "sit", "pmt", "scte35"
119 120 121
};
static bool pb_print_table[TABLE_END];

Christophe Massiot's avatar
Christophe Massiot committed
122 123
static void handle_pmt_es(uint8_t *p_pmt, bool b_select);

124
/*****************************************************************************
125
 * print_wrapper
126
 *****************************************************************************/
127
static void print_wrapper(void *_unused, const char *psz_format, ...)
128 129 130 131 132 133 134
{
    char psz_fmt[strlen(psz_format) + 2];
    va_list args;
    va_start(args, psz_format);
    strcpy(psz_fmt, psz_format);
    strcat(psz_fmt, "\n");
    vprintf(psz_fmt, args);
135
    va_end(args);
136 137
}

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
/*****************************************************************************
 * iconv_wrapper
 *****************************************************************************/
static char *iconv_append_null(const char *p_string, size_t i_length)
{
    char *psz_string = malloc(i_length + 1);
    memcpy(psz_string, p_string, i_length);
    psz_string[i_length] = '\0';
    return psz_string;
}

static char *iconv_wrapper(void *_unused, const char *psz_encoding,
                           char *p_string, size_t i_length)
{
    char *psz_string, *p;
    size_t i_out_length;

    if (!strcmp(psz_encoding, psz_native_encoding))
        return iconv_append_null(p_string, i_length);

    if (iconv_handle != (iconv_t)-1 &&
        strcmp(psz_encoding, psz_current_encoding)) {
        iconv_close(iconv_handle);
        iconv_handle = (iconv_t)-1;
    }

    if (iconv_handle == (iconv_t)-1)
        iconv_handle = iconv_open(psz_native_encoding, psz_encoding);
    if (iconv_handle == (iconv_t)-1) {
167
        fprintf(stderr, "couldn't convert from %s to %s (%m)\n", psz_encoding,
168 169 170
                psz_native_encoding);
        return iconv_append_null(p_string, i_length);
    }
171
    psz_current_encoding = psz_encoding;
172 173 174 175 176

    /* converted strings can be up to six times larger */
    i_out_length = i_length * 6;
    p = psz_string = malloc(i_out_length);
    if (iconv(iconv_handle, &p_string, &i_length, &p, &i_out_length) == -1) {
177
        fprintf(stderr, "couldn't convert from %s to %s (%m)\n", psz_encoding,
178 179 180 181 182
                psz_native_encoding);
        free(psz_string);
        return iconv_append_null(p_string, i_length);
    }
    if (i_length)
183
        fprintf(stderr, "partial conversion from %s to %s\n", psz_encoding,
184 185 186 187 188 189
                psz_native_encoding);

    *p = '\0';
    return psz_string;
}

190 191 192 193 194 195 196 197 198 199
/*****************************************************************************
 * handle_pat
 *****************************************************************************/
static void handle_pat(void)
{
    PSI_TABLE_DECLARE(pp_old_pat_sections);
    uint8_t i_last_section = psi_table_get_lastsection(pp_next_pat_sections);
    uint8_t i;

    if (psi_table_validate(pp_current_pat_sections) &&
200 201
        psi_table_compare(pp_current_pat_sections, pp_next_pat_sections)) {
        /* Identical PAT. Shortcut. */
202 203 204 205 206 207
        psi_table_free(pp_next_pat_sections);
        psi_table_init(pp_next_pat_sections);
        return;
    }

    if (!pat_table_validate(pp_next_pat_sections)) {
208 209 210 211 212 213 214
        switch (i_print_type) {
        case PRINT_XML:
            printf("<ERROR type=\"invalid_pat\"/>\n");
            break;
        default:
            printf("invalid PAT received\n");
        }
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
        psi_table_free(pp_next_pat_sections);
        psi_table_init(pp_next_pat_sections);
        return;
    }

    /* Switch tables. */
    psi_table_copy(pp_old_pat_sections, pp_current_pat_sections);
    psi_table_copy(pp_current_pat_sections, pp_next_pat_sections);
    psi_table_init(pp_next_pat_sections);

    for (i = 0; i <= i_last_section; i++) {
        uint8_t *p_section = psi_table_get_section(pp_current_pat_sections, i);
        const uint8_t *p_program;
        int j = 0;

        while ((p_program = pat_get_program(p_section, j)) != NULL) {
            const uint8_t *p_old_program = NULL;
            uint16_t i_sid = patn_get_program(p_program);
            uint16_t i_pid = patn_get_pid(p_program);
            j++;

            if (i_sid == 0) {
                if (i_pid != NIT_PID)
238
                    fprintf(stderr,
239
                        "NIT is carried on PID %hu which isn't DVB compliant\n",
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
                        i_pid);
                continue; /* NIT */
            }

            if (!psi_table_validate(pp_old_pat_sections)
                  || (p_old_program =
                      pat_table_find_program(pp_old_pat_sections, i_sid))
                       == NULL
                  || patn_get_pid(p_old_program) != i_pid) {
                sid_t *p_sid;
                int i_pmt;
                if (p_old_program != NULL)
                    p_pids[patn_get_pid(p_old_program)].i_psi_refcount--;
                p_pids[i_pid].i_psi_refcount++;

                for (i_pmt = 0; i_pmt < i_nb_sids; i_pmt++)
                    if (pp_sids[i_pmt]->i_sid == i_sid ||
                        pp_sids[i_pmt]->i_sid == 0)
                        break;

                if (i_pmt == i_nb_sids) {
                    p_sid = malloc(sizeof(sid_t));
                    pp_sids = realloc(pp_sids, ++i_nb_sids * sizeof(sid_t *));
                    pp_sids[i_pmt] = p_sid;
                    p_sid->p_current_pmt = NULL;
265
                    psi_table_init(p_sid->pp_eit_sections);
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
                }
                else
                    p_sid = pp_sids[i_pmt];

                p_sid->i_sid = i_sid;
                p_sid->i_pmt_pid = i_pid;
            }
        }
    }

    if (psi_table_validate(pp_old_pat_sections)) {
        i_last_section = psi_table_get_lastsection( pp_old_pat_sections );
        for (i = 0; i <= i_last_section; i++) {
            uint8_t *p_section = psi_table_get_section(pp_old_pat_sections, i);
            const uint8_t *p_program;
            int j = 0;

            while ((p_program = pat_get_program(p_section, j)) != NULL) {
                uint16_t i_sid = patn_get_program(p_program);
                j++;

                if (i_sid == 0)
                    continue; /* NIT */

                if (pat_table_find_program(pp_current_pat_sections, i_sid)
                      == NULL) {
                    int i_pmt;
                    for (i_pmt = 0; i_pmt < i_nb_sids; i_pmt++)
                        if (pp_sids[i_pmt]->i_sid == i_sid) {
                            pp_sids[i_pmt]->i_sid = 0;
296 297 298
                            if (pp_sids[i_pmt]->p_current_pmt != NULL)
                                handle_pmt_es(pp_sids[i_pmt]->p_current_pmt,
                                              false);
299
                            free(pp_sids[i_pmt]->p_current_pmt);
300
                            pp_sids[i_pmt]->p_current_pmt = NULL;
301 302
                            psi_table_free(pp_sids[i]->pp_eit_sections);
                            psi_table_init(pp_sids[i]->pp_eit_sections);
303 304 305 306 307 308 309 310 311
                            break;
                        }
                }
            }
        }

        psi_table_free(pp_old_pat_sections);
    }

312 313 314
    if (pb_print_table[TABLE_PAT])
        pat_table_print(pp_current_pat_sections, print_wrapper, NULL,
                        i_print_type);
315 316 317 318 319
}

static void handle_pat_section(uint16_t i_pid, uint8_t *p_section)
{
    if (i_pid != PAT_PID || !pat_validate(p_section)) {
320 321 322 323 324 325 326
        switch (i_print_type) {
        case PRINT_XML:
            printf("<ERROR type=\"invalid_pat_section\"/>\n");
            break;
        default:
            printf("invalid PAT section received on PID %hu\n", i_pid);
        }
327 328 329 330 331 332 333 334 335 336
        free(p_section);
        return;
    }

    if (!psi_table_section(pp_next_pat_sections, p_section))
        return;

    handle_pat();
}

337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
/*****************************************************************************
 * handle_cat
 *****************************************************************************/
static void handle_cat(void)
{
    PSI_TABLE_DECLARE(pp_old_cat_sections);

    if (psi_table_validate(pp_current_cat_sections) &&
        psi_table_compare(pp_current_cat_sections, pp_next_cat_sections)) {
        /* Identical CAT. Shortcut. */
        psi_table_free(pp_next_cat_sections);
        psi_table_init(pp_next_cat_sections);
        return;
    }

    if (!cat_table_validate(pp_next_cat_sections)) {
        switch (i_print_type) {
        case PRINT_XML:
            printf("<ERROR type=\"invalid_cat\"/>\n");
            break;
        default:
            printf("invalid CAT received\n");
        }
        psi_table_free(pp_next_cat_sections);
        psi_table_init(pp_next_cat_sections);
        return;
    }

    /* Switch tables. */
    psi_table_copy(pp_old_cat_sections, pp_current_cat_sections);
    psi_table_copy(pp_current_cat_sections, pp_next_cat_sections);
    psi_table_init(pp_next_cat_sections);

    if (psi_table_validate(pp_old_cat_sections))
        psi_table_free(pp_old_cat_sections);

373 374 375
    if (pb_print_table[TABLE_CAT])
        cat_table_print(pp_current_cat_sections, print_wrapper, NULL,
                        i_print_type);
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
}

static void handle_cat_section(uint16_t i_pid, uint8_t *p_section)
{
    if (i_pid != CAT_PID || !cat_validate(p_section)) {
        switch (i_print_type) {
        case PRINT_XML:
            printf("<ERROR type=\"invalid_cat_section\"/>\n");
            break;
        default:
            printf("invalid CAT section received on PID %hu\n", i_pid);
        }
        free(p_section);
        return;
    }

    if (!psi_table_section(pp_next_cat_sections, p_section))
        return;

    handle_cat();
}

398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
/*****************************************************************************
 * handle_tsdt
 *****************************************************************************/
static void handle_tsdt(void)
{
    PSI_TABLE_DECLARE(pp_old_tsdt_sections);

    if (psi_table_validate(pp_current_tsdt_sections) &&
        psi_table_compare(pp_current_tsdt_sections, pp_next_tsdt_sections)) {
        /* Identical TSDT. Shortcut. */
        psi_table_free(pp_next_tsdt_sections);
        psi_table_init(pp_next_tsdt_sections);
        return;
    }

    if (!tsdt_table_validate(pp_next_tsdt_sections)) {
        switch (i_print_type) {
        case PRINT_XML:
            printf("<ERROR type=\"invalid_tsdt\"/>\n");
            break;
        default:
            printf("invalid TSDT received\n");
        }
        psi_table_free(pp_next_tsdt_sections);
        psi_table_init(pp_next_tsdt_sections);
        return;
    }

    /* Switch tables. */
    psi_table_copy(pp_old_tsdt_sections, pp_current_tsdt_sections);
    psi_table_copy(pp_current_tsdt_sections, pp_next_tsdt_sections);
    psi_table_init(pp_next_tsdt_sections);

    if (psi_table_validate(pp_old_tsdt_sections))
        psi_table_free(pp_old_tsdt_sections);

434 435 436
    if (pb_print_table[TABLE_TSDT])
        tsdt_table_print(pp_current_tsdt_sections, print_wrapper, NULL,
                         i_print_type);
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
}

static void handle_tsdt_section(uint16_t i_pid, uint8_t *p_section)
{
    if (i_pid != TSDT_PID || !tsdt_validate(p_section)) {
        switch (i_print_type) {
        case PRINT_XML:
            printf("<ERROR type=\"invalid_tsdt_section\"/>\n");
            break;
        default:
            printf("invalid TSDT section received on PID %hu\n", i_pid);
        }
        free(p_section);
        return;
    }

    if (!psi_table_section(pp_next_tsdt_sections, p_section))
        return;

    handle_tsdt();
}

459 460 461
/*****************************************************************************
 * handle_pmt
 *****************************************************************************/
Christophe Massiot's avatar
Christophe Massiot committed
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
static void handle_pmt_es(uint8_t *p_pmt, bool b_select)
{
    int j = 0;
    uint8_t *p_es;
    while ((p_es = pmt_get_es(p_pmt, j)) != NULL) {
        uint16_t i_pid = pmtn_get_pid(p_es);
        uint8_t i_type = pmtn_get_streamtype(p_es);
        j++;

        switch (i_type) {
            case PMT_STREAMTYPE_SCTE_35:
                if (b_select)
                    p_pids[i_pid].i_psi_refcount++;
                else
                    p_pids[i_pid].i_psi_refcount--;
                break;
            default:
                break;
        }
    }
}

484 485 486 487 488 489 490 491
static void handle_pmt(uint16_t i_pid, uint8_t *p_pmt)
{
    uint16_t i_sid = pmt_get_program(p_pmt);
    sid_t *p_sid;
    int i;

    /* we do this before checking the service ID */
    if (!pmt_validate(p_pmt)) {
492 493 494 495 496 497 498 499
        switch (i_print_type) {
        case PRINT_XML:
            printf("<ERROR type=\"invalid_pmt_section\" pid=\"%hu\"/>\n",
                   i_pid);
            break;
        default:
            printf("invalid PMT section received on PID %hu\n", i_pid);
        }
500 501 502 503 504 505 506 507 508
        free(p_pmt);
        return;
    }

    for (i = 0; i < i_nb_sids; i++)
        if (pp_sids[i]->i_sid && pp_sids[i]->i_sid == i_sid)
            break;

    if (i == i_nb_sids) {
509 510
        switch (i_print_type) {
        case PRINT_XML:
511
            printf("<ERROR type=\"ghost_pmt\" program=\"%hu\" pid=\"%hu\"/>\n",
512 513 514 515 516 517
                   i_sid, i_pid);
            break;
        default:
            printf("ghost PMT for service %hu carried on PID %hu\n", i_sid,
                   i_pid);
        }
518 519 520 521 522 523
        p_sid = malloc(sizeof(sid_t));
        pp_sids = realloc(pp_sids, ++i_nb_sids * sizeof(sid_t *));
        pp_sids[i] = p_sid;
        p_sid->i_sid = i_sid;
        p_sid->i_pmt_pid = i_pid;
        p_sid->p_current_pmt = NULL;
524
        psi_table_init(p_sid->pp_eit_sections);
525 526
    } else {
        p_sid = pp_sids[i];
527 528 529
        if (i_pid != p_sid->i_pmt_pid) {
            switch (i_print_type) {
            case PRINT_XML:
530
                printf("<ERROR type=\"ghost_pmt\" program=\"%hu\" pid=\"%hu\"/>\n",
531 532 533 534 535 536 537
                       i_sid, i_pid);
                break;
            default:
                printf("ghost PMT for service %hu carried on PID %hu\n", i_sid,
                       i_pid);
            }
        }
538 539 540
    }

    if (p_sid->p_current_pmt != NULL &&
541 542
        psi_compare(p_sid->p_current_pmt, p_pmt)) {
        /* Identical PMT. Shortcut. */
543 544 545 546
        free(p_pmt);
        return;
    }

Christophe Massiot's avatar
Christophe Massiot committed
547 548 549
    if (p_sid->p_current_pmt)
        handle_pmt_es(p_sid->p_current_pmt, false);

550 551
    free(p_sid->p_current_pmt);
    p_sid->p_current_pmt = p_pmt;
Christophe Massiot's avatar
Christophe Massiot committed
552
    handle_pmt_es(p_pmt, true);
553

554 555 556
    if (pb_print_table[TABLE_PMT])
        pmt_print(p_pmt, print_wrapper, NULL, iconv_wrapper, NULL,
                  i_print_type);
557 558 559 560 561 562 563 564
}

/*****************************************************************************
 * handle_nit
 *****************************************************************************/
static void handle_nit(void)
{
    if (psi_table_validate(pp_current_nit_sections) &&
565
        psi_table_compare(pp_current_nit_sections, pp_next_nit_sections)) {
566 567 568 569 570 571 572
        /* Same version NIT. Shortcut. */
        psi_table_free(pp_next_nit_sections);
        psi_table_init(pp_next_nit_sections);
        return;
    }

    if (!nit_table_validate(pp_next_nit_sections)) {
573 574 575 576 577 578 579
        switch (i_print_type) {
        case PRINT_XML:
            printf("<ERROR type=\"invalid_nit\"/>\n");
            break;
        default:
            printf("invalid NIT received\n");
        }
580 581
        psi_table_free(pp_next_nit_sections);
        psi_table_init(pp_next_nit_sections);
582 583 584 585 586 587 588 589
        return;
    }

    /* Switch tables. */
    psi_table_free(pp_current_nit_sections);
    psi_table_copy(pp_current_nit_sections, pp_next_nit_sections);
    psi_table_init(pp_next_nit_sections);

590 591 592
    if (pb_print_table[TABLE_NIT])
        nit_table_print(pp_current_nit_sections, print_wrapper, NULL,
                        iconv_wrapper, NULL, i_print_type);
593 594 595 596 597
}

static void handle_nit_section(uint16_t i_pid, uint8_t *p_section)
{
    if (i_pid != NIT_PID || !nit_validate(p_section)) {
598 599 600 601 602 603 604 605
        switch (i_print_type) {
        case PRINT_XML:
            printf("<ERROR type=\"invalid_nit_section\" pid=\"%hu\"/>\n",
                   i_pid);
            break;
        default:
            printf("invalid NIT section received on PID %hu\n", i_pid);
        }
606 607 608 609 610 611 612 613 614 615
        free(p_section);
        return;
    }

    if (!psi_table_section(pp_next_nit_sections, p_section))
        return;

    handle_nit();
}

616 617 618
/*****************************************************************************
 * handle_bat
 *****************************************************************************/
619
static void handle_bat(bouquet_t *p_bouquet)
620
{
621 622 623
    if (psi_table_validate(p_bouquet->pp_current_bat_sections) &&
        psi_table_compare(p_bouquet->pp_current_bat_sections,
                          p_bouquet->pp_next_bat_sections)) {
624
        /* Same version BAT. Shortcut. */
625 626
        psi_table_free(p_bouquet->pp_next_bat_sections);
        psi_table_init(p_bouquet->pp_next_bat_sections);
627 628 629
        return;
    }

630
    if (!bat_table_validate(p_bouquet->pp_next_bat_sections)) {
631 632 633 634 635 636 637
        switch (i_print_type) {
        case PRINT_XML:
            printf("<ERROR type=\"invalid_bat\"/>\n");
            break;
        default:
            printf("invalid BAT received\n");
        }
638 639
        psi_table_free(p_bouquet->pp_next_bat_sections);
        psi_table_init(p_bouquet->pp_next_bat_sections);
640 641 642 643
        return;
    }

    /* Switch tables. */
644 645 646 647
    psi_table_free(p_bouquet->pp_current_bat_sections);
    psi_table_copy(p_bouquet->pp_current_bat_sections,
                   p_bouquet->pp_next_bat_sections);
    psi_table_init(p_bouquet->pp_next_bat_sections);
648

649
    if (pb_print_table[TABLE_BAT])
650
        bat_table_print(p_bouquet->pp_current_bat_sections, print_wrapper, NULL,
651
                        iconv_wrapper, NULL, i_print_type);
652 653 654 655
}

static void handle_bat_section(uint16_t i_pid, uint8_t *p_section)
{
656 657 658 659
    uint16_t i_bouquet;
    bouquet_t *p_bouquet;
    int i;

660 661 662 663 664 665 666 667 668 669 670 671 672
    if (i_pid != BAT_PID || !bat_validate(p_section)) {
        switch (i_print_type) {
        case PRINT_XML:
            printf("<ERROR type=\"invalid_bat_section\" pid=\"%hu\"/>\n",
                   i_pid);
            break;
        default:
            printf("invalid BAT section received on PID %hu\n", i_pid);
        }
        free(p_section);
        return;
    }

673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
    i_bouquet = psi_get_tableidext(p_section);
    for (i = 0; i < i_nb_bouquets; i++)
        if (pp_bouquets[i]->i_bouquet && pp_bouquets[i]->i_bouquet == i_bouquet)
            break;
    if (i == i_nb_bouquets) {
        p_bouquet = malloc(sizeof(bouquet_t));
        pp_bouquets = realloc(pp_bouquets, ++i_nb_bouquets * sizeof(bouquet_t *));
        pp_bouquets[i] = p_bouquet;
        p_bouquet->i_bouquet = i_bouquet;
        psi_table_init(p_bouquet->pp_current_bat_sections);
        psi_table_init(p_bouquet->pp_next_bat_sections);
    } else
        p_bouquet = pp_bouquets[i];

    if (!psi_table_section(p_bouquet->pp_next_bat_sections, p_section))
688 689
        return;

690
    handle_bat(p_bouquet);
691 692
}

693 694 695 696 697 698
/*****************************************************************************
 * handle_sdt
 *****************************************************************************/
static void handle_sdt(void)
{
    if (psi_table_validate(pp_current_sdt_sections) &&
699 700
        psi_table_compare(pp_current_sdt_sections, pp_next_sdt_sections)) {
        /* Identical SDT. Shortcut. */
701 702 703 704 705 706
        psi_table_free(pp_next_sdt_sections);
        psi_table_init(pp_next_sdt_sections);
        return;
    }

    if (!sdt_table_validate(pp_next_sdt_sections)) {
707 708 709 710 711 712 713
        switch (i_print_type) {
        case PRINT_XML:
            printf("<ERROR type=\"invalid_sdt\"/>\n");
            break;
        default:
            printf("invalid SDT received\n");
        }
714 715 716 717 718 719 720 721 722 723
        psi_table_free(pp_next_sdt_sections);
        psi_table_init(pp_next_sdt_sections);
        return;
    }

    /* Switch tables. */
    psi_table_free(pp_current_sdt_sections);
    psi_table_copy(pp_current_sdt_sections, pp_next_sdt_sections);
    psi_table_init(pp_next_sdt_sections);

724 725 726
    if (pb_print_table[TABLE_SDT])
        sdt_table_print(pp_current_sdt_sections, print_wrapper, NULL,
                        iconv_wrapper, NULL, i_print_type);
727 728 729 730 731
}

static void handle_sdt_section(uint16_t i_pid, uint8_t *p_section)
{
    if (i_pid != SDT_PID || !sdt_validate(p_section)) {
732 733 734 735 736 737 738 739
        switch (i_print_type) {
        case PRINT_XML:
            printf("<ERROR type=\"invalid_sdt_section\" pid=\"%hu\"/>\n",
                   i_pid);
            break;
        default:
            printf("invalid SDT section received on PID %hu\n", i_pid);
        }
740 741 742 743 744 745 746 747 748 749
        free(p_section);
        return;
    }

    if (!psi_table_section(pp_next_sdt_sections, p_section))
        return;

    handle_sdt();
}

750 751 752
/*****************************************************************************
 * handle_eit
 *****************************************************************************/
753
static void handle_eit_section(uint16_t i_pid, uint8_t *p_section)
754
{
755 756 757 758 759 760
    uint16_t i_sid;
    uint8_t i_section;
    sid_t *p_sid;
    int i;

    if (i_pid != EIT_PID || !eit_validate(p_section)) {
761 762 763 764 765 766 767 768
        switch (i_print_type) {
        case PRINT_XML:
            printf("<ERROR type=\"invalid_eit_section\" pid=\"%hu\"/>\n",
                   i_pid);
            break;
        default:
            printf("invalid EIT section received on PID %hu\n", i_pid);
        }
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
        free(p_section);
        return;
    }

    i_sid = psi_get_tableidext(p_section);
    for (i = 0; i < i_nb_sids; i++)
        if (pp_sids[i]->i_sid && pp_sids[i]->i_sid == i_sid)
            break;
    if (i == i_nb_sids) {
        switch (i_print_type) {
        case PRINT_XML:
            printf("<ERROR type=\"ghost_eit\" sid=\"%hu\"/>\n", i_sid);
            break;
        default:
            printf("ghost EIT for service %hu\n", i_sid);
        }
        p_sid = malloc(sizeof(sid_t));
        pp_sids = realloc(pp_sids, ++i_nb_sids * sizeof(sid_t *));
        pp_sids[i] = p_sid;
        p_sid->i_sid = i_sid;
        p_sid->i_pmt_pid = 0;
        p_sid->p_current_pmt = NULL;
        psi_table_init(p_sid->pp_eit_sections);
    } else
        p_sid = pp_sids[i];

    /* We do not use psi_table_* primitives as the spec allows for holes in
     * section numbering, and there is no sure way to know whether you have
     * gathered all sections. */
    i_section = psi_get_section(p_section);
    if (p_sid->pp_eit_sections[i_section] != NULL &&
        psi_compare(p_sid->pp_eit_sections[i_section], p_section)) {
        /* Identical section. Shortcut. */
        free(p_section);
803 804 805
        return;
    }

806 807 808
    free(p_sid->pp_eit_sections[i_section]);
    p_sid->pp_eit_sections[i_section] = p_section;

809
    if (pb_print_table[TABLE_EIT])
810
        eit_print(p_section, print_wrapper, NULL, iconv_wrapper, NULL,
811
                  i_print_type);
812 813
}

814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
/*****************************************************************************
 * handle_tdt
 *****************************************************************************/
static void handle_tdt_section(uint16_t i_pid, uint8_t *p_tdt)
{
    if (i_pid != TDT_PID || !tdt_validate(p_tdt)) {
        switch (i_print_type) {
        case PRINT_XML:
            printf("<ERROR type=\"invalid_tdt_section\" pid=\"%hu\"/>\n",
                   i_pid);
            break;
        default:
            printf("invalid TDT section received on PID %hu\n", i_pid);
        }
        free(p_tdt);
        return;
    }

832 833 834
    if (pb_print_table[TABLE_TDT])
        tdt_print(p_tdt, print_wrapper, NULL, iconv_wrapper, NULL,
                  i_print_type);
835 836 837 838

    free(p_tdt);
}

839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
/*****************************************************************************
 * handle_tot
 *****************************************************************************/
static void handle_tot_section(uint16_t i_pid, uint8_t *p_tot)
{
    if (i_pid != TOT_PID || !tot_validate(p_tot)) {
        switch (i_print_type) {
        case PRINT_XML:
            printf("<ERROR type=\"invalid_tot_section\" pid=\"%hu\"/>\n",
                   i_pid);
            break;
        default:
            printf("invalid TOT section received on PID %hu\n", i_pid);
        }
        free(p_tot);
        return;
    }

857 858 859
    if (pb_print_table[TABLE_TOT])
        tot_print(p_tot, print_wrapper, NULL, iconv_wrapper, NULL,
                  i_print_type);
860 861 862 863

    free(p_tot);
}

864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
/*****************************************************************************
 * handle_dit
 *****************************************************************************/
static void handle_dit_section(uint16_t i_pid, uint8_t *p_dit)
{
    if (i_pid != DIT_PID || !dit_validate(p_dit)) {
        switch (i_print_type) {
        case PRINT_XML:
            printf("<ERROR type=\"invalid_dit_section\" pid=\"%hu\"/>\n",
                   i_pid);
            break;
        default:
            printf("invalid DIT section received on PID %hu\n", i_pid);
        }
        free(p_dit);
        return;
    }

882 883 884
    if (pb_print_table[TABLE_DIT])
        dit_print(p_dit, print_wrapper, NULL, iconv_wrapper, NULL,
                  i_print_type);
885 886 887 888

    free(p_dit);
}

889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
/*****************************************************************************
 * handle_rst
 *****************************************************************************/
static void handle_rst_section(uint16_t i_pid, uint8_t *p_rst)
{
    if (i_pid != RST_PID || !rst_validate(p_rst)) {
        switch (i_print_type) {
        case PRINT_XML:
            printf("<ERROR type=\"invalid_rst_section\" pid=\"%hu\"/>\n",
                   i_pid);
            break;
        default:
            printf("invalid RST section received on PID %hu\n", i_pid);
        }
        free(p_rst);
        return;
    }

907 908 909
    if (pb_print_table[TABLE_RST])
        rst_print(p_rst, print_wrapper, NULL, iconv_wrapper, NULL,
                  i_print_type);
910 911 912 913

    free(p_rst);
}

914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
/*****************************************************************************
 * handle_sit
 *****************************************************************************/
static void handle_sit_section(uint16_t i_pid, uint8_t *p_sit)
{
    if (i_pid != SIT_PID || !sit_validate(p_sit)) {
        switch (i_print_type) {
        case PRINT_XML:
            printf("<ERROR type=\"invalid_sit_section\" pid=\"%hu\"/>\n",
                   i_pid);
            break;
        default:
            printf("invalid SIT section received on PID %hu\n", i_pid);
        }
        free(p_sit);
        return;
    }

932 933 934
    if (pb_print_table[TABLE_SIT])
        sit_print(p_sit, print_wrapper, NULL, iconv_wrapper, NULL,
                  i_print_type);
935 936 937 938

    free(p_sit);
}

Christophe Massiot's avatar
Christophe Massiot committed
939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
/*****************************************************************************
 * handle_scte35
 *****************************************************************************/
static void handle_scte35_section(uint16_t i_pid, uint8_t *p_scte35)
{
    if (!scte35_validate(p_scte35)) {
        switch (i_print_type) {
        case PRINT_XML:
            printf("<ERROR type=\"invalid_scte35_section\" pid=\"%hu\"/>\n",
                   i_pid);
            break;
        default:
            printf("invalid SCTE35 section received on PID %hu\n", i_pid);
        }
        free(p_scte35);
        return;
    }

    if (pb_print_table[TABLE_SCTE35] &&
        scte35_get_command_type(p_scte35) != SCTE35_NULL_COMMAND)
        scte35_print(p_scte35, print_wrapper, NULL, i_print_type);

    free(p_scte35);
}

964 965 966 967 968 969 970 971
/*****************************************************************************
 * handle_section
 *****************************************************************************/
static void handle_section(uint16_t i_pid, uint8_t *p_section)
{
    uint8_t i_table_id = psi_get_tableid(p_section);

    if (!psi_validate(p_section)) {
972 973 974 975 976 977 978
        switch (i_print_type) {
        case PRINT_XML:
            printf("<ERROR type=\"invalid_section\" pid=\"%hu\"/>\n", i_pid);
            break;
        default:
            printf("invalid section on PID %hu\n", i_pid);
        }
979 980 981 982 983 984 985 986 987
        free(p_section);
        return;
    }

    switch (i_table_id) {
    case PAT_TABLE_ID:
        handle_pat_section(i_pid, p_section);
        break;

988 989 990 991
    case CAT_TABLE_ID:
        handle_cat_section(i_pid, p_section);
        break;

992 993 994 995
    case TSDT_TABLE_ID:
        handle_tsdt_section(i_pid, p_section);
        break;

996 997 998 999 1000 1001 1002 1003
    case PMT_TABLE_ID:
        handle_pmt(i_pid, p_section);
        break;

    case NIT_TABLE_ID_ACTUAL:
        handle_nit_section(i_pid, p_section);
        break;

1004 1005 1006 1007
    case BAT_TABLE_ID:
        handle_bat_section(i_pid, p_section);
        break;

1008 1009 1010 1011
    case SDT_TABLE_ID_ACTUAL:
        handle_sdt_section(i_pid, p_section);
        break;

1012 1013 1014 1015
    case TDT_TABLE_ID:
        handle_tdt_section(i_pid, p_section);
        break;

1016 1017 1018 1019
    case TOT_TABLE_ID:
        handle_tot_section(i_pid, p_section);
        break;

1020 1021 1022 1023
    case RST_TABLE_ID:
        handle_rst_section(i_pid, p_section);
        break;

1024 1025 1026 1027
    case DIT_TABLE_ID:
        handle_dit_section(i_pid, p_section);
        break;

1028 1029 1030 1031
    case SIT_TABLE_ID:
        handle_sit_section(i_pid, p_section);
        break;

1032 1033 1034
    case EIT_TABLE_ID_PF_ACTUAL:
        handle_eit_section(i_pid, p_section);
        break;
1035

Christophe Massiot's avatar
Christophe Massiot committed
1036 1037 1038 1039
    case SCTE35_TABLE_ID:
        handle_scte35_section(i_pid, p_section);
        break;

1040
    default:
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
        free( p_section );
        break;
    }
}

/*****************************************************************************
 * handle_psi_packet
 *****************************************************************************/
static void handle_psi_packet(uint8_t *p_ts)
{
    uint16_t i_pid = ts_get_pid(p_ts);
    ts_pid_t *p_pid = &p_pids[i_pid];
    uint8_t i_cc = ts_get_cc(p_ts);
1054
    const uint8_t *p_payload;
1055 1056 1057 1058 1059 1060 1061 1062 1063
    uint8_t i_length;

    if (ts_check_duplicate(i_cc, p_pid->i_last_cc) || !ts_has_payload(p_ts))
        return;

    if (p_pid->i_last_cc != -1
          && ts_check_discontinuity(i_cc, p_pid->i_last_cc))
        psi_assemble_reset(&p_pid->p_psi_buffer, &p_pid->i_psi_buffer_used);

1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
    p_payload = ts_section(p_ts);
    i_length = p_ts + TS_SIZE - p_payload;

    if (!psi_assemble_empty(&p_pid->p_psi_buffer, &p_pid->i_psi_buffer_used)) {
        uint8_t *p_section = psi_assemble_payload(&p_pid->p_psi_buffer,
                                                  &p_pid->i_psi_buffer_used,
                                                  &p_payload, &i_length);
        if (p_section != NULL)
            handle_section(i_pid, p_section);
    }
1074

1075
    p_payload = ts_next_section( p_ts );
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
    i_length = p_ts + TS_SIZE - p_payload;

    while (i_length) {
        uint8_t *p_section = psi_assemble_payload(&p_pid->p_psi_buffer,
                                                  &p_pid->i_psi_buffer_used,
                                                  &p_payload, &i_length);
        if (p_section != NULL)
            handle_section(i_pid, p_section);
    }
}

/*****************************************************************************
 * Main loop
 *****************************************************************************/
1090 1091
static void usage(const char *psz)
{
1092
    fprintf(stderr, "usage: %s [-x xml] [-T <tables>] < <input file> [> <output>]\n", psz);
1093 1094 1095
    exit(EXIT_FAILURE);
}

1096 1097
int main(int i_argc, char **ppsz_argv)
{
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
    int i, c;
    char *psz_tables = NULL;

    static const struct option long_options[] = {
        { "print",           required_argument, NULL, 'x' },
        { "help",            no_argument,       NULL, 'h' },
        { "version",         no_argument,       NULL, 'V' },
        { "tables",          no_argument,       NULL, 'T' },
        { 0, 0, 0, 0 }
    };

    while ((c = getopt_long(i_argc, ppsz_argv, "x:hVT:", long_options, NULL)) != -1)
    {
        switch (c) {
        case 'x':
            if (!strcmp(optarg, "text"))
                i_print_type = PRINT_TEXT;
            else if (!strcmp(optarg, "xml"))
                i_print_type = PRINT_XML;
            else
                fprintf(stderr, "unrecognized print type %s\n", optarg);
            break;
1120

1121 1122 1123
        case 'T':
            psz_tables = strdup(optarg);
            break;
1124

1125 1126 1127 1128 1129 1130 1131 1132 1133
        case 'V':
            fprintf(stderr, "biTStream %d.%d.%d\n", BITSTREAM_VERSION_MAJOR,
                    BITSTREAM_VERSION_MINOR, BITSTREAM_VERSION_REVISION);
            exit(0);
            break;


        case 'h':
        default:
1134
            usage(ppsz_argv[0]);
1135
        }
1136
    }
1137 1138
    if (optind < i_argc)
        usage(ppsz_argv[0]);
1139

1140 1141
    setvbuf(stdout, NULL, _IOLBF, 0);

1142 1143 1144 1145
    memset(p_pids, 0, sizeof(p_pids));

    for (i = 0; i < 8192; i++) {
        p_pids[i].i_last_cc = -1;
1146 1147
        psi_assemble_init(&p_pids[i].p_psi_buffer,
                          &p_pids[i].i_psi_buffer_used);
1148 1149 1150
    }

    p_pids[PAT_PID].i_psi_refcount++;
1151
    p_pids[CAT_PID].i_psi_refcount++;
1152
    p_pids[TSDT_PID].i_psi_refcount++;
1153
    p_pids[NIT_PID].i_psi_refcount++;
1154
    p_pids[BAT_PID].i_psi_refcount++;
1155
    p_pids[SDT_PID].i_psi_refcount++;
1156
    p_pids[EIT_PID].i_psi_refcount++;
1157
    p_pids[TDT_PID].i_psi_refcount++;
1158
    p_pids[RST_PID].i_psi_refcount++;
1159
    p_pids[DIT_PID].i_psi_refcount++;
1160
    p_pids[SIT_PID].i_psi_refcount++;
1161

1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
    psi_table_init(pp_current_pat_sections);
    psi_table_init(pp_next_pat_sections);
    psi_table_init(pp_current_cat_sections);
    psi_table_init(pp_next_cat_sections);
    psi_table_init(pp_current_tsdt_sections);
    psi_table_init(pp_next_tsdt_sections);
    psi_table_init(pp_current_nit_sections);
    psi_table_init(pp_next_nit_sections);
    psi_table_init(pp_current_sdt_sections);
    psi_table_init(pp_next_sdt_sections);

1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
    if (psz_tables != NULL) {
        char *psz_table = psz_tables;
        for (i = 0; i < TABLE_END; i++)
            pb_print_table[i] = false;

        do {
            char *psz_next = strpbrk(psz_table, ",");
            if (psz_next != NULL) *psz_next++ = '\0';

            for (i = 0; i < TABLE_END; i++)
                if (!strcasecmp(psz_table, ppsz_all_tables[i]))
                    pb_print_table[i] = true;

            psz_table = psz_next;
        } while (psz_table != NULL);

        free(psz_tables);
    } else
        for (i = 0; i < TABLE_END; i++)
            pb_print_table[i] = true;

1194 1195 1196 1197 1198 1199 1200 1201 1202
    switch (i_print_type) {
    case PRINT_XML:
        printf("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
        printf("<TS>\n");
        break;
    default:
        break;
    }

1203
    bool b_is_last_invalid = false;
1204 1205 1206 1207
    while (!feof(stdin) && !ferror(stdin)) {
        uint8_t p_ts[TS_SIZE];
        size_t i_ret = fread(p_ts, sizeof(p_ts), 1, stdin);
        if (i_ret != 1) continue;
1208
        if (!ts_validate(p_ts)) {
1209 1210 1211 1212 1213 1214 1215 1216 1217
            if (!b_is_last_invalid) {
                switch (i_print_type) {
                case PRINT_XML:
                    printf("<ERROR type=\"invalid_ts\"/>\n");
                    break;
                default:
                    printf("invalid TS packet\n");
                }
                b_is_last_invalid = true;
1218
            }
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230

            int i;
            for (i = 1; i < TS_SIZE; i++) {
                if (ts_validate(p_ts + i)) {
                    memmove(p_ts, p_ts + i, TS_SIZE - i);
                    i_ret = fread(p_ts + TS_SIZE - i, i, 1, stdin);
                    if (i_ret != 1) continue;
                    break;
                }
            }
            if (i == TS_SIZE)
                continue;
1231
        }
1232 1233 1234 1235 1236 1237 1238

        uint16_t i_pid = ts_get_pid(p_ts);
        ts_pid_t *p_pid = &p_pids[i_pid];
        if (p_pid->i_psi_refcount)
            handle_psi_packet(p_ts);
        p_pid->i_last_cc = ts_get_cc(p_ts);
        b_is_last_invalid = false;
1239 1240
    }

1241 1242 1243 1244 1245 1246 1247 1248
    switch (i_print_type) {
    case PRINT_XML:
        printf("</TS>\n");
        break;
    default:
        break;
    }

1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
    if (iconv_handle != (iconv_t)-1)
        iconv_close(iconv_handle);

    psi_table_free(pp_current_pat_sections);
    psi_table_free(pp_next_pat_sections);
    psi_table_free(pp_current_cat_sections);
    psi_table_free(pp_next_cat_sections);
    psi_table_free(pp_current_tsdt_sections);
    psi_table_free(pp_next_tsdt_sections);
    psi_table_free(pp_current_nit_sections);
    psi_table_free(pp_next_nit_sections);
    psi_table_free(pp_current_sdt_sections);
    psi_table_free(pp_next_sdt_sections);

    for (i = 0; i < i_nb_bouquets; i++) {
        psi_table_free(pp_bouquets[i]->pp_current_bat_sections);
        psi_table_free(pp_bouquets[i]->pp_next_bat_sections);
        free(pp_bouquets[i]);
    }
    free(pp_bouquets);

    for (i = 0; i < i_nb_sids; i++) {
        psi_table_free(pp_sids[i]->pp_eit_sections);
        free(pp_sids[i]->p_current_pmt);
        free(pp_sids[i]);
    }
    free(pp_sids);

    for (i = 0; i < 8192; i++)
        psi_assemble_reset(&p_pids[i].p_psi_buffer,
                           &p_pids[i].i_psi_buffer_used);

1281
    return EXIT_SUCCESS;
1282
}