Coverage Report

Created: 2026-08-14 07:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vlc/modules/packetizer/dts.c
Line
Count
Source
1
/*****************************************************************************
2
 * dts.c: parse DTS audio sync info and packetize the stream
3
 *****************************************************************************
4
 * Copyright (C) 2001-2016 VLC authors and VideoLAN
5
 *
6
 * Authors: Gildas Bazin <gbazin@videolan.org>
7
 *          Thomas Guillem <thomas@gllm.fr>
8
 *
9
 * This program is free software; you can redistribute it and/or modify it
10
 * under the terms of the GNU Lesser General Public License as published by
11
 * the Free Software Foundation; either version 2.1 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU Lesser General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public License
20
 * along with this program; if not, write to the Free Software Foundation,
21
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22
 *****************************************************************************/
23
24
/*****************************************************************************
25
 * Preamble
26
 *****************************************************************************/
27
#ifdef HAVE_CONFIG_H
28
# include "config.h"
29
#endif
30
31
#include <stdbit.h>
32
33
#include <vlc_common.h>
34
#include <vlc_plugin.h>
35
#include <vlc_codec.h>
36
#include <vlc_block_helper.h>
37
#include <vlc_modules.h>
38
39
#include "dts_header.h"
40
41
#include "packetizer_helper.h"
42
43
static int  Open( vlc_object_t * );
44
static void Close( vlc_object_t * );
45
46
172
vlc_module_begin ()
47
86
    set_subcategory( SUBCAT_SOUT_PACKETIZER )
48
86
    set_description( N_("DTS audio packetizer") )
49
86
    set_capability( "audio packetizer", 10 )
50
172
    set_callbacks( Open, Close )
