tables.c 29.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
/*****************************************************************************
 * tables.c
 *****************************************************************************
 * Copyright (C) 2001-2005, 2015 VLC authors and VideoLAN
 *
 * 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
 * (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
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * 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.
 *****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <vlc_common.h>
#include <vlc_block.h>
#include <vlc_es.h>

# include <dvbpsi/dvbpsi.h>
# include <dvbpsi/demux.h>
# include <dvbpsi/descriptor.h>
# include <dvbpsi/pat.h>
# include <dvbpsi/pmt.h>
# include <dvbpsi/sdt.h>
# include <dvbpsi/dr.h>
# include <dvbpsi/psi.h>

#include "dvbpsi_compat.h"

#include "streams.h"
#include "tsutil.h"
#include "tables.h"
#include "bits.h"
43
#include "pes.h"
44

45 46
#include "../../codec/jpeg2000.h"

47 48
#include <assert.h>

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
block_t *WritePSISection( dvbpsi_psi_section_t* p_section )
{
    block_t   *p_psi, *p_first = NULL;

    while( p_section )
    {
        int i_size = (uint32_t)(p_section->p_payload_end - p_section->p_data) +
                  (p_section->b_syntax_indicator ? 4 : 0);

        p_psi = block_Alloc( i_size + 1 );
        if( !p_psi )
            goto error;
        p_psi->i_pts = 0;
        p_psi->i_dts = 0;
        p_psi->i_length = 0;
        p_psi->i_buffer = i_size + 1;

        p_psi->p_buffer[0] = 0; /* pointer */
        memcpy( p_psi->p_buffer + 1,
                p_section->p_data,
                i_size );

        block_ChainAppend( &p_first, p_psi );

        p_section = p_section->p_next;
    }

    return( p_first );

error:
    if( p_first )
        block_ChainRelease( p_first );
    return NULL;
}

84
void BuildPAT( dvbpsi_t *p_dvbpsi,
85 86
               void *p_opaque, PEStoTSCallback pf_callback,
               int i_tsid, int i_pat_version_number,
87 88
               tsmux_stream_t *p_pat,
               unsigned i_programs, tsmux_stream_t *p_pmt, const int *pi_programs_number )
89 90 91 92
{
    dvbpsi_pat_t         patpsi;
    dvbpsi_psi_section_t *p_section;

93
    dvbpsi_pat_init( &patpsi, i_tsid, i_pat_version_number, true /* b_current_next */ );
94 95
    /* add all programs */
    for (unsigned i = 0; i < i_programs; i++ )
96
        dvbpsi_pat_program_add( &patpsi, pi_programs_number[i], p_pmt[i].i_pid );
97 98

    p_section = dvbpsi_pat_sections_generate( p_dvbpsi, &patpsi, 0 );
99 100 101 102 103 104 105 106 107 108
    if( likely(p_section) )
    {
        block_t *p_block = WritePSISection( p_section );
        if( likely(p_block) )
        {
            PEStoTS( p_opaque, pf_callback, p_block, p_pat->i_pid,
                     &p_pat->b_discontinuity, &p_pat->i_continuity_counter );
        }
        dvbpsi_DeletePSISections( p_section );
    }
109
    dvbpsi_pat_empty( &patpsi );
110 111 112 113 114 115 116 117 118 119 120 121 122 123
}
#if 1

static uint32_t GetDescriptorLength24b( int i_length )
{
    uint32_t i_l1, i_l2, i_l3;

    i_l1 = i_length&0x7f;
    i_l2 = ( i_length >> 7 )&0x7f;
    i_l3 = ( i_length >> 14 )&0x7f;

    return( 0x808000 | ( i_l3 << 16 ) | ( i_l2 << 8 ) | i_l1 );
}

