Coverage Report

Created: 2026-01-10 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vlc/modules/demux/mpeg/ps.h
Line
Count
Source
1
/*****************************************************************************
2
 * ps.h: Program Stream demuxer helper
3
 *****************************************************************************
4
 * Copyright (C) 2004-2009 VLC authors and VideoLAN
5
 *
6
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7
 *
8
 * This program is free software; you can redistribute it and/or modify it
9
 * under the terms of the GNU Lesser General Public License as published by
10
 * the Free Software Foundation; either version 2.1 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with this program; if not, write to the Free Software Foundation,
20
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21
 *****************************************************************************/
22
23
#include <assert.h>
24
#include <vlc_demux.h>
25
#include "timestamps.h"
26
27
26.3M
#define PS_STREAM_ID_END_STREAM       0xB9
28
166k
#define PS_STREAM_ID_PACK_HEADER      0xBA
29
5.81k
#define PS_STREAM_ID_SYSTEM_HEADER    0xBB
30
31
588k
#define PS_PACKET_ID_MASK_VOB        0xBD00
32
289k
#define PS_PACKET_ID_MASK_AOB        0xA000
33
188k
#define PS_PACKET_ID_MASK_EXTENDED   0xFD00
34
35
/* 0xBD20 + 0x00 to 0x1f */
36
#define PS_SPU_ID_OFFSET             (PS_PACKET_ID_MASK_VOB | 0x20)
37
38
#define PS_AOB_PACKET_ID_LPCM        (PS_PACKET_ID_MASK_AOB | 0x00)
39
236k
#define PS_AOB_PACKET_ID_MLP         (PS_PACKET_ID_MASK_AOB | 0x01)
40
236k
#define PS_VOB_PACKET_ID_MLP         (PS_PACKET_ID_MASK_VOB | 0xA1)
41
42
enum ps_source {
43
    PS_SOURCE_UNKNOWN, // any PS/PES source
44
    PS_SOURCE_VOB,     // when reading a DVD-Video
45
    PS_SOURCE_AOB,     // when reading a DVD-Audio
46
};
47
48
/* 256-0xC0 for normal stream, 256 for VOB stream, 256 for EVOB stream, 8 for AOB stream */
49
37.8M
#define PS_TK_COUNT (256+256+256+8 - 0xc0)
50
static inline unsigned ps_id_to_tk( unsigned i_id )
51
188k
{
52
188k
    assert(i_id >= 0xc0);
53
188k
    if(unlikely(i_id < 0xc0))
54
0
        return 0;
55
188k
    if( i_id <= 0xff )
56
91.0k
        return i_id - 0xc0;
57
97.3k
    if( (i_id & 0xff00) == PS_PACKET_ID_MASK_VOB )
58
86.8k
        return 256-0xC0 + (i_id & 0xff);
59
10.4k
    if( (i_id & 0xff00) == PS_PACKET_ID_MASK_EXTENDED )
60
5.44k
        return 512-0xc0 + (i_id & 0xff);
61
10.4k
    assert( (i_id & 0xff00) == PS_PACKET_ID_MASK_AOB );
62
5.04k
    return 768-0xc0 + (i_id & 0x07);
63
5.04k
}
64
65
typedef struct ps_psm_t ps_psm_t;
66
static inline uint8_t ps_id_to_type( const ps_psm_t *, uint16_t );
67
static inline const uint8_t *ps_id_to_lang( const ps_psm_t *, uint16_t );
68
69
typedef struct
70
{
71
    bool        b_configured;
72
    bool        b_updated;
73
    int         i_skip;
74
    int         i_id;
75
    int         i_next_block_flags;
76
    es_out_id_t *es;
77
    es_format_t fmt;
78
    vlc_tick_t  i_first_pts;
79
    vlc_tick_t  i_last_pts;
80
81
} ps_track_t;
82
83
/* Init a set of track */
84
static inline void ps_track_init( ps_track_t tk[PS_TK_COUNT] )
85
439
{
86
439
    int i;
87
256k
    for( i = 0; i < PS_TK_COUNT; i++ )
88
256k
    {
89
256k
        tk[i].b_configured = false;
90
256k
        tk[i].b_updated = false;
91
256k
        tk[i].i_skip = 0;
92
256k
        tk[i].i_id   = 0;
93
256k
        tk[i].i_next_block_flags = 0;
94
256k
        tk[i].es     = NULL;
95
256k
        tk[i].i_first_pts = VLC_TICK_INVALID;
96
256k
        tk[i].i_last_pts = VLC_TICK_INVALID;
97
256k
        es_format_Init( &tk[i].fmt, UNKNOWN_ES, 0 );
98
256k
    }
99
439
}
100
101
static inline bool ps_is_H264( const uint8_t *p_data, size_t i_data )
102
74.3k
{
103
74.3k
    const uint8_t startcode[3] = { 0, 0, 1 };
104
74.3k
    int i_flags = 0;
105
106
74.3k
    if( i_data < 9 ||
107
73.8k
       (!memcmp( p_data, startcode, 3 ) &&
108
339
        !memcmp( &p_data[1], startcode, 3 )) )
109
489
        return false;
110
111
    /* Shitty H264 probing. We need a centralized way do to this */
112
1.44M
    while( i_data > 5 )
113
1.39M
    {
114
1.39M
        if( !memcmp( p_data, startcode, 3 ) )
115
25.4k
        {
116
25.4k
            if(p_data[3] == 0x67)
117
2
                i_flags ^= 0x01;
118
25.4k
            else if(p_data[3] == 0x68)
119
2
                i_flags ^= 0x02;
120
25.4k
            else if( p_data[3] & 0x80 )
121
13.7k
                return false;
122
11.6k
            else if( (p_data[3] & 0x1F) > 23 || (p_data[3] & 0x1F) < 1 )
123
10.6k
                return false;
124
1.01k
            else if( (p_data[3] & 0x1F) < 6 )
125
901
                return (i_flags == 0x03);
126
25.4k
        }
127
1.36M
        p_data++;
128
1.36M
        i_data--;
129
1.36M
    }
130
131
48.5k
    return false;
132
73.8k
}
133
134
static inline bool ps_is_EAC3( const uint8_t *p_data, size_t i_data )
135
5.35k
{
136
    /* AC-3 marking, see vlc_a52_header_Parse */
137
5.35k
    if( i_data < 8 || p_data[0] != 0x0b || p_data[1] != 0x77 )
138
4.92k
        return false;
139
432
    int bsid = p_data[5] >> 3;
140
432
    return bsid > 10 && bsid <= 16;
141
5.35k
}
142
143
/* From id fill i_skip and es_format_t */
144
static inline int ps_track_fill( ps_track_t *tk, ps_psm_t *p_psm,
145
                                 int i_id,
146
                                 const uint8_t *p_pkt, size_t i_pkt,
147
                                 bool b_mpeg2only )