51
86
vlc_module_end ()
52
53
typedef struct
54
{
55
    /*
56
     * Input properties
57
     */
58
    int i_state;
59
60
    block_bytestream_t bytestream;
61
    size_t i_next_offset;
62
63
    /*
64
     * Common properties
65
     */
66
    date_t  end_date;
67
    bool    b_date_set;
68
69
    vlc_tick_t i_pts;
70
    bool    b_discontinuity;
71
72
    vlc_dts_header_t first, second;
73
    size_t  i_input_size;
74
} decoder_sys_t;
75
76
enum
77
{
78
    STATE_SYNC_SUBSTREAM_EXTENSIONS = STATE_CUSTOM_FIRST,
79
    STATE_NEXT_SYNC_SUBSTREAM_EXTENSIONS,
80
};
81
82
static void PacketizeFlush( decoder_t *p_dec )
83
2.29k
{
84
2.29k
    decoder_sys_t *p_sys = p_dec->p_sys;
85
86
2.29k
    p_sys->b_discontinuity = true;
87
2.29k
    date_Set( &p_sys->end_date, VLC_TICK_INVALID );
88
2.29k
    p_sys->i_state = STATE_NOSYNC;
89
2.29k
    block_BytestreamEmpty( &p_sys->bytestream );
90
2.29k
}
91
92
static block_t *GetOutBuffer( decoder_t *p_dec )
93
240k
{
94
240k
    decoder_sys_t *p_sys = p_dec->p_sys;
95
240k
    if (p_sys->i_input_size == 0)
96
0
        return NULL;
97
98
240k
    if( !p_sys->b_date_set
99
239k
     || p_dec->fmt_out.audio.i_rate != p_sys->first.i_rate )
100
2.82k
    {
101
2.82k
        msg_Dbg( p_dec, "DTS samplerate:%d bitrate:%d",
102
2.82k
                 p_sys->first.i_rate, p_sys->first.i_bitrate );
103
104
2.82k
        date_Init( &p_sys->end_date, p_sys->first.i_rate, 1 );
105
2.82k
        date_Set( &p_sys->end_date, p_sys->i_pts );
106
2.82k
        p_sys->b_date_set = true;
107
2.82k
    }
108
109
240k
    p_dec->fmt_out.audio.i_rate     = p_sys->first.i_rate;
110
240k
    if( p_dec->fmt_out.audio.i_bytes_per_frame < p_sys->first.i_frame_size )
111
1.32k
        p_dec->fmt_out.audio.i_bytes_per_frame = p_sys->first.i_frame_size;
112
240k
    p_dec->fmt_out.audio.i_frame_length = p_sys->first.i_frame_length;
113
114
240k
    p_dec->fmt_out.audio.i_chan_mode = p_sys->first.i_chan_mode;
115
240k
    p_dec->fmt_out.audio.i_physical_channels = p_sys->first.i_physical_channels;
116
240k
    p_dec->fmt_out.audio.i_channels =
117
240k
        stdc_count_ones( p_dec->fmt_out.audio.i_physical_channels );
118
119
240k
    p_dec->fmt_out.i_bitrate = p_sys->first.i_bitrate;
120
121
240k
    block_t *p_block = block_Alloc( p_sys->i_input_size );
122
240k
    if( p_block == NULL )
123
0
        return NULL;
124
125
240k
    p_block->i_nb_samples = p_sys->first.i_frame_length;
126
240k
    p_block->i_pts = p_block->i_dts = date_Get( &p_sys->end_date );
127
240k
    p_block->i_length =
128
240k
        date_Increment( &p_sys->end_date, p_block->i_nb_samples ) - p_block->i_pts;
129
240k
    return p_block;
130
240k
}
131
132
static block_t *PacketizeBlock( decoder_t *p_dec, block_t **pp_block )
133
4.23M
{
134
4.23M
    decoder_sys_t *p_sys = p_dec->p_sys;
135
4.23M
    uint8_t p_header[VLC_DTS_HEADER_SIZE];
136
4.23M
    block_t *p_out_buffer;
137
138
4.23M
    block_t *p_block = pp_block ? *pp_block : NULL;
139
140
4.23M
    if( p_block )
141
4.22M
    {
142
4.22M
        if ( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) ) {
143
            /* First always drain complete blocks before discontinuity */
144
2.45k
            block_t *p_drain = PacketizeBlock( p_dec, NULL );
145
2.45k
            if(p_drain)
146
160
                return p_drain;
147
148
2.29k
            PacketizeFlush( p_dec );
149
150
2.29k
            if ( p_block->i_flags & BLOCK_FLAG_CORRUPTED ) {
151
191
                block_Release( p_block );
152
191
                return NULL;
153
191
            }
154
2.29k
        }
155
156
4.22M
        if ( p_block->i_pts == VLC_TICK_INVALID &&
157
193k
             date_Get( &p_sys->end_date ) == VLC_TICK_INVALID ) {
158
            /* We've just started the stream, wait for the first PTS. */
159
7.17k
            block_Release( p_block );
160
7.17k
            return NULL;
161
7.17k
        }
162
163
4.21M
        block_BytestreamPush( &p_sys->bytestream, p_block );
164
4.21M
    }
165
166
7.29M
    while( 1 )