124 125 126 127 128 129 130 131 132 133 134 135 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 167 168 169 170 171 172 173 174
static void Mpeg4SUBTDecoderSpecific_55( bits_buffer_t *b )
{
    bits_write( b, 8, 0x10 ); /* textFormat, 0x10 for 3GPP TS 26.245 */
    bits_write( b, 8, 0x00 ); /* flags: 1b: associated video info flag
                                        3b: reserved
                                        1b: duration flag
                                        3b: reserved */
    bits_write( b, 8, 52 );   /* remaining size */

    bits_write( b, 32, 0x00 ); /* display flags */

    bits_write( b, 8, 0x00 ); /* horizontal justification (-1: left, 0 center, 1 right) */
    bits_write( b, 8, 0x01 );  /* vertical   justification (-1: top, 0 center, 1 bottom) */

    bits_write( b, 24, 0x00 ); /* background rgb */
    bits_write( b, 8,  0xff ); /* background a */

    bits_write( b, 16,  0x00 ); /* text box top */
    bits_write( b, 16,  0x00 ); /* text box left */
    bits_write( b, 16,  0x00 ); /* text box bottom */
    bits_write( b, 16,  0x00 ); /* text box right */

    bits_write( b, 16,  0x00 ); /* start char */
    bits_write( b, 16,  0x00 ); /* end char */
    bits_write( b, 16,  0x00 ); /* default font id */


    bits_write( b, 8, 0x00 );   /* font style flags */
    bits_write( b, 8,   12 );   /* font size */

    bits_write( b, 24, 0x00 );  /* foreground rgb */
    bits_write( b,  8, 0x00 );  /* foreground a */

    bits_write( b, 24, 0x00 );
    bits_write( b,  8,   22 );  /* atom size */

    bits_write( b,  8,   'f' ); /* atom id */
    bits_write( b,  8,   't' );
    bits_write( b,  8,   'a' );
    bits_write( b,  8,   'b' );

    bits_write( b,  8,  0x00 );
    bits_write( b,  8,  0x01 ); /* entry count */

    bits_write( b, 16,  0x00 ); /* font id */
    bits_write( b,  8,     9 ); /* font name length */
    const char fontname[] = "Helvetica";
    for(int i=0; i<9; i++)
        bits_write( b,  8, fontname[i] ); /* font name */
}

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
static void GetPMTmpeg4( vlc_object_t *p_object, dvbpsi_pmt_t *p_dvbpmt,
                         unsigned i_mapped_streams, const pes_mapped_stream_t *p_mapped_streams )
{
    uint8_t iod[4096];
    bits_buffer_t bits, bits_fix_IOD;

    /* Make valgrind happy : it works at byte level not bit one so
     * bit_write confuse it (but DON'T CHANGE the way that bit_write is
     * working (needed when fixing some bits) */
    memset( iod, 0, 4096 );

    bits_initwrite( &bits, 4096, iod );
    /* IOD_label_scope */
    bits_write( &bits, 8,   0x11 );
    /* IOD_label */
    bits_write( &bits, 8,   0x01 );
    /* InitialObjectDescriptor */
    bits_align( &bits );
    bits_write( &bits, 8,   0x02 );     /* tag */
    bits_fix_IOD = bits;    /* save states to fix length later */
    bits_write( &bits, 24,
        GetDescriptorLength24b( 0 ) );  /* variable length (fixed later) */
    bits_write( &bits, 10,  0x01 );     /* ObjectDescriptorID */
    bits_write( &bits, 1,   0x00 );     /* URL Flag */
    bits_write( &bits, 1,   0x00 );     /* includeInlineProfileLevelFlag */
    bits_write( &bits, 4,   0x0f );     /* reserved */
    bits_write( &bits, 8,   0xff );     /* ODProfile (no ODcapability ) */
    bits_write( &bits, 8,   0xff );     /* sceneProfile */
    bits_write( &bits, 8,   0xfe );     /* audioProfile (unspecified) */
    bits_write( &bits, 8,   0xfe );     /* visualProfile( // ) */
    bits_write( &bits, 8,   0xff );     /* graphicProfile (no ) */
    for (unsigned i = 0; i < i_mapped_streams; i++ )
    {
        const pes_mapped_stream_t *p_stream = &p_mapped_streams[i];

        if( p_stream->pes->i_stream_id != 0xfa && p_stream->pes->i_stream_id != 0xfb &&
            p_stream->pes->i_stream_id != 0xfe )
            continue;

        bits_buffer_t bits_fix_ESDescr, bits_fix_Decoder;
        /* ES descriptor */
        bits_align( &bits );
        bits_write( &bits, 8,   0x03 );     /* ES_DescrTag */
        bits_fix_ESDescr = bits;
        bits_write( &bits, 24,
                    GetDescriptorLength24b( 0 ) ); /* variable size */
        bits_write( &bits, 16,  p_stream->pes->i_es_id );
        bits_write( &bits, 1,   0x00 );     /* streamDependency */
        bits_write( &bits, 1,   0x00 );     /* URL Flag */
        bits_write( &bits, 1,   0x00 );     /* OCRStreamFlag */
        bits_write( &bits, 5,   0x1f );     /* streamPriority */

        /* DecoderConfigDesciptor */
        bits_align( &bits );
        bits_write( &bits, 8,   0x04 ); /* DecoderConfigDescrTag */
        bits_fix_Decoder = bits;
        bits_write( &bits, 24,  GetDescriptorLength24b( 0 ) );
232
        if( p_stream->ts->i_stream_type == 0x10 )
233 234 235 236
        {
            bits_write( &bits, 8, 0x20 );   /* Visual 14496-2 */
            bits_write( &bits, 6, 0x04 );   /* VisualStream */
        }
237
        else if( p_stream->ts->i_stream_type == 0x1b )
238 239 240 241
        {
            bits_write( &bits, 8, 0x21 );   /* Visual 14496-2 */
            bits_write( &bits, 6, 0x04 );   /* VisualStream */
        }
242 243
        else if( p_stream->ts->i_stream_type == 0x11 ||
                 p_stream->ts->i_stream_type == 0x0f )
244 245 246 247
        {
            bits_write( &bits, 8, 0x40 );   /* Audio 14496-3 */
            bits_write( &bits, 6, 0x05 );   /* AudioStream */
        }
248
        else if( p_stream->ts->i_stream_type == 0x12 &&
249
                 p_stream->fmt->i_codec == VLC_CODEC_SUBT )
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
        {
            bits_write( &bits, 8, 0x0B );   /* Text Stream */
            bits_write( &bits, 6, 0x04 );   /* VisualStream */
        }
        else
        {
            bits_write( &bits, 8, 0x00 );
            bits_write( &bits, 6, 0x00 );

            msg_Err( p_object, "Unsupported stream_type => broken IOD" );
        }
        bits_write( &bits, 1,   0x00 );         /* UpStream */
        bits_write( &bits, 1,   0x01 );         /* reserved */
        bits_write( &bits, 24,  1024 * 1024 );  /* bufferSizeDB */
        bits_write( &bits, 32,  0x7fffffff );   /* maxBitrate */
        bits_write( &bits, 32,  0 );            /* avgBitrate */

267
        /* DecoderSpecificInfo */
268
        if( p_stream->fmt->i_codec == VLC_CODEC_SUBT )
269 270 271 272 273 274 275
        {
            bits_align( &bits );
            bits_write( &bits, 8,   0x05 ); /* tag */
            bits_write( &bits, 24,  55 );
            /* Create decoder specific info for subt */
            Mpeg4SUBTDecoderSpecific_55( &bits );
        }
276
        else if( p_stream->fmt->i_extra > 0 )
277 278 279 280 281
        {
            /* DecoderSpecificInfo */
            bits_align( &bits );
            bits_write( &bits, 8,   0x05 ); /* tag */
            bits_write( &bits, 24, GetDescriptorLength24b(
282 283
                        p_stream->fmt->i_extra ) );
            for (int j = 0; j < p_stream->fmt->i_extra; j++ )
284 285
            {
                bits_write( &bits, 8,
286
                    ((uint8_t*)p_stream->fmt->p_extra)[j] );
287 288
            }
        }
289

290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
        /* fix Decoder length */
        bits_write( &bits_fix_Decoder, 24,
                    GetDescriptorLength24b( bits.i_data -
                    bits_fix_Decoder.i_data - 3 ) );

        /* SLConfigDescriptor : predefined (0x01) */
        bits_align( &bits );
        bits_write( &bits, 8,   0x06 ); /* tag */
        bits_write( &bits, 24,  GetDescriptorLength24b( 8 ) );
        bits_write( &bits, 8,   0x01 );/* predefined */
        bits_write( &bits, 1,   0 );   /* durationFlag */
        bits_write( &bits, 32,  0 );   /* OCRResolution */
        bits_write( &bits, 8,   0 );   /* OCRLength */
        bits_write( &bits, 8,   0 );   /* InstantBitrateLength */
        bits_align( &bits );

        /* fix ESDescr length */
        bits_write( &bits_fix_ESDescr, 24,
                    GetDescriptorLength24b( bits.i_data -
                    bits_fix_ESDescr.i_data - 3 ) );
    }
    bits_align( &bits );
    /* fix IOD length */
    bits_write( &bits_fix_IOD, 24,
                GetDescriptorLength24b(bits.i_data - bits_fix_IOD.i_data - 3 ));

316
    dvbpsi_pmt_descriptor_add(&p_dvbpmt[0], 0x1d, bits.i_data, bits.p_data);
317 318
}