148
286k
{
149
286k
    tk->i_skip = 0;
150
286k
    tk->i_id = i_id;
151
152
286k
    if( ( i_id&0xff00 ) == PS_PACKET_ID_MASK_VOB ) /* 0xBD00 -> 0xBDFF, VOB Private Stream 1 */
153
114k
    {
154
114k
        if( ( i_id&0xf8 ) == 0x88 || /* 0x88 -> 0x8f - Can be DTS-HD primary audio in evob */
155
90.7k
            ( i_id&0xf8 ) == 0x98 )  /* 0x98 -> 0x9f - Can be DTS-HD secondary audio in evob */
156
24.5k
        {
157
24.5k
            es_format_Change( &tk->fmt, AUDIO_ES, VLC_CODEC_DTS );
158
24.5k
            tk->i_skip = 4;
159
24.5k
        }
160
90.0k
        else if( ( i_id&0xf8 ) == 0x80 || /* 0x80 -> 0x87 */
161
88.6k
                 ( i_id&0xf0 ) == 0xc0 )  /* 0xc0 -> 0xcf AC-3, Can also be DD+/E-AC3 in evob */
162
5.07k
        {
163
5.07k
            bool b_eac3 = false;
164
5.07k
            if( ( i_id&0xf0 ) == 0xc0 )
165
3.71k
            {
166
3.71k
                if( p_pkt == NULL || i_pkt < 9 )
167
0
                    return VLC_EGENERIC;
168
169
3.71k
                unsigned i_start = 9 + p_pkt[8];
170
3.71k
                if( i_start + 9 < i_pkt )
171
3.32k
                    b_eac3 = ps_is_EAC3( &p_pkt[i_start + 4], i_pkt - i_start - 4 );
172
3.71k
            }
173
174
5.07k
            es_format_Change( &tk->fmt, AUDIO_ES, b_eac3 ? VLC_CODEC_EAC3 : VLC_CODEC_A52 );
175
5.07k
            tk->i_skip = 4;
176
5.07k
        }
177
84.9k
        else if( ( i_id&0xfc ) == 0x00 ) /* 0x00 -> 0x03 */
178
9.31k
        {
179
9.31k
            es_format_Change( &tk->fmt, SPU_ES, VLC_CODEC_CVD );
180
9.31k
        }
181
75.6k
        else if( ( i_id&0xff ) == 0x0b ) /* 0x0b */
182
2.03k
        {
183
2.03k
            bool b_eac3 = i_pkt > 8 && ps_is_EAC3( &p_pkt[9], i_pkt - 9 );
184
2.03k
            es_format_Change( &tk->fmt, AUDIO_ES, b_eac3 ? VLC_CODEC_EAC3 : VLC_CODEC_A52 );
185
2.03k
        }
186
73.5k
        else if( ( i_id&0xff ) == 0x10 ) /* 0x10 */
187
687
        {
188
687
            es_format_Change( &tk->fmt, SPU_ES, VLC_CODEC_TELETEXT );
189
687
        }
190
72.9k
        else if( ( i_id&0xe0 ) == 0x20 ) /* 0x20 -> 0x3f */
191
31.0k
        {
192
31.0k
            es_format_Change( &tk->fmt, SPU_ES, VLC_CODEC_SPU );
193
31.0k
            tk->i_skip = 1;
194
31.0k
        }
195
41.8k
        else if( ( i_id&0xff ) == 0x70 ) /* 0x70 */
196
32.2k
        {
197
32.2k
            es_format_Change( &tk->fmt, SPU_ES, VLC_CODEC_OGT );
198
32.2k
        }
199
9.62k
        else if( ( i_id&0xf0 ) == 0xa0 ) /* 0xa0 -> 0xaf */
200
6.63k
        {
201
6.63k
            es_format_Change( &tk->fmt, AUDIO_ES, VLC_CODEC_DVD_LPCM );
202
6.63k
            tk->i_skip = 1;
203
6.63k
        }
204
2.99k
        else if( ( i_id&0xf0 ) == 0xb0 ) /* 0xb0 -> 0xbf */
205
608
        {
206
608
            es_format_Change( &tk->fmt, AUDIO_ES, VLC_CODEC_TRUEHD );
207
608
            tk->i_skip = 5;
208
608
        }
209
2.38k
        else
210
2.38k
        {
211
2.38k
            es_format_Change( &tk->fmt, UNKNOWN_ES, 0 );
212
2.38k
            return VLC_EGENERIC;
213
2.38k
        }
214
114k
    }
215
172k
    else if( (i_id&0xff00) == PS_PACKET_ID_MASK_EXTENDED ) /* EVOB: 0xFD00 -> 0xFDFF */
216
5.23k
    {
217
5.23k
        uint8_t i_sub_id = i_id & 0xff;
218
5.23k
        if( ( i_sub_id >= 0x55 && i_sub_id <= 0x5f ) || /* Can be primary VC-1 in evob */
219
5.23k
            ( i_sub_id >= 0x75 && i_sub_id <= 0x7f ) )  /* Secondary VC-1 */
220
251
        {
221
251
            es_format_Change( &tk->fmt, VIDEO_ES, VLC_CODEC_VC1 );
222
251
        }
223
4.98k
        else
224
4.98k
        {
225
4.98k
            es_format_Change( &tk->fmt, UNKNOWN_ES, 0 );
226
4.98k
            return VLC_EGENERIC;
227
4.98k
        }
228
5.23k
    }
229
166k
    else if( (i_id&0xff00) == PS_PACKET_ID_MASK_AOB ) /* AOB: 0xA000 -> 0xA0FF */
230
27.3k
    {
231
27.3k
        uint8_t i_sub_id = i_id & 0x07;
232
27.3k
        if( i_sub_id == 0 )
233
27.3k
        {
234
27.3k
            es_format_Change( &tk->fmt, AUDIO_ES, VLC_CODEC_DVDA_LPCM );
235
27.3k
            tk->i_skip = 1;
236
27.3k
        }
237
0
        else if( i_sub_id == 1 )
238
0
        {
239
0
            es_format_Change( &tk->fmt, AUDIO_ES, VLC_CODEC_MLP );
240
0
            tk->i_skip = -1; /* It's a hack for variable skip value */
241
0
        }
242
0
        else
243
0
        {
244
0
            es_format_Change( &tk->fmt, UNKNOWN_ES, 0 );
245
0
            return VLC_EGENERIC;
246
0
        }
247
27.3k
    }
248
139k
    else
249
139k
    {
250
139k
        int i_type = ps_id_to_type( p_psm , i_id );
251
252
139k
        es_format_Change( &tk->fmt, UNKNOWN_ES, 0 );
253
254
139k
        if( (i_id&0xf0) == 0xe0 ) /* 0xe0 -> 0xef */
255
113k
        {
256
113k
            if( i_type == 0x01 )
257
4.88k
            {
258
4.88k
                es_format_Change( &tk->fmt, VIDEO_ES, VLC_CODEC_MPGV );
259
4.88k
                tk->fmt.i_original_fourcc = VLC_CODEC_MP1V;
260
4.88k
            }
261
108k
            else if( i_type == 0x02 )
262
0
            {
263
0
                es_format_Change( &tk->fmt, VIDEO_ES, VLC_CODEC_MPGV );
264
0
            }
265
108k
            else if( i_type == 0x10 )
266
668
            {
267
668
                es_format_Change( &tk->fmt, VIDEO_ES, VLC_CODEC_MP4V );
268
668
            }
269
107k
            else if( i_type == 0x1b )
270
34
            {
271
34
                es_format_Change( &tk->fmt, VIDEO_ES, VLC_CODEC_H264 );
272
34
            }
273
107k
            else if( i_type == 0x24 )
274
2.12k
            {
275
2.12k
                es_format_Change( &tk->fmt, VIDEO_ES, VLC_CODEC_HEVC );
276
2.12k
            }
277
105k
            else if( i_id == 0xe2 || /* Primary H.264 in evob */
278
80.8k
                     i_id == 0xe3 )  /* Secondary H.264 in evob */
279
26.6k
            {
280
26.6k
                es_format_Change( &tk->fmt, VIDEO_ES, VLC_CODEC_H264 );
281
26.6k
            }
282
78.9k
            else if( p_pkt && i_type == 0x00 && /* Not from PSM */
283
78.8k
                     i_pkt > 9 + 5 &&
284
74.4k
                     i_pkt > 9U + 5 + p_pkt[8] &&
285
74.3k
                     ps_is_H264( &p_pkt[ 9 + p_pkt[8] ],
286
74.3k
                                  i_pkt - 9 - p_pkt[8] ) )
287
0
            {
288
0
                es_format_Change( &tk->fmt, VIDEO_ES, VLC_CODEC_H264 );
289
0
            }
290
78.9k
            else if( tk->fmt.i_cat == UNKNOWN_ES &&
291
78.9k
                     ( p_pkt != NULL /* Not system */ || b_mpeg2only ) )
292
78.8k
            {
293
78.8k
                es_format_Change( &tk->fmt, VIDEO_ES, VLC_CODEC_MPGV );
294
78.8k
            }
295
113k
        }
296
26.3k
        else if( ( i_id&0xe0 ) == 0xc0 ) /* 0xc0 -> 0xdf */
297
14.1k
        {
298
14.1k
            if( i_type == 0x03 ||
299
14.1k
                i_type == 0x04 )
300
0
            {
301
0
                es_format_Change( &tk->fmt, AUDIO_ES, VLC_CODEC_MPGA );
302
0
            }
303
14.1k
            else if( i_type == 0x0f )
304
656
            {
305
656
                es_format_Change( &tk->fmt, AUDIO_ES, VLC_CODEC_MP4A );
306
656
                tk->fmt.i_original_fourcc = VLC_FOURCC('A','D','T','S');
307
656
            }
308
13.5k
            else if( i_type == 0x11 )
309
702
            {
310
702
                es_format_Change( &tk->fmt, AUDIO_ES, VLC_CODEC_MP4A );
311
702
                tk->fmt.i_original_fourcc = VLC_FOURCC('L','A','T','M');
312
702
            }
313
12.8k
            else if( i_type == 0x2d )
314
639
            {
315
639
                es_format_Change( &tk->fmt, AUDIO_ES, VLC_CODEC_MPEGH );
316
639
            }
317
12.1k
            else if( tk->fmt.i_cat == UNKNOWN_ES )
318
12.1k
            {
319
12.1k
                es_format_Change( &tk->fmt, AUDIO_ES, VLC_CODEC_MPGA );
320
12.1k
            }
321
14.1k
        }
322
12.1k
        else if( tk->fmt.i_cat == UNKNOWN_ES ) return VLC_EGENERIC;
323
139k
    }
324
325
    /* PES packets usually contain truncated frames */
326
267k
    tk->fmt.b_packetized = false;
327
267k
    tk->fmt.i_priority = ~i_id & 0x0F;
328
329
267k
    if( ps_id_to_lang( p_psm, i_id ) )
330
9.75k
    {
331
9.75k
        tk->fmt.psz_language = malloc( 4 );
332
9.75k
        if( tk->fmt.psz_language )
333
9.75k
        {
334
9.75k
            memcpy( tk->fmt.psz_language, ps_id_to_lang( p_psm , i_id ), 3 );
335
9.75k
            tk->fmt.psz_language[3] = 0;
336
9.75k
        }
337
9.75k
    }
338
339
267k
    return (tk->fmt.i_cat != UNKNOWN_ES || p_pkt) ? VLC_SUCCESS : VLC_EGENERIC;
340
286k
}
341
342
/* return the id of a PES (should be valid) */
343
static inline int ps_pkt_id( const uint8_t *p_pkt, size_t i_pkt, enum ps_source source )
344
204k
{
345
204k
    if(unlikely(i_pkt < 4))
346
0
        return 0;
347
204k
    if( p_pkt[3] == STREAM_ID_PRIVATE_STREAM_1 )
348
91.8k
    {
349
91.8k
        uint8_t i_sub_id = 0;
350
91.8k
        if( i_pkt >= 9 &&
351
88.7k
            i_pkt > 9 + (size_t)p_pkt[8] )
352
77.0k
        {
353
77.0k
            const unsigned i_start = 9 + p_pkt[8];
354
77.0k
            i_sub_id = p_pkt[i_start];
355
356
77.0k
            if( i_sub_id == 0xa0 &&
357
5.63k
                i_pkt >= i_start + 7 &&
358
5.14k
                p_pkt[i_start + 6] != 0x80 )
359
5.04k
            {
360
                /* AOB LPCM extension */
361
5.04k
                return PS_PACKET_ID_MASK_AOB | (i_sub_id & 0x01);
362
5.04k
            }
363
364
72.0k
            if( i_sub_id == 0xa1 &&
365
750
                source == PS_SOURCE_AOB )
366
0
            {
367
                /* AOB MLP extension */
368
0
                return PS_PACKET_ID_MASK_AOB | (i_sub_id & 0x01);
369
0
            }
370
72.0k
        }
371
372
        /* VOB extension */
373
86.8k
        return PS_PACKET_ID_MASK_VOB | i_sub_id;
374
91.8k
    }
375
112k
    if( i_pkt >= 9 &&
376
106k
             p_pkt[3] == STREAM_ID_EXTENDED_STREAM_ID &&
377
14.6k
             (p_pkt[6]&0xC0) == 0x80 &&   /* mpeg2 */
378
8.70k
             (p_pkt[7]&0x01) == 0x01 )    /* extension_flag */
379
8.36k
    {
380
        /* ISO 13818 amendment 2 and SMPTE RP 227 */
381
8.36k
        const uint8_t i_flags = p_pkt[7];
382
8.36k
        unsigned int i_skip = 9;
383
384
        /* Find PES extension */
385
8.36k
        if( (i_flags & 0x80 ) )
386
668
        {
387
668
            i_skip += 5;        /* pts */
388
668
            if( (i_flags & 0x40) )
389
652
                i_skip += 5;    /* dts */
390
668
        }
391
8.36k
        if( (i_flags & 0x20 ) )
392
1.35k
            i_skip += 6;
393
8.36k
        if( (i_flags & 0x10 ) )
394
652
            i_skip += 3;
395
8.36k
        if( (i_flags & 0x08 ) )
396
2.63k
            i_skip += 1;
397
8.36k
        if( (i_flags & 0x04 ) )
398
1.10k
            i_skip += 1;
399
8.36k
        if( (i_flags & 0x02 ) )
400
668
            i_skip += 2;
401
402
8.36k
        if( i_skip < i_pkt && (p_pkt[i_skip]&0x01) )
403
7.27k
        {
404
7.27k
            const uint8_t i_flags2 = p_pkt[i_skip];
405
406
            /* Find PES extension 2 */
407
7.27k
            i_skip += 1;
408
7.27k
            if( i_flags2 & 0x80 )
409
67
                i_skip += 16;
410
7.27k
            if( (i_flags2 & 0x40) && i_skip < i_pkt )
411
1.32k
                i_skip += 1 + p_pkt[i_skip];
412
7.27k
            if( i_flags2 & 0x20 )
413
1.31k
                i_skip += 2;
414
7.27k
            if( i_flags2 & 0x10 )
415
1.29k
                i_skip += 2;
416
417
7.27k
            if( i_skip + 1 < i_pkt )
418
5.66k
            {
419
5.66k
                const int i_extension_field_length = p_pkt[i_skip]&0x7f;
420
5.66k
                if( i_extension_field_length >=1 )
421
5.41k
                {
422
5.41k
                    int i_stream_id_extension_flag = (p_pkt[i_skip+1] >> 7)&0x1;
423
5.41k
                    if( i_stream_id_extension_flag == 0 )
424
5.18k
                        return PS_PACKET_ID_MASK_EXTENDED | (p_pkt[i_skip+1]&0x7f);
425
5.41k
                }
426
5.66k
            }
427
7.27k
        }
428
8.36k
    }
429
107k
    return p_pkt[3];
430
112k
}
431
432
/* return the size of the next packet */
433
static inline int ps_pkt_size( const uint8_t *p, int i_peek )
434
271k
{
435
271k
    if( unlikely(i_peek < 4) )
436
0
        return -1;
437
438
271k
    switch( p[3] )
439
271k
    {
440
2.12k
        case PS_STREAM_ID_END_STREAM:
441
2.12k
            return 4;
442
443
1.16k
        case PS_STREAM_ID_PACK_HEADER:
444
1.16k
            if( i_peek > 4 )
445
1.16k
            {
446
1.16k
                if( i_peek >= 14 && (p[4] >> 6) == 0x01 )
447
283
                    return 14 + (p[13]&0x07);
448
877
                if( i_peek >= 12 && (p[4] >> 4) == 0x02 )
449
869
                    return 12;
450
877
            }
451
8
            break;
452
453
3.68k
        case PS_STREAM_ID_SYSTEM_HEADER:
454
83.5k
        case STREAM_ID_PROGRAM_STREAM_MAP:
455
83.7k
        case STREAM_ID_PROGRAM_STREAM_DIRECTORY:
456
268k
        default:
457
268k
            if( i_peek >= 6 )
458
268k
                return 6 + ((p[4]<<8) | p[5] );
459
271k
    }
460
16
    return -1;
461
271k
}
462
463
/* parse a PACK PES */
464
static inline int ps_pkt_parse_pack( const uint8_t *p_pkt, size_t i_pkt,
465
                                     vlc_tick_t *pi_scr, int *pi_mux_rate )
