Coverage Report

Created: 2025-11-09 06:39

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