319
static void UpdateServiceType( uint8_t *pi_service_cat, uint8_t *pi_service_type,
320
                               const tsmux_stream_t *p_ts, const es_format_t *fmt )
321 322 323
{
    uint8_t i_type = 0x00;

324
    switch( p_ts->i_stream_type )
325 326 327 328
    {
        case 0x01: /* MPEG1 */
        case 0x02: /* MPEG2 */
        case 0x80:
329
            i_type = 0x01;
330 331 332 333 334 335 336 337 338 339 340 341 342 343
            break;

        case 0x24: /* HEVC */
        case 0x10: /* MPEG4 */
        case 0x1b: /* H264 */
        case 0xA0: /* private */
        case 0xd1: /* dirac */
            i_type = 0x16;
            break;

        default:
            break;
    }

344 345
    if( i_type == 0x01 && fmt->video.i_visible_height > 468 &&
                          fmt->video.i_visible_width > 720 ) /* MPEG2 SD -> HD */
346 347 348
    {
         i_type = 0x11;
    }
349 350
    else if( i_type == 0x16 && fmt->video.i_visible_height > 468 &&
                               fmt->video.i_visible_width > 720 ) /* Advanced codec SD -> HD */
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
    {
         i_type = 0x19;
    }

    if( i_type != 0x00 )
    {
        if( *pi_service_cat != VIDEO_ES || i_type > *pi_service_type )
        {
            *pi_service_type = i_type;
            *pi_service_cat = VIDEO_ES;
        }
        return;
    }

    if( *pi_service_cat != VIDEO_ES ) /* Don't overwrite video */
    {
        /* Not video, try audio */
368
        switch( p_ts->i_stream_type )
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
        {
            case 0x03: /* MPEG1 audio */
            case 0x04: /* MPEG2 audio */
                i_type = 0x02;
                break;

            case 0x06:
            case 0x0f:
            case 0x81:
            case 0x83:
                i_type = 0x0A; /* Avanced codec digital radio */
                break;

            default:
                break;
        }

        if( i_type > *pi_service_type )
            *pi_service_type = i_type;
    }
}