167
7.29M
    {
168
7.29M
        switch( p_sys->i_state )
169
7.29M
        {
170
3.08M
        case STATE_NOSYNC:
171
67.0M
            while( block_PeekBytes( &p_sys->bytestream, p_header, 6 )
172
67.0M
                   == VLC_SUCCESS )
173
66.0M
            {
174
66.0M
                if( vlc_dts_header_IsSync( p_header, 6 ) )
175
2.01M
                {
176
2.01M
                    p_sys->i_state = STATE_SYNC;
177
2.01M
                    break;
178
2.01M
                }
179
63.9M
                (void) block_SkipByte( &p_sys->bytestream );
180
63.9M
            }
181
3.08M
            if( p_sys->i_state != STATE_SYNC )
182
1.07M
            {
183
1.07M
                block_BytestreamFlush( &p_sys->bytestream );
184
185
                /* Need more data */
186
1.07M
                return NULL;
187
1.07M
            }
188
            /* fallthrough */
189
190
2.01M
        case STATE_SYNC:
191
            /* New frame, set the Presentation Time Stamp */
192
2.01M
            p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
193
2.01M
            if( p_sys->i_pts != VLC_TICK_INVALID &&
194
389k
                p_sys->i_pts != date_Get( &p_sys->end_date ) )
195
115k
            {
196
115k
                date_Set( &p_sys->end_date, p_sys->i_pts );
197
115k
            }
198
2.01M
            p_sys->i_state = STATE_HEADER;
199
            /* fallthrough */
200
201
2.04M
        case STATE_HEADER:
202
            /* Get DTS frame header (VLC_DTS_HEADER_SIZE bytes) */
203
2.04M
            if( block_PeekBytes( &p_sys->bytestream, p_header,
204
2.04M
                                 VLC_DTS_HEADER_SIZE ) != VLC_SUCCESS )
205
29.1k
            {
206
                /* Need more data */
207
29.1k
                return NULL;
208
29.1k
            }
209
210
            /* Check if frame is valid and get frame info */
211
2.01M
            if( vlc_dts_header_Parse( &p_sys->first, p_header,
212
2.01M
                                      VLC_DTS_HEADER_SIZE ) != VLC_SUCCESS
213
1.24M
             || p_sys->first.i_frame_size == 0 )
214
1.31M
            {
215
1.31M
                msg_Dbg( p_dec, "emulated sync word" );
216
1.31M
                block_SkipByte( &p_sys->bytestream );
217
1.31M
                p_sys->i_state = STATE_NOSYNC;
218
1.31M
                break;
219
1.31M
            }
220
221
699k
            if( p_sys->first.syncword == DTS_SYNC_SUBSTREAM )
222
580k
                p_sys->i_state = STATE_SYNC_SUBSTREAM_EXTENSIONS;
223
118k
            else
224
118k
                p_sys->i_state = STATE_NEXT_SYNC;
225
699k
            p_sys->i_input_size = p_sys->i_next_offset = p_sys->first.i_frame_size;
226
699k
            break;
227
228
1.23M
        case STATE_SYNC_SUBSTREAM_EXTENSIONS:
229
            /* Peek into the substream extension (sync + header size < frame_size) */
230
1.23M
            if( block_PeekOffsetBytes( &p_sys->bytestream,
231
1.23M
                                       p_sys->first.i_substream_header_size,
232
1.23M
                                       p_header,
233
1.23M
                                       VLC_DTS_HEADER_SIZE ) != VLC_SUCCESS )
234
654k
            {
235
                /* Need more data */
236
654k
                return NULL;
237
654k
            }
238
239
580k
            vlc_dts_header_t xssheader;
240
580k
            if( vlc_dts_header_Parse( &xssheader, p_header,
241
580k
                                      VLC_DTS_HEADER_SIZE ) != VLC_SUCCESS )
242
226k
            {
243
226k
                msg_Dbg( p_dec, "emulated substream sync word, can't find extension" );
244
226k
                block_SkipByte( &p_sys->bytestream );
245
226k
                p_sys->i_state = STATE_NOSYNC;
246
226k
                break;
247
226k
            }
248
249
354k
            if( xssheader.syncword == DTS_SYNC_SUBSTREAM_LBR )
250
240k
            {
251
                /*
252
                 * LBR exists as independent SUBSTREAM. It is seen valid
253
                 * only when SUBSTREAM[LBR]..SUBTREAM.
254
                 * CORE...SUBSTREAM is regular extension.
255
                 * SUBSTREAM...CORE is sync issue.
256
                 */
257
240k
                if (xssheader.i_rate != 0 && xssheader.i_frame_length != 0)
258
239k
                {
259
239k
                    p_dec->fmt_out.i_profile = PROFILE_DTS_EXPRESS;
260
239k
                    p_sys->first.i_rate = xssheader.i_rate;
261
239k
                    p_sys->first.i_frame_length = xssheader.i_frame_length;
262
239k
                    p_sys->i_state = STATE_NEXT_SYNC;
263
239k
                    break;
264
239k
                }
265
240k
            }
266
267
114k
            msg_Warn( p_dec, "substream without the paired core stream, skip it" );
268
114k
            p_sys->i_state = STATE_NOSYNC;
269
114k
            p_dec->fmt_out.i_profile = PROFILE_DTS;
270
114k
            if( block_SkipBytes( &p_sys->bytestream,
271
114k
                                 p_sys->first.i_frame_size ) != VLC_SUCCESS )
272
25.2k
                return NULL;
273
89.6k
            break;
274
275
2.62M
        case STATE_NEXT_SYNC:
276
            /* Check if next expected frame contains the sync word */
277
36.9M
            while( p_sys->i_state == STATE_NEXT_SYNC )
278
36.7M
            {
279
36.7M
                if( block_PeekOffsetBytes( &p_sys->bytestream,
280
36.7M
                                           p_sys->i_next_offset, p_header,
281
36.7M
                                           VLC_DTS_HEADER_SIZE )
282
36.7M
                                           != VLC_SUCCESS )
283
2.20M
                {
284
2.20M
                    if( p_block == NULL ) /* drain */
285
1.73k
                    {
286
1.73k
                        p_sys->i_state = STATE_GET_DATA;
287
1.73k
                        break;
288
1.73k
                    }
289
                    /* Need more data */
290
2.20M
                    return NULL;
291
2.20M
                }
292
293
34.5M
                if( p_header[0] == 0 )
294
34.1M
                {
295
                    /* DTS wav files, audio CD's and some mkvs use stuffing */
296
34.1M
                    p_sys->i_next_offset++;
297
34.1M
                    continue;
298
34.1M
                }
299
300
417k
                if( !vlc_dts_header_IsSync( p_header, VLC_DTS_HEADER_SIZE ) )
301
177k
                {
302
                    /* Even frame size is likely incorrect FSIZE #18166 */
303
177k
                    if( (p_sys->first.i_frame_size % 2) && p_sys->i_next_offset > 0 &&
304
144k
                        block_PeekOffsetBytes( &p_sys->bytestream,
305
144k
                                               p_sys->i_next_offset - 1, p_header,
306
144k
                                               VLC_DTS_HEADER_SIZE ) == 0 &&
307
144k
                         vlc_dts_header_IsSync( p_header, VLC_DTS_HEADER_SIZE ) )
308
63.0k
                    {
309
63.0k
                        p_sys->i_input_size = p_sys->i_next_offset = p_sys->first.i_frame_size - 1;
310
63.0k
                        assert(p_sys->i_input_size);
311
63.0k
                        if(p_sys->i_input_size >= VLC_DTS_HEADER_SIZE)
312
61.6k
                            break; /* reenter */
313
63.0k
                    }
314
116k
                    msg_Dbg( p_dec, "emulated sync word "
315
116k
                             "(no sync on following frame)" );
316
116k
                    p_sys->i_state = STATE_NOSYNC;
317
116k
                    block_SkipByte( &p_sys->bytestream );
318
116k
                    break;
319
177k
                }
320
321
                /* Check if a DTS substream packet is located just after
322
                 * the core packet */
323
240k
                if( p_sys->i_next_offset == p_sys->first.i_frame_size &&
324
159k
                    vlc_dts_header_Parse( &p_sys->second,
325
159k
                                          p_header, VLC_DTS_HEADER_SIZE ) == VLC_SUCCESS &&
326
155k
                    p_sys->second.syncword == DTS_SYNC_SUBSTREAM )
327
76.9k
                {
328
76.9k
                    p_sys->i_state = STATE_NEXT_SYNC_SUBSTREAM_EXTENSIONS;
329
76.9k
                }
330
163k
                else
331
163k
                {
332
163k
                    p_dec->fmt_out.i_profile = PROFILE_DTS;
333
163k
                    p_sys->i_state = STATE_GET_DATA;
334
163k
                }
335
240k
            }
336
419k
            break;
337
338
419k
        case STATE_NEXT_SYNC_SUBSTREAM_EXTENSIONS:
339
76.9k
            assert(p_sys->second.syncword == DTS_SYNC_SUBSTREAM);
340
76.9k
            if( p_sys->first.syncword == DTS_SYNC_SUBSTREAM )
341
63.5k
            {
342
                /* First substream must have been LBR */
343
63.5k
                p_dec->fmt_out.i_profile = PROFILE_DTS_EXPRESS;
344
63.5k
            }
345
13.3k
            else /* Otherwise that's core + extensions, we need to output both */
346
13.3k
            {
347
13.3k
                p_dec->fmt_out.i_profile = PROFILE_DTS_HD;
348
13.3k
                p_sys->i_input_size += p_sys->second.i_frame_size;
349
13.3k
            }
350
76.9k
            p_sys->i_state = STATE_GET_DATA;
351
76.9k
            break;
352
353
245k
        case STATE_GET_DATA:
354
            /* Make sure we have enough data. */
355
245k
            if( block_WaitBytes( &p_sys->bytestream,
356
245k
                                 p_sys->i_input_size ) != VLC_SUCCESS )
357
5.34k
            {
358
                /* Need more data */
359
5.34k
                return NULL;
360
5.34k
            }
361
240k
            p_sys->i_state = STATE_SEND_DATA;
362
            /* fallthrough */
363
364
240k
        case STATE_SEND_DATA:
365
240k
            if( !(p_out_buffer = GetOutBuffer( p_dec )) )
366
0
            {
367
0
                return NULL;
368
0
            }
369
370
            /* Copy the whole frame into the buffer. When we reach this point
371
             * we already know we have enough data available. */
372
240k
            block_GetBytes( &p_sys->bytestream, p_out_buffer->p_buffer,
373
240k
                            p_out_buffer->i_buffer );
374
375
            /* Make sure we don't reuse the same pts twice */
376
240k
            if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
377
226k
                p_sys->i_pts = p_sys->bytestream.p_block->i_pts = VLC_TICK_INVALID;
378
379
240k
            if( p_sys->b_discontinuity )
380
249
            {
381
249
                p_sys->b_discontinuity = false;
382
249
                p_out_buffer->i_flags |= BLOCK_FLAG_DISCONTINUITY;
383
249
            }
384
385
            /* So p_block doesn't get re-added several times */
386
240k
            if( pp_block )
387
235k
                *pp_block = block_BytestreamPop( &p_sys->bytestream );
388
389
240k
            p_sys->i_state = STATE_NOSYNC;
390
391
240k
            return p_out_buffer;
392
7.29M
        }
393
7.29M
    }