466
714
{
467
714
    const uint8_t *p = p_pkt;
468
714
    ts_90khz_t i_scr;
469
714
    if( i_pkt >= 14 && (p[4] >> 6) == 0x01 ) /* 0b01 H.222 MPEG-2 Pack Header */
470
183
    {
471
183
        i_scr = ExtractPackHeaderTimestamp( &p[4] );
472
183
        *pi_mux_rate = ( p[10] << 14 )|( p[11] << 6 )|( p[12] >> 2);
473
183
    }
474
531
    else if( i_pkt >= 12 && (p[4] >> 4) == 0x02 ) /* 0b0010 ISO 11172-1 MPEG-1 Pack Header */
475
531
    {
476
531
        if(!ExtractPESTimestamp( &p[4], 0x02, &i_scr )) /* same bits as PES/PTS */
477
274
            return VLC_EGENERIC;
478
257
        *pi_mux_rate = ( ( p[9]&0x7f )<< 15 )|( p[10] << 7 )|( p[11] >> 1);
479
257
    }
480
0
    else
481
0
    {
482
0
        return VLC_EGENERIC;
483
0
    }
484
440
    *pi_scr = FROM_SCALE( i_scr );
485
440
    return VLC_SUCCESS;
486
714
}
487
488
/* Parse a SYSTEM PES */
489
static inline int ps_pkt_parse_system( const uint8_t *p_pkt, size_t i_pkt,
490
                                       ps_psm_t *p_psm,
491
                                       ps_track_t tk[PS_TK_COUNT] )