391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
static inline size_t Write_AnnexA_String( uint8_t *p_dest, const char *p_src )
{
    size_t i_src;
    if( p_src == NULL || !(i_src = strlen( p_src )) )
    {
        p_dest[0] = 0;
        return 1;
    }

    bool b_latin = (p_src[0] > 0x20);
    for ( size_t i=0; i< i_src && b_latin; i++ )
        b_latin &= !( p_src[i] & 0x80 );

    if( b_latin )
    {
        i_src = __MIN( i_src, UINT8_MAX );
        p_dest[0] = i_src; /* Total size */
        memcpy( &p_dest[1], p_src, i_src );
        return 1 + i_src;
    }
    else
    {
        i_src = __MIN( i_src, UINT8_MAX - 1 );
        p_dest[0] = 1 + i_src; /* Total size */
        p_dest[1] = 0x15; /* UTF8 Encoding */
        memcpy( &p_dest[2], p_src, i_src );
        return 2 + i_src;
    }
}

421
void BuildPMT( dvbpsi_t *p_dvbpsi, vlc_object_t *p_object,
422
               ts_mux_standard standard,
423 424 425 426
               void *p_opaque, PEStoTSCallback pf_callback,
               int i_tsid, int i_pmt_version_number,
               int i_pcr_pid,
               sdt_psi_t *p_sdt,
427
               unsigned i_programs, tsmux_stream_t *p_pmt, const int *pi_programs_number,
428 429
               unsigned i_mapped_streams, const pes_mapped_stream_t *p_mapped_streams )
{
Thomas Guillem's avatar
Thomas Guillem committed
430
    dvbpsi_pmt_t *dvbpmt = vlc_alloc( i_programs, sizeof(dvbpsi_pmt_t) );
431 432 433
    if( !dvbpmt )
            return;

434
    VLC_UNUSED(standard);
435
    dvbpsi_sdt_t sdtpsi;
436 437
    uint8_t *pi_service_types = NULL;
    uint8_t *pi_service_cats = NULL;
438
    if( p_sdt )
439
    {
440
        dvbpsi_sdt_init( &sdtpsi, 0x42, i_tsid, 1, true, p_sdt->i_netid );
441
        pi_service_types = calloc( i_programs * 2, sizeof *pi_service_types );
442 443 444 445 446 447 448
        if( !pi_service_types )
        {
            free( dvbpmt );
            return;
        }
        pi_service_cats = &pi_service_types[i_programs];
    }
449 450 451

    for (unsigned i = 0; i < i_programs; i++ )
    {
452
        dvbpsi_pmt_init( &dvbpmt[i],
453 454
                        pi_programs_number[i],   /* program number */
                        i_pmt_version_number,
455
                        true,      /* b_current_next */
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
                        i_pcr_pid );
    }

    for (unsigned i = 0; i < i_mapped_streams; i++ )
    {
        const pes_mapped_stream_t *p_stream = &p_mapped_streams[i];
        if( p_stream->pes->i_stream_id == 0xfa ||
            p_stream->pes->i_stream_id == 0xfb ||
            p_stream->pes->i_stream_id == 0xfe )
        {
            /* Has at least 1 MPEG4 stream */
            GetPMTmpeg4( p_object, dvbpmt, i_mapped_streams, p_mapped_streams );
            break;
        }
    }

    for (unsigned i = 0; i < i_mapped_streams; i++ )
    {
        const pes_mapped_stream_t *p_stream = &p_mapped_streams[i];

476
        dvbpsi_pmt_es_t *p_es = dvbpsi_pmt_es_add( &dvbpmt[p_stream->i_mapped_prog],
477
                    p_stream->ts->i_stream_type, p_stream->ts->i_pid );
478 479 480 481 482 483 484 485

        if( p_stream->pes->i_stream_id == 0xfa || p_stream->pes->i_stream_id == 0xfb )
        {
            uint8_t     es_id[2];

            /* SL descriptor */
            es_id[0] = (p_stream->pes->i_es_id >> 8)&0xff;
            es_id[1] = (p_stream->pes->i_es_id)&0xff;
486
            dvbpsi_pmt_es_descriptor_add( p_es, 0x1f, 2, es_id );
487
        }
488
        else if( p_stream->ts->i_stream_type == 0xa0 )
489 490
        {
            uint8_t data[512];
491
            size_t i_extra = __MIN( p_stream->fmt->i_extra, 502 );
492 493

            /* private DIV3 descripor */
494 495 496 497 498
            memcpy( &data[0], &p_stream->fmt->i_codec, 4 );
            data[4] = ( p_stream->fmt->video.i_visible_width >> 8 )&0xff;
            data[5] = ( p_stream->fmt->video.i_visible_width      )&0xff;
            data[6] = ( p_stream->fmt->video.i_visible_height>> 8 )&0xff;
            data[7] = ( p_stream->fmt->video.i_visible_height     )&0xff;
499 500 501 502
            data[8] = ( i_extra >> 8 )&0xff;
            data[9] = ( i_extra      )&0xff;
            if( i_extra > 0 )
            {
503
                memcpy( &data[10], p_stream->fmt->p_extra, i_extra );
504 505 506
            }

            /* 0xa0 is private */
507
            dvbpsi_pmt_es_descriptor_add( p_es, 0xa0, i_extra + 10, data );
508
        }
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
        else if( p_stream->fmt->i_codec == VLC_CODEC_JPEG2000 )
        {
            uint8_t *p_data = calloc( 1, 24 + p_stream->fmt->i_extra );
            if( p_data )
            {
                const int profile = j2k_get_profile( p_stream->fmt->video.i_visible_width,
                                                     p_stream->fmt->video.i_visible_height,
                                                     p_stream->fmt->video.i_frame_rate,
                                                     p_stream->fmt->video.i_frame_rate_base, true );
                p_data[0] = 0x01;
                if( profile < J2K_PROFILE_HD )
                    p_data[1] = 0x01; /* 0x0101 */
                else if( profile < J2K_PROFILE_3G )
                    p_data[1] = 0x02; /* 0x0102 */
                else
                    p_data[1] = 0x04; /* 0x0104 */
                SetDWBE( &p_data[2], p_stream->fmt->video.i_visible_width );
                SetDWBE( &p_data[6], p_stream->fmt->video.i_visible_height );
                SetWBE( &p_data[18], p_stream->fmt->video.i_frame_rate_base );
                SetWBE( &p_data[20], p_stream->fmt->video.i_frame_rate );
                p_data[21] = j2k_get_color_spec( p_stream->fmt->video.primaries,
                                                 p_stream->fmt->video.transfer,
                                                 p_stream->fmt->video.space );
                memcpy( &p_data[24], p_stream->fmt->p_extra, p_stream->fmt->i_extra );
                dvbpsi_pmt_es_descriptor_add( p_es, 0x32, 24 + p_stream->fmt->i_extra, p_data );
                free(p_data);
            }
        }
537
        else if( p_stream->fmt->i_codec == VLC_CODEC_DIRAC )
538 539 540 541
        {
            /* Dirac registration descriptor */

            uint8_t data[4] = { 'd', 'r', 'a', 'c' };
542
            dvbpsi_pmt_es_descriptor_add( p_es, 0x05, 4, data );
543
        }
544
        else if( p_stream->fmt->i_codec == VLC_CODEC_DTS )
545
        {
546
            uint8_t i_ver;
547
            /* DTS registration descriptor (ETSI TS 101 154 Annex F) */
548
            if(vlc_popcount(p_stream->fmt->audio.i_frame_length) == 1)
549
            {
550 551 552 553
                i_ver = ctz( p_stream->fmt->audio.i_frame_length >> 8 );
                if(i_ver == 0 || i_ver > 3)
                   i_ver = 1;
                i_ver += '0';
554
            }
555 556 557
            else i_ver = '0';
            uint8_t data[4] = { 'D', 'T', 'S', i_ver };
            dvbpsi_pmt_es_descriptor_add( p_es, 0x05, 4, data );
558
        }
559
        else if( p_stream->fmt->i_codec == VLC_CODEC_A52 )
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
        {
            uint8_t format[4] = { 'A', 'C', '-', '3'};

            /* "registration" descriptor : "AC-3" */
            dvbpsi_pmt_es_descriptor_add( p_es, 0x05, 4, format );

            if( standard == TS_MUX_STANDARD_ATSC )
            {
                assert(p_stream->ts->i_stream_type == 0x81);
                /* FIXME: ATSC AC-3 audio_stream_descriptor */
                uint8_t data[1] = { 0x00 };
                dvbpsi_pmt_es_descriptor_add( p_es, 0x81, 1, data );
            }
            else
            {
                /* FIXME: DVB AC-3 descriptor */
                uint8_t data[1] = { 0x00 };
                dvbpsi_pmt_es_descriptor_add( p_es, 0x6a, 1, data );
            }
        }
580
        else if( p_stream->fmt->i_codec == VLC_CODEC_EAC3 )
581
        {
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
            uint8_t format[4] = { 'E', 'A', 'C', '3'};

            /* "registration" descriptor : "EAC3" */
            dvbpsi_pmt_es_descriptor_add( p_es, 0x05, 4, format );

            if( standard == TS_MUX_STANDARD_ATSC )
            {
                assert( p_stream->ts->i_stream_type == 0x87 );
                /* FIXME: ATSC EAC3 audio_stream_descriptor */
                uint8_t data[1] = { 0x00 };
                dvbpsi_pmt_es_descriptor_add( p_es, 0xcc, 1, data );
                /* FIXME: ATSC A-71 stream_info_details */
            }
            else
            {
                uint8_t data[1] = { 0x00 };
                dvbpsi_pmt_es_descriptor_add( p_es, 0x7a, 1, data );
            }
600
        }
601
        else if( p_stream->fmt->i_codec == VLC_CODEC_OPUS )
602 603 604 605 606
        {
            uint8_t data[2] = {
                0x80, /* tag extension */
                p_stream->fmt->audio.i_channels
            };
607
            dvbpsi_pmt_es_descriptor_add( p_es, 0x7f, 2, data );
608 609
            uint8_t format[4] = { 'O', 'p', 'u', 's'};
            /* "registration" descriptor : "Opus" */
610
            dvbpsi_pmt_es_descriptor_add( p_es, 0x05, 4, format );
611
        }
612
        else if( p_stream->fmt->i_codec == VLC_CODEC_TELETEXT )
613
        {
614
            if( p_stream->fmt->i_extra )
615
            {
616
                dvbpsi_pmt_es_descriptor_add( p_es, 0x56,
617 618
                                           p_stream->fmt->i_extra,
                                           p_stream->fmt->p_extra );
619 620 621
            }
            continue;
        }
622
        else if( p_stream->fmt->i_codec == VLC_CODEC_DVBS )
623 624
        {
            /* DVB subtitles */
625
            if( p_stream->fmt->i_extra )
626 627
            {
                /* pass-through from the TS demux */
628
                dvbpsi_pmt_es_descriptor_add( p_es, 0x59,
629 630
                                           p_stream->fmt->i_extra,
                                           p_stream->fmt->p_extra );
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
            }
            else
            {
                /* from the dvbsub transcoder */
                dvbpsi_subtitling_dr_t descr;
                dvbpsi_subtitle_t sub;
                dvbpsi_descriptor_t *p_descr;

                memcpy( sub.i_iso6392_language_code, p_stream->pes->lang, 3 );
                sub.i_subtitling_type = 0x10; /* no aspect-ratio criticality */
                sub.i_composition_page_id = p_stream->pes->i_es_id & 0xFF;
                sub.i_ancillary_page_id = p_stream->pes->i_es_id >> 16;

                descr.i_subtitles_number = 1;
                descr.p_subtitle[0] = sub;

                p_descr = dvbpsi_GenSubtitlingDr( &descr, 0 );
                /* Work around bug in old libdvbpsi */ p_descr->i_length = 8;
649
                dvbpsi_pmt_es_descriptor_add( p_es, p_descr->i_tag,
650 651 652 653 654 655 656
                                           p_descr->i_length, p_descr->p_data );
            }
            continue;
        }

        if( p_stream->pes->i_langs )
        {
657
            dvbpsi_pmt_es_descriptor_add( p_es, 0x0a, 4*p_stream->pes->i_langs,
658 659
                p_stream->pes->lang);
        }
660 661 662 663 664

        if( p_sdt )
        {
            UpdateServiceType( &pi_service_cats[p_stream->i_mapped_prog],
                               &pi_service_types[p_stream->i_mapped_prog],
665
                               p_stream->ts, p_stream->fmt );
666
        }
667 668 669 670
    }

    for (unsigned i = 0; i < i_programs; i++ )
    {
671
        dvbpsi_psi_section_t *sect = dvbpsi_pmt_sections_generate( p_dvbpsi, &dvbpmt[i] );
672 673 674 675 676 677 678 679 680 681
        if( likely(sect) )
        {
            block_t *pmt = WritePSISection( sect );
            if( likely(pmt) )
            {
                PEStoTS( p_opaque, pf_callback, pmt, p_pmt[i].i_pid,
                         &p_pmt[i].b_discontinuity, &p_pmt[i].i_continuity_counter );
            }
            dvbpsi_DeletePSISections(sect);
        }
682
        dvbpsi_pmt_empty( &dvbpmt[i] );
683
    }
684
    free( dvbpmt );
685 686 687

    if( p_sdt )
    {
688 689 690 691 692 693 694 695 696 697 698 699
        for (unsigned i = 0; i < i_programs; i++ )
        {
            dvbpsi_sdt_service_t *p_service = dvbpsi_sdt_service_add( &sdtpsi,
                                                                      pi_programs_number[i], /* service id */
                                                                      false,     /* eit schedule */
                                                                      false,     /* eit present */
                                                                      4,         /* running status ("4=RUNNING") */
                                                                      false );   /* free ca */

            const char *psz_sdtprov = p_sdt->desc[i].psz_provider;
            const char *psz_sdtserv = p_sdt->desc[i].psz_service_name;

700 701
            uint8_t p_sdt_desc[4 + 255 * 2];
            size_t i_sdt_desc = 0;
702

703
            /* mapped service type according to es types */
704
            p_sdt_desc[i_sdt_desc++] = pi_service_types[i];
705 706

            /* service provider name length */
707
            i_sdt_desc += Write_AnnexA_String( &p_sdt_desc[i_sdt_desc], psz_sdtprov );
708 709

            /* service name length */
710
            i_sdt_desc += Write_AnnexA_String( &p_sdt_desc[i_sdt_desc], psz_sdtserv );
711

712
            dvbpsi_sdt_service_descriptor_add( p_service, 0x48, i_sdt_desc, p_sdt_desc );
713 714 715
        }
        free( pi_service_types );

716
        dvbpsi_psi_section_t *sect = dvbpsi_sdt_sections_generate( p_dvbpsi, &sdtpsi );
717 718 719 720 721 722 723 724 725 726
        if( likely(sect) )
        {
            block_t *p_sdtblock = WritePSISection( sect );
            if( likely(p_sdtblock) )
            {
                PEStoTS( p_opaque, pf_callback, p_sdtblock, p_sdt->ts.i_pid,
                         &p_sdt->ts.b_discontinuity, &p_sdt->ts.i_continuity_counter );
            }
            dvbpsi_DeletePSISections( sect );
        }
727
        dvbpsi_sdt_empty( &sdtpsi );
728 729 730
    }
}