394
4.22M
}
395
396
static void Close( vlc_object_t *p_this )
397
3.48k
{
398
3.48k
    decoder_t *p_dec = (decoder_t*)p_this;
399
3.48k
    decoder_sys_t *p_sys = p_dec->p_sys;
400
401
3.48k
    block_BytestreamRelease( &p_sys->bytestream );
402
403
3.48k
    free( p_sys );
404
3.48k
}
405
406
static int Open( vlc_object_t *p_this )
407
93.6k
{
408
93.6k
    decoder_t *p_dec = (decoder_t*)p_this;
409
93.6k
    decoder_sys_t *p_sys;
410
411
93.6k
    if( p_dec->fmt_in->i_codec != VLC_CODEC_DTS )
412
90.1k
        return VLC_EGENERIC;
413
414
    /* Allocate the memory needed to store the decoder's structure */
415
3.48k
    if( ( p_dec->p_sys = p_sys = malloc(sizeof(decoder_sys_t)) ) == NULL )
416
0
        return VLC_ENOMEM;
417
418
    /* Misc init */
419
3.48k
    p_sys->i_state = STATE_NOSYNC;
420
3.48k
    date_Set( &p_sys->end_date, VLC_TICK_INVALID );
421
3.48k
    p_sys->i_pts = VLC_TICK_INVALID;
422
3.48k
    p_sys->b_date_set = false;
423
3.48k
    p_sys->b_discontinuity = false;
424
3.48k
    memset(&p_sys->first, 0, sizeof(vlc_dts_header_t));
425
3.48k
    memset(&p_sys->second, 0, sizeof(vlc_dts_header_t));
426
3.48k
    block_BytestreamInit( &p_sys->bytestream );
427
428
    /* Set output properties (passthrough only) */
429
3.48k
    p_dec->fmt_out.i_codec = p_dec->fmt_in->i_codec;
430
3.48k
    p_dec->fmt_out.audio = p_dec->fmt_in->audio;
431
432
    /* Set callback */
433
3.48k
    p_dec->pf_packetize = PacketizeBlock;
434
3.48k
    p_dec->pf_flush     = PacketizeFlush;
435
3.48k
    p_dec->pf_get_cc    = NULL;
436
3.48k
    return VLC_SUCCESS;
437
3.48k
}