492
2.13k
{
493
2.13k
    const uint8_t *p = &p_pkt[6 + 3 + 1 + 1 + 1];
494
2.13k
    const uint8_t *p_pktend = &p_pkt[i_pkt];
495
496
    /* System header is not usable if it references private streams (0xBD)
497
     * or 'all audio streams' (0xB8) or 'all video streams' (0xB9) */
498
11.0k
    while( p < p_pktend && (p[0] & 0x80) )
499
9.30k
    {
500
9.30k
        int i_id = p[0];
501
9.30k
        switch( i_id )
502
9.30k
        {
503
265
            case 0xB7:
504
265
                if( p_pktend - p < 6 )
505
0
                    return VLC_EGENERIC;
506
265
                i_id = PS_PACKET_ID_MASK_EXTENDED | (p[2] & 0x7F);
507
265
                p += 6;
508
265
                break;
509
9.03k
            default:
510
9.03k
                if( p_pktend - p < 3 )
511
384
                    return VLC_EGENERIC;
512
8.65k
                p += 3;
513
8.65k
                break;
514
9.30k
        }
515
516
8.91k
        if( i_id < 0xc0 )
517
2.07k
            continue;
518
519
6.84k
        unsigned i_tk = ps_id_to_tk( i_id );
520
6.84k
        if( !tk[i_tk].b_configured )
521
6.60k
            ps_track_fill( &tk[i_tk], p_psm, i_id, NULL, 0, false );
522
6.84k
    }
523
1.75k
    return VLC_SUCCESS;
524
2.13k
}
525
526
/* Parse a PES (and skip i_skip_extra in the payload) */
527
static inline int ps_pkt_parse_pes( vlc_object_t *p_object, block_t *p_pes, int i_skip_extra )
528
166k
{
529
166k
    unsigned int i_skip;
530
166k
    ts_pes_header_t pesh;
531
166k
    ts_pes_header_init( &pesh );
532
533
166k
    if( ParsePESHeader( p_object->logger, p_pes->p_buffer, p_pes->i_buffer, &pesh ) != VLC_SUCCESS )
534
59.9k
        return VLC_EGENERIC;
535
536
106k
    i_skip = pesh.i_size;
537
538
106k
    if( pesh.b_scrambling )
539
3.89k
        p_pes->i_flags |= BLOCK_FLAG_SCRAMBLED;
540
541
106k
    if( i_skip_extra >= 0 )
542
106k
        i_skip += i_skip_extra;
543
0
    else if( p_pes->i_buffer > i_skip + 3 &&
544
0
             ( ps_pkt_id( p_pes->p_buffer, p_pes->i_buffer, PS_SOURCE_AOB ) == PS_AOB_PACKET_ID_MLP ||
545
0
               ps_pkt_id( p_pes->p_buffer, p_pes->i_buffer, PS_SOURCE_VOB ) == PS_VOB_PACKET_ID_MLP ) )
546
0
        i_skip += 4 + p_pes->p_buffer[i_skip+3];
547
548
106k
    if( p_pes->i_buffer <= i_skip )
549
11.1k
    {
550
11.1k
        return VLC_EGENERIC;
551
11.1k
    }
552
553
95.7k
    p_pes->p_buffer += i_skip;
554
95.7k
    p_pes->i_buffer -= i_skip;
555
556
95.7k
    if( pesh.i_pts != TS_90KHZ_INVALID )
557
39.6k
    {
558
39.6k
        p_pes->i_pts = FROM_SCALE( pesh.i_pts );
559
39.6k
        if( pesh.i_dts != TS_90KHZ_INVALID )
560
777
            p_pes->i_dts = FROM_SCALE( pesh.i_dts );
561
38.8k
        else /* ISO/IEC 13818-1 2.7.5: if pts and no dts, then dts == pts */
562
38.8k
            p_pes->i_dts = p_pes->i_pts;
563
39.6k
    }
564
565
95.7k
    return VLC_SUCCESS;
566
106k
}
567
568
typedef struct
569
{
570
    /* Language is iso639-2T */
571
    uint8_t lang[3];
572
} ps_descriptors_t;
573
574
/* Program stream map handling */
575
typedef struct ps_es_t
576
{
577
    uint8_t i_type;
578
    uint16_t i_id;
579
580
    ps_descriptors_t desc;
581
582
} ps_es_t;
583
584
struct ps_psm_t
585
{
586
    uint8_t i_version;
587
588
    size_t  i_es;
589
    ps_es_t *es;
590
591
    ps_descriptors_t uniqueextdesc;
592
};
593
594
static inline uint8_t ps_id_to_type( const ps_psm_t *p_psm, uint16_t i_id )
595
139k
{
596
139k
    size_t i;
597
311k
    for( i = 0; p_psm && i < p_psm->i_es; i++ )
598
181k
    {
599
181k
        if( p_psm->es[i].i_id == i_id ) return p_psm->es[i].i_type;
600
181k
    }
601
129k
    return 0;
602
139k
}
603
604
static inline const uint8_t *ps_id_to_lang( const ps_psm_t *p_psm, uint16_t i_id )
605
276k
{
606
276k
    size_t i;
607
635k
    for( i = 0; p_psm && i < p_psm->i_es; i++ )
608
377k
    {
609
377k
        if( p_psm->es[i].i_id == i_id )
610
19.5k
            return p_psm->es[i].desc.lang;
611
377k
    }
612
257k
    return 0;
613
276k
}
614
615
static inline void ps_psm_init( ps_psm_t *p_psm )
616
439
{
617
439
    p_psm->i_version = 0xFF;
618
439
    p_psm->i_es = 0;
619
439
    p_psm->es = 0;
620
439
    memset( &p_psm->uniqueextdesc, 0, 3 );
621
439
}
622
623
static inline void ps_psm_destroy( ps_psm_t *p_psm )
624
43.6k
{
625
43.6k
    free( p_psm->es );
626
43.6k
    p_psm->es = NULL;
627
43.6k
    p_psm->i_es = 0;
628
43.6k
}
629
630
static inline void ps_parse_descriptors( const uint8_t *p_data, size_t i_data,
631
                                        ps_descriptors_t *p_desc )