731
int FillPMTESParams( ts_mux_standard standard, const es_format_t *fmt,
732
                     tsmux_stream_t *ts, pesmux_stream_t *pes )
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
{
    switch( fmt->i_codec )
    {
    /* VIDEO */

    case VLC_CODEC_MPGV:
    case VLC_CODEC_MP2V:
    case VLC_CODEC_MP1V:
        /* TODO: do we need to check MPEG-I/II ? */
        ts->i_stream_type = 0x02;
        pes->i_stream_id = 0xe0;
        break;
    case VLC_CODEC_MP4V:
        ts->i_stream_type = 0x10;
        pes->i_stream_id = 0xe0;
        pes->i_es_id = ts->i_pid;
        break;
    case VLC_CODEC_HEVC:
        ts->i_stream_type = 0x24;
        pes->i_stream_id = 0xe0;
        break;
    case VLC_CODEC_H264:
        ts->i_stream_type = 0x1b;
        pes->i_stream_id = 0xe0;
        break;
    /* XXX dirty dirty but somebody want crapy MS-codec XXX */
    case VLC_CODEC_H263I:
    case VLC_CODEC_H263:
    case VLC_CODEC_WMV3:
    case VLC_CODEC_WMV2:
    case VLC_CODEC_WMV1:
    case VLC_CODEC_DIV3:
    case VLC_CODEC_DIV2:
    case VLC_CODEC_DIV1:
    case VLC_CODEC_MJPG:
        ts->i_stream_type = 0xa0; /* private */
        pes->i_stream_id = 0xa0;   /* beurk */
        break;
    case VLC_CODEC_DIRAC:
        /* stream_id makes use of stream_id_extension */
        pes->i_stream_id = (PES_EXTENDED_STREAM_ID << 8) | 0x60;
        ts->i_stream_type = 0xd1;
        break;
776 777 778 779 780 781 782
    case VLC_CODEC_JPEG2000:
        if( !j2k_is_valid_framerate( fmt->video.i_frame_rate,
                                     fmt->video.i_frame_rate_base ) )
            return VLC_EGENERIC;
        ts->i_stream_type = 0x21;
        pes->i_stream_id = 0xbd;
    break;
783 784 785 786

    /* AUDIO */

    case VLC_CODEC_MPGA:
787
    case VLC_CODEC_MP2:
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
    case VLC_CODEC_MP3:
        ts->i_stream_type = fmt->audio.i_rate >= 32000 ? 0x03 : 0x04;
        pes->i_stream_id = 0xc0;
        break;
    case VLC_CODEC_A52:
        pes->i_stream_id = 0xbd;
        if( standard == TS_MUX_STANDARD_ATSC )
        {
            ts->i_stream_type = 0x81;
        }
        else
        {
            ts->i_stream_type = 0x06;
        }
        break;
    case VLC_CODEC_DVD_LPCM:
        ts->i_stream_type = 0x83;
        pes->i_stream_id = 0xbd;
        break;
    case VLC_CODEC_OPUS:
        if (fmt->audio.i_channels > 8)
            return VLC_EGENERIC;
        pes->i_stream_id = 0xbd;
        pes->i_stream_id = 0x06;
        break;
    case VLC_CODEC_EAC3:
        pes->i_stream_id = 0xbd;
        if( standard == TS_MUX_STANDARD_ATSC )
        {
            /* FIXME: Mandatory EAC3 audio_descriptor */
            ts->i_stream_type = 0x87;
        }
        else
        {
            ts->i_stream_type = 0x06;
        }
        break;
    case VLC_CODEC_DTS:
        if( standard == TS_MUX_STANDARD_ATSC )
        {
            return VLC_EGENERIC;
        }
        else
        {
            ts->i_stream_type = 0x06;
            pes->i_stream_id = 0xbd;
        }
        break;
    case VLC_CODEC_MP4A:
        /* XXX: make that configurable in some way when LOAS
         * is implemented for AAC in TS */
        //ts->i_stream_type = 0x11; /* LOAS/LATM */
        ts->i_stream_type = 0x0f; /* ADTS */
        pes->i_stream_id = 0xc0;
        pes->i_es_id = ts->i_pid;
        break;

    /* TEXT */

    case VLC_CODEC_SPU:
        ts->i_stream_type = 0x82;
        pes->i_stream_id = 0xbd;
        break;
    case VLC_CODEC_SUBT:
        ts->i_stream_type = 0x12;
        pes->i_stream_id = 0xfa;
        pes->i_es_id = ts->i_pid;
        break;
    case VLC_CODEC_DVBS:
        ts->i_stream_type = 0x06;
        pes->i_es_id = fmt->subs.dvb.i_id;
        pes->i_stream_id = 0xbd;
        break;
    case VLC_CODEC_TELETEXT:
        ts->i_stream_type = 0x06;
        pes->i_stream_id = 0xbd; /* FIXME */
        break;

    default:
        return VLC_EGENERIC;
    }

    return VLC_SUCCESS;
}
872 873

#endif