Coverage Report

Created: 2026-08-31 08:22

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
170
vlc_module_begin ()
47
85
    set_subcategory( SUBCAT_SOUT_PACKETIZER )
48
85
    set_description( N_("DTS audio packetizer") )
49
85
    set_capability( "audio packetizer", 10 )
50
170
    set_callbacks( Open, Close )
51
85
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
202k
{
94
202k
    decoder_sys_t *p_sys = p_dec->p_sys;
95
202k
    if (p_sys->i_input_size == 0)
96
0
        return NULL;
97
98
202k
    if( !p_sys->b_date_set
99
201k
     || p_dec->fmt_out.audio.i_rate != p_sys->first.i_rate )
100
2.50k
    {
101
2.50k
        msg_Dbg( p_dec, "DTS samplerate:%d bitrate:%d",
102
2.50k
                 p_sys->first.i_rate, p_sys->first.i_bitrate );
103
104
2.50k
        date_Init( &p_sys->end_date, p_sys->first.i_rate, 1 );
105
2.50k
        date_Set( &p_sys->end_date, p_sys->i_pts );
106
2.50k
        p_sys->b_date_set = true;
107
2.50k
    }
108
109
202k
    p_dec->fmt_out.audio.i_rate     = p_sys->first.i_rate;
110
202k
    if( p_dec->fmt_out.audio.i_bytes_per_frame < p_sys->first.i_frame_size )
111
1.18k
        p_dec->fmt_out.audio.i_bytes_per_frame = p_sys->first.i_frame_size;
112
202k
    p_dec->fmt_out.audio.i_frame_length = p_sys->first.i_frame_length;
113
114
202k
    p_dec->fmt_out.audio.i_chan_mode = p_sys->first.i_chan_mode;
115
202k
    p_dec->fmt_out.audio.i_physical_channels = p_sys->first.i_physical_channels;
116
202k
    p_dec->fmt_out.audio.i_channels =
117
202k
        stdc_count_ones( p_dec->fmt_out.audio.i_physical_channels );
118
119
202k
    p_dec->fmt_out.i_bitrate = p_sys->first.i_bitrate;
120
121
202k
    block_t *p_block = block_Alloc( p_sys->i_input_size );
122
202k
    if( p_block == NULL )
123
0
        return NULL;
124
125
202k
    p_block->i_nb_samples = p_sys->first.i_frame_length;
126
202k
    p_block->i_pts = p_block->i_dts = date_Get( &p_sys->end_date );
127
202k
    p_block->i_length =
128
202k
        date_Increment( &p_sys->end_date, p_block->i_nb_samples ) - p_block->i_pts;
129
202k
    return p_block;
130
202k
}
131
132
static block_t *PacketizeBlock( decoder_t *p_dec, block_t **pp_block )
133
2.98M
{
134
2.98M
    decoder_sys_t *p_sys = p_dec->p_sys;
135
2.98M
    uint8_t p_header[VLC_DTS_HEADER_SIZE];
136
2.98M
    block_t *p_out_buffer;
137
138
2.98M
    block_t *p_block = pp_block ? *pp_block : NULL;
139
140
2.98M
    if( p_block )
141
2.97M
    {
142
2.97M
        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
2.97M
        if ( p_block->i_pts == VLC_TICK_INVALID &&
157
157k
             date_Get( &p_sys->end_date ) == VLC_TICK_INVALID ) {
158
            /* We've just started the stream, wait for the first PTS. */
159
6.21k
            block_Release( p_block );
160
6.21k
            return NULL;
161
6.21k
        }
162
163
2.96M
        block_BytestreamPush( &p_sys->bytestream, p_block );
164
2.96M
    }
165
166
5.50M
    while( 1 )
167
5.50M
    {
168
5.50M
        switch( p_sys->i_state )
169
5.50M
        {
170
2.69M
        case STATE_NOSYNC:
171
54.4M
            while( block_PeekBytes( &p_sys->bytestream, p_header, 6 )
172
54.4M
                   == VLC_SUCCESS )
173
53.4M
            {
174
53.4M
                if( vlc_dts_header_IsSync( p_header, 6 ) )
175
1.67M
                {
176
1.67M
                    p_sys->i_state = STATE_SYNC;
177
1.67M
                    break;
178
1.67M
                }
179
51.7M
                (void) block_SkipByte( &p_sys->bytestream );
180
51.7M
            }
181
2.69M
            if( p_sys->i_state != STATE_SYNC )
182
1.01M
            {
183
1.01M
                block_BytestreamFlush( &p_sys->bytestream );
184
185
                /* Need more data */
186
1.01M
                return NULL;
187
1.01M
            }
188
            /* fallthrough */
189
190
1.67M
        case STATE_SYNC:
191
            /* New frame, set the Presentation Time Stamp */
192
1.67M
            p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
193
1.67M
            if( p_sys->i_pts != VLC_TICK_INVALID &&
194
329k
                p_sys->i_pts != date_Get( &p_sys->end_date ) )
195
89.7k
            {
196
89.7k
                date_Set( &p_sys->end_date, p_sys->i_pts );
197
89.7k
            }
198
1.67M
            p_sys->i_state = STATE_HEADER;
199
            /* fallthrough */
200
201
1.69M
        case STATE_HEADER:
202
            /* Get DTS frame header (VLC_DTS_HEADER_SIZE bytes) */
203
1.69M
            if( block_PeekBytes( &p_sys->bytestream, p_header,
204
1.69M
                                 VLC_DTS_HEADER_SIZE ) != VLC_SUCCESS )
205
19.0k
            {
206
                /* Need more data */
207
19.0k
                return NULL;
208
19.0k
            }
209
210
            /* Check if frame is valid and get frame info */
211
1.67M
            if( vlc_dts_header_Parse( &p_sys->first, p_header,
212
1.67M
                                      VLC_DTS_HEADER_SIZE ) != VLC_SUCCESS
213
1.03M
             || p_sys->first.i_frame_size == 0 )
214
1.11M
            {
215
1.11M
                msg_Dbg( p_dec, "emulated sync word" );
216
1.11M
                block_SkipByte( &p_sys->bytestream );
217
1.11M
                p_sys->i_state = STATE_NOSYNC;
218
1.11M
                break;
219
1.11M
            }
220
221
566k
            if( p_sys->first.syncword == DTS_SYNC_SUBSTREAM )
222
463k
                p_sys->i_state = STATE_SYNC_SUBSTREAM_EXTENSIONS;
223
102k
            else
224
102k
                p_sys->i_state = STATE_NEXT_SYNC;
225
566k
            p_sys->i_input_size = p_sys->i_next_offset = p_sys->first.i_frame_size;
226
566k
            break;
227
228
1.08M
        case STATE_SYNC_SUBSTREAM_EXTENSIONS:
229
            /* Peek into the substream extension (sync + header size < frame_size) */
230
1.08M
            if( block_PeekOffsetBytes( &p_sys->bytestream,
231
1.08M
                                       p_sys->first.i_substream_header_size,
232
1.08M
                                       p_header,
233
1.08M
                                       VLC_DTS_HEADER_SIZE ) != VLC_SUCCESS )
234
622k
            {
235
                /* Need more data */
236
622k
                return NULL;
237
622k
            }
238
239
463k
            vlc_dts_header_t xssheader;
240
463k
            if( vlc_dts_header_Parse( &xssheader, p_header,
241
463k
                                      VLC_DTS_HEADER_SIZE ) != VLC_SUCCESS )
242
172k
            {
243
172k
                msg_Dbg( p_dec, "emulated substream sync word, can't find extension" );
244
172k
                block_SkipByte( &p_sys->bytestream );
245
172k
                p_sys->i_state = STATE_NOSYNC;
246
172k
                break;
247
172k
            }
248
249
291k
            if( xssheader.syncword == DTS_SYNC_SUBSTREAM_LBR )
250
201k
            {
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
201k
                if (xssheader.i_rate != 0 && xssheader.i_frame_length != 0)
258
199k
                {
259
199k
                    p_dec->fmt_out.i_profile = PROFILE_DTS_EXPRESS;
260
199k
                    p_sys->first.i_rate = xssheader.i_rate;
261
199k
                    p_sys->first.i_frame_length = xssheader.i_frame_length;
262
199k
                    p_sys->i_state = STATE_NEXT_SYNC;
263
199k
                    break;
264
199k
                }
265
201k
            }
266
267
91.7k
            msg_Warn( p_dec, "substream without the paired core stream, skip it" );
268
91.7k
            p_sys->i_state = STATE_NOSYNC;
269
91.7k
            p_dec->fmt_out.i_profile = PROFILE_DTS;
270
91.7k
            if( block_SkipBytes( &p_sys->bytestream,
271
91.7k
                                 p_sys->first.i_frame_size ) != VLC_SUCCESS )
272
24.5k
                return NULL;
273
67.1k
            break;
274
275
1.43M
        case STATE_NEXT_SYNC:
276
            /* Check if next expected frame contains the sync word */
277
33.1M
            while( p_sys->i_state == STATE_NEXT_SYNC )
278
32.9M
            {
279
32.9M
                if( block_PeekOffsetBytes( &p_sys->bytestream,
280
32.9M
                                           p_sys->i_next_offset, p_header,
281
32.9M
                                           VLC_DTS_HEADER_SIZE )
282
32.9M
                                           != VLC_SUCCESS )
283
1.09M
                {
284
1.09M
                    if( p_block == NULL ) /* drain */
285
1.62k
                    {
286
1.62k
                        p_sys->i_state = STATE_GET_DATA;
287
1.62k
                        break;
288
1.62k
                    }
289
                    /* Need more data */
290
1.09M
                    return NULL;
291
1.09M
                }
292
293
31.8M
                if( p_header[0] == 0 )
294
31.5M
                {
295
                    /* DTS wav files, audio CD's and some mkvs use stuffing */
296
31.5M
                    p_sys->i_next_offset++;
297
31.5M
                    continue;
298
31.5M
                }
299
300
340k
                if( !vlc_dts_header_IsSync( p_header, VLC_DTS_HEADER_SIZE ) )
301
138k
                {
302
                    /* Even frame size is likely incorrect FSIZE #18166 */
303
138k
                    if( (p_sys->first.i_frame_size % 2) && p_sys->i_next_offset > 0 &&
304
108k
                        block_PeekOffsetBytes( &p_sys->bytestream,
305
108k
                                               p_sys->i_next_offset - 1, p_header,
306
108k
                                               VLC_DTS_HEADER_SIZE ) == 0 &&
307
108k
                         vlc_dts_header_IsSync( p_header, VLC_DTS_HEADER_SIZE ) )
308
41.2k
                    {
309
41.2k
                        p_sys->i_input_size = p_sys->i_next_offset = p_sys->first.i_frame_size - 1;
310
41.2k
                        assert(p_sys->i_input_size);
311
41.2k
                        if(p_sys->i_input_size >= VLC_DTS_HEADER_SIZE)
312
39.9k
                            break; /* reenter */
313
41.2k
                    }
314
98.4k
                    msg_Dbg( p_dec, "emulated sync word "
315
98.4k
                             "(no sync on following frame)" );
316
98.4k
                    p_sys->i_state = STATE_NOSYNC;
317
98.4k
                    block_SkipByte( &p_sys->bytestream );
318
98.4k
                    break;
319
138k
                }
320
321
                /* Check if a DTS substream packet is located just after
322
                 * the core packet */
323
202k
                if( p_sys->i_next_offset == p_sys->first.i_frame_size &&
324
143k
                    vlc_dts_header_Parse( &p_sys->second,
325
143k
                                          p_header, VLC_DTS_HEADER_SIZE ) == VLC_SUCCESS &&
326
140k
                    p_sys->second.syncword == DTS_SYNC_SUBSTREAM )
327
73.6k
                {
328
73.6k
                    p_sys->i_state = STATE_NEXT_SYNC_SUBSTREAM_EXTENSIONS;
329
73.6k
                }
330
128k
                else
331
128k
                {
332
128k
                    p_dec->fmt_out.i_profile = PROFILE_DTS;
333
128k
                    p_sys->i_state = STATE_GET_DATA;
334
128k
                }
335
202k
            }
336
342k
            break;
337
338
342k
        case STATE_NEXT_SYNC_SUBSTREAM_EXTENSIONS:
339
73.6k
            assert(p_sys->second.syncword == DTS_SYNC_SUBSTREAM);
340
73.6k
            if( p_sys->first.syncword == DTS_SYNC_SUBSTREAM )
341
60.2k
            {
342
                /* First substream must have been LBR */
343
60.2k
                p_dec->fmt_out.i_profile = PROFILE_DTS_EXPRESS;
344
60.2k
            }
345
13.4k
            else /* Otherwise that's core + extensions, we need to output both */
346
13.4k
            {
347
13.4k
                p_dec->fmt_out.i_profile = PROFILE_DTS_HD;
348
13.4k
                p_sys->i_input_size += p_sys->second.i_frame_size;
349
13.4k
            }
350
73.6k
            p_sys->i_state = STATE_GET_DATA;
351
73.6k
            break;
352
353
207k
        case STATE_GET_DATA:
354
            /* Make sure we have enough data. */
355
207k
            if( block_WaitBytes( &p_sys->bytestream,
356
207k
                                 p_sys->i_input_size ) != VLC_SUCCESS )
357
5.34k
            {
358
                /* Need more data */
359
5.34k
                return NULL;
360
5.34k
            }
361
202k
            p_sys->i_state = STATE_SEND_DATA;
362
            /* fallthrough */
363
364
202k
        case STATE_SEND_DATA:
365
202k
            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
202k
            block_GetBytes( &p_sys->bytestream, p_out_buffer->p_buffer,
373
202k
                            p_out_buffer->i_buffer );
374
375
            /* Make sure we don't reuse the same pts twice */
376
202k
            if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
377
190k
                p_sys->i_pts = p_sys->bytestream.p_block->i_pts = VLC_TICK_INVALID;
378
379
202k
            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
202k
            if( pp_block )
387
199k
                *pp_block = block_BytestreamPop( &p_sys->bytestream );
388
389
202k
            p_sys->i_state = STATE_NOSYNC;
390
391
202k
            return p_out_buffer;
392
5.50M
        }
393
5.50M
    }
394
2.97M
}
395
396
static void Close( vlc_object_t *p_this )
397
3.27k
{
398
3.27k
    decoder_t *p_dec = (decoder_t*)p_this;
399
3.27k
    decoder_sys_t *p_sys = p_dec->p_sys;
400
401
3.27k
    block_BytestreamRelease( &p_sys->bytestream );
402
403
3.27k
    free( p_sys );
404
3.27k
}
405
406
static int Open( vlc_object_t *p_this )
407
91.8k
{
408
91.8k
    decoder_t *p_dec = (decoder_t*)p_this;
409
91.8k
    decoder_sys_t *p_sys;
410
411
91.8k
    if( p_dec->fmt_in->i_codec != VLC_CODEC_DTS )
412
88.5k
        return VLC_EGENERIC;
413
414
    /* Allocate the memory needed to store the decoder's structure */
415
3.27k
    if( ( p_dec->p_sys = p_sys = malloc(sizeof(decoder_sys_t)) ) == NULL )
416
0
        return VLC_ENOMEM;
417
418
    /* Misc init */
419
3.27k
    p_sys->i_state = STATE_NOSYNC;
420
3.27k
    date_Set( &p_sys->end_date, VLC_TICK_INVALID );
421
3.27k
    p_sys->i_pts = VLC_TICK_INVALID;
422
3.27k
    p_sys->b_date_set = false;
423
3.27k
    p_sys->b_discontinuity = false;
424
3.27k
    memset(&p_sys->first, 0, sizeof(vlc_dts_header_t));
425
3.27k
    memset(&p_sys->second, 0, sizeof(vlc_dts_header_t));
426
3.27k
    block_BytestreamInit( &p_sys->bytestream );
427
428
    /* Set output properties (passthrough only) */
429
3.27k
    p_dec->fmt_out.i_codec = p_dec->fmt_in->i_codec;
430
3.27k
    p_dec->fmt_out.audio = p_dec->fmt_in->audio;
431
432
    /* Set callback */
433
3.27k
    p_dec->pf_packetize = PacketizeBlock;
434
3.27k
    p_dec->pf_flush     = PacketizeFlush;
435
3.27k
    p_dec->pf_get_cc    = NULL;
436
3.27k
    return VLC_SUCCESS;
437
3.27k
}