632
33.7k
{
633
74.8k
    while( i_data > 3 && i_data > 2u + p_data[1] )
634
41.1k
    {
635
41.1k
        switch( p_data[0] )
636
41.1k
        {
637
1.05k
            case 0x0A: /* ISO_639_language_descriptor */
638
1.05k
                if( i_data >= 6 )
639
664
                    memcpy( p_desc->lang, &p_data[2], 3 );
640
1.05k
                break;
641
642
40.0k
            default:
643
40.0k
                break;
644
41.1k
        }
645
41.1k
        uint8_t i_desc_size = p_data[1];
646
41.1k
        p_data += 2 + i_desc_size;
647
41.1k
        i_data -= 2 + i_desc_size;
648
41.1k
    }
649
33.7k
}
650
651
static inline int ps_psm_fill( ps_psm_t *p_psm,
652
                               const uint8_t *p_buffer, size_t i_pkt,
653
                               ps_track_t tk[PS_TK_COUNT])
654
59.8k
{
655
59.8k
    size_t i_length, i_info_length, i_es_base;
656
59.8k
    uint8_t i_version;
657
59.8k
    bool b_single_extension;
658
659
    // Demux() checks that we have at least 4 bytes, but we need
660
    // at least 10 to read up to the info_length field
661
59.8k
    assert(i_pkt >= 4);
662
59.8k
    if( !p_psm || i_pkt < 10 || p_buffer[3] != STREAM_ID_PROGRAM_STREAM_MAP)
663
127
        return VLC_EGENERIC;
664
665
59.7k
    i_length = GetWBE(&p_buffer[4]) + 6;
666
59.7k
    if( i_length > i_pkt ) return VLC_EGENERIC;
667
668
59.7k
    if((p_buffer[6] & 0x80) == 0) /* current_next_indicator */
669
1.39k
        return VLC_EGENERIC;
670
671
58.3k
    b_single_extension = p_buffer[6] & 0x40;
672
58.3k
    i_version = (p_buffer[6] & 0xf8);
673
674
58.3k
    if( p_psm->i_version == i_version ) return VLC_EGENERIC;
675
676
43.2k
    ps_psm_destroy( p_psm );
677
678
43.2k
    i_info_length = GetWBE(&p_buffer[8]);
679
43.2k
    if( i_info_length + 10 > i_length )
680
994
        return VLC_EGENERIC;
681
682
    /* Elementary stream map */
683
    /* int i_esm_length = (uint16_t)(p_buffer[ 10 + i_info_length ] << 8) +
684
        p_buffer[ 11 + i_info_length]; */
685
42.2k
    i_es_base = 12 + i_info_length;
686
687
75.9k
    while( i_es_base + 4 < i_length )
688
59.0k
    {
689
59.0k
        ps_es_t *tmp_es = realloc( p_psm->es, sizeof(ps_es_t) * (p_psm->i_es+1) );
690
59.0k
        if( tmp_es == NULL )
691
0
            break;
692
59.0k
        p_psm->es = tmp_es;
693
694
59.0k
        ps_es_t *p_es = &p_psm->es[ p_psm->i_es++ ];
695
59.0k
        p_es->i_type = p_buffer[ i_es_base  ];
696
59.0k
        p_es->i_id = p_buffer[ i_es_base + 1 ];
697
698
59.0k
        i_info_length = GetWBE(&p_buffer[ i_es_base + 2 ]);
699
700
59.0k
        if( i_es_base + 4 + i_info_length > i_length )
701
25.0k
            break;
702
703
        /* TODO Add support for VC-1 stream:
704
         *      stream_type=0xea, stream_id=0xfd AND registration
705
         *      descriptor 0x5 with format_identifier == 0x56432D31 (VC-1)
706
         *      (I need a sample that use PSM with VC-1) */
707
708
34.0k
        if( p_es->i_id == STREAM_ID_EXTENDED_STREAM_ID && b_single_extension == 0 )
709
10.0k
        {
710
10.0k
            if( i_info_length < 3 )
711
326
                break;
712
9.70k
            p_es->i_id = (p_es->i_id << 8) | (p_buffer[i_es_base + 6] & 0x7F);
713
9.70k
            ps_parse_descriptors( &p_buffer[i_es_base + 4 + 3],
714
9.70k
                                  i_info_length - 3,
715
9.70k
                                  &p_psm->uniqueextdesc );
716
9.70k
        }
717
23.9k
        else
718
23.9k
        {
719
23.9k
            ps_parse_descriptors( &p_buffer[i_es_base + 4],
720
23.9k
                                  i_info_length, &p_es->desc );
721
23.9k
        }
722
723
33.7k
        i_es_base += 4 + i_info_length;
724
33.7k
    }
725
726
    /* TODO: CRC */
727
728
42.2k
    p_psm->i_version = i_version;
729
730
    /* Check/Modify our existing tracks */
731
24.6M
    for( int i = 0; i < PS_TK_COUNT; i++ )
732
24.6M
    {
733
24.6M
        if( !tk[i].b_configured )
734
24.3M
            continue;
735
736
263k
        ps_track_t tk_tmp;
737
263k
        es_format_Init( &tk_tmp.fmt, UNKNOWN_ES, 0 );
738
739
263k
        if( ps_track_fill( &tk_tmp, p_psm, tk[i].i_id,
740
263k
                           p_buffer, i_pkt, false ) != VLC_SUCCESS )
741
0
            continue;
742
743
263k
        if( tk_tmp.fmt.i_codec == tk[i].fmt.i_codec )
744
243k
        {
745
243k
            es_format_Clean( &tk_tmp.fmt );
746
243k
            continue;
747
243k
        }
748
749
        /* replace with new version */
750
19.6k
        tk_tmp.b_configured = true;
751
19.6k
        tk_tmp.b_updated = true;
752
19.6k
        tk_tmp.es = tk[i].es;
753
19.6k
        es_format_Clean( &tk[i].fmt );
754
19.6k
        tk[i] = tk_tmp;
755
19.6k
    }
756
757
42.2k
    return VLC_SUCCESS;
758
43.2k
}