Coverage Report

Created: 2025-12-14 06:40

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