Coverage Report

Created: 2026-02-05 06:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vlc/modules/packetizer/mpegaudio.c
Line
Count
Source
1
/*****************************************************************************
2
 * mpegaudio.c: parse MPEG audio sync info and packetize the stream
3
 *****************************************************************************
4
 * Copyright (C) 2001-2016 VLC authors and VideoLAN
5
 *
6
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7
 *          Eric Petit <titer@videolan.org>
8
 *          Christophe Massiot <massiot@via.ecp.fr>
9
 *          Gildas Bazin <gbazin@videolan.org>
10
 *
11
 * This program is free software; you can redistribute it and/or modify it
12
 * under the terms of the GNU Lesser General Public License as published by
13
 * the Free Software Foundation; either version 2.1 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Lesser General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Lesser General Public License
22
 * along with this program; if not, write to the Free Software Foundation,
23
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24
 *****************************************************************************/
25
26
/*****************************************************************************
27
 * Preamble
28
 *****************************************************************************/
29
#ifdef HAVE_CONFIG_H
30
# include "config.h"
31
#endif
32
33
#include <vlc_common.h>
34
#include <vlc_plugin.h>
35
#include <vlc_codec.h>
36
#include <vlc_aout.h>
37
#include <vlc_modules.h>
38
#include <assert.h>
39
40
#include <vlc_block_helper.h>
41
42
#include "packetizer_helper.h"
43
#include "mpegaudio.h"
44
45
/*****************************************************************************
46
 * decoder_sys_t : decoder descriptor
47
 *****************************************************************************/
48
typedef struct
49
{
50
    /*
51
     * Input properties
52
     */
53
    int        i_state;
54
55
    block_bytestream_t bytestream;
56
57
    /*
58
     * Common properties
59
     */
60
    date_t          end_date;
61
62
    vlc_tick_t i_pts;
63
64
    unsigned int i_free_frame_size;
65
    struct mpga_frameheader_s header;
66
67
    bool   b_discontinuity;
68
} decoder_sys_t;
69
70
13.5M
#define MAD_BUFFER_GUARD 8
71
13.0M
#define MPGA_HEADER_SIZE 4
72
73
/****************************************************************************
74
 * Local prototypes
75
 ****************************************************************************/
76
77
static int  Open( vlc_object_t * );
78
static void Close(  vlc_object_t * );
79
80
/*****************************************************************************
81
 * Module descriptor
82
 *****************************************************************************/
83
108
vlc_module_begin ()
84
54
    set_subcategory( SUBCAT_INPUT_ACODEC )
85
54
    set_description( N_("MPEG audio layer I/II/III packetizer") )
86
54
    set_capability( "audio packetizer", 10 )
87
108
    set_callbacks( Open, Close )
88
54
vlc_module_end ()
89
90
/*****************************************************************************
91
 * Flush:
92
 *****************************************************************************/
93
static void Flush( decoder_t *p_dec )
94
0
{
95
0
    decoder_sys_t *p_sys = p_dec->p_sys;
96
97
0
    date_Set( &p_sys->end_date, VLC_TICK_INVALID );
98
0
    p_sys->i_state = STATE_NOSYNC;
99
0
    block_BytestreamEmpty( &p_sys->bytestream );
100
0
    p_sys->b_discontinuity = true;
101
0
}
102
103
/*****************************************************************************
104
 * GetOutBuffer:
105
 *****************************************************************************/
106
static uint8_t *GetOutBuffer( decoder_t *p_dec, block_t **pp_out_buffer )
107
312k
{
108
312k
    decoder_sys_t *p_sys = p_dec->p_sys;
109
110
312k
    if( p_dec->fmt_out.audio.i_rate != p_sys->header.i_sample_rate ||
111
311k
        date_Get( &p_sys->end_date ) == VLC_TICK_INVALID )
112
1.91k
    {
113
1.91k
        msg_Dbg( p_dec, "MPGA channels:%"PRIu8" samplerate:%u bitrate:%"PRIu16,
114
1.91k
                 p_sys->header.i_channels, p_sys->header.i_sample_rate,
115
1.91k
                 p_sys->header.i_bit_rate );
116
117
1.91k
        if( p_sys->end_date.i_divider_num == 0 )
118
0
            date_Init( &p_sys->end_date, p_sys->header.i_sample_rate, 1 );
119
1.91k
        else
120
1.91k
            date_Change( &p_sys->end_date, p_sys->header.i_sample_rate, 1 );
121
1.91k
        date_Set( &p_sys->end_date, p_sys->i_pts );
122
1.91k
    }
123
124
312k
    p_dec->fmt_out.i_profile        = p_sys->header.i_layer;
125
312k
    p_dec->fmt_out.audio.i_rate     = p_sys->header.i_sample_rate;
126
312k
    p_dec->fmt_out.audio.i_channels = p_sys->header.i_channels;
127
312k
    p_dec->fmt_out.audio.i_frame_length = p_sys->header.i_samples_per_frame;
128
312k
    p_dec->fmt_out.audio.i_bytes_per_frame = p_sys->header.i_max_frame_size;
129
130
312k
    p_dec->fmt_out.audio.i_physical_channels = p_sys->header.i_channels_conf;
131
312k
    p_dec->fmt_out.audio.i_chan_mode = p_sys->header.i_chan_mode;
132
133
312k
    p_dec->fmt_out.i_bitrate = p_sys->header.i_bit_rate * 1000U;
134
135
312k
    block_t *p_block = block_Alloc( p_sys->header.i_frame_size );
136
312k
    if( p_block == NULL )
137
0
        return NULL;
138
139
312k
    p_block->i_pts = p_block->i_dts = date_Get( &p_sys->end_date );
140
312k
    p_block->i_length =
141
312k
        date_Increment( &p_sys->end_date, p_sys->header.i_samples_per_frame ) - p_block->i_pts;
142
143
312k
    *pp_out_buffer = p_block;
144
312k
    return p_block->p_buffer;
145
312k
}
146
147
/****************************************************************************
148
 * DecodeBlock: the whole thing
149
 ****************************************************************************
150
 * This function is called just after the thread is launched.
151
 ****************************************************************************/
152
static block_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
153
361k
{
154
361k
    decoder_sys_t *p_sys = p_dec->p_sys;
155
361k
    uint8_t p_header[MAD_BUFFER_GUARD];
156
361k
    uint32_t i_header;
157
361k
    uint8_t *p_buf;
158
361k
    block_t *p_out_buffer;
159
160
361k
    block_t *p_block = pp_block ? *pp_block : NULL;
161
162
361k
    if (p_block)
163
359k
    {
164
359k
        if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
165
0
        {
166
            /* First always drain complete blocks before discontinuity */
167
0
            block_t *p_drain = DecodeBlock( p_dec, NULL );
168
0
            if( p_drain )
169
0
                return p_drain;
170
171
0
            Flush( p_dec );
172
173
0
            if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
174
0
            {
175
0
                block_Release( p_block );
176
0
                return NULL;
177
0
            }
178
0
        }
179
180
359k
        if( p_block->i_pts == VLC_TICK_INVALID &&
181
354k
            date_Get( &p_sys->end_date ) == VLC_TICK_INVALID )
182
3.86k
        {
183
            /* We've just started the stream, wait for the first PTS. */
184
3.86k
            msg_Dbg( p_dec, "waiting for PTS" );
185
3.86k
            block_Release( p_block );
186
3.86k
            return NULL;
187
3.86k
        }
188
189
356k
        block_BytestreamPush( &p_sys->bytestream, p_block );
190
356k
    }
191
192
23.8M
    while( 1 )
193
23.8M
    {
194
23.8M
        switch( p_sys->i_state )
195
23.8M
        {
196
197
12.7M
        case STATE_NOSYNC:
198
37.4M
            while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
199
37.4M
                   == VLC_SUCCESS )
200
37.4M
            {
201
                /* Look for sync word - should be 0xffe */
202
37.4M
                if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
203
12.7M
                {
204
12.7M
                    p_sys->i_state = STATE_SYNC;
205
12.7M
                    break;
206
12.7M
                }
207
24.7M
                block_SkipByte( &p_sys->bytestream );
208
24.7M
            }
209
12.7M
            if( p_sys->i_state != STATE_SYNC )
210
8.05k
            {
211
8.05k
                block_BytestreamFlush( &p_sys->bytestream );
212
213
                /* Need more data */
214
8.05k
                return NULL;
215
8.05k
            }
216
            /* fallthrough */
217
218
12.7M
        case STATE_SYNC:
219
            /* New frame, set the Presentation Time Stamp */
220
12.7M
            p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
221
12.7M
            if( p_sys->i_pts != VLC_TICK_INVALID &&
222
260k
                p_sys->i_pts != date_Get( &p_sys->end_date ) )
223
1.09k
            {
224
1.09k
                if( p_dec->fmt_in->i_original_fourcc == VLC_FOURCC( 'D','V','R',' ') )
225
0
                {
226
0
                    if( date_Get( &p_sys->end_date ) == VLC_TICK_INVALID )
227
0
                        date_Set( &p_sys->end_date, p_sys->i_pts );
228
0
                }
229
1.09k
                else if ( p_sys->i_pts != date_Get( &p_sys->end_date ) )
230
1.09k
                {
231
1.09k
                    date_Set( &p_sys->end_date, p_sys->i_pts );
232
1.09k
                }
233
1.09k
            }
234
12.7M
            p_sys->i_state = STATE_HEADER;
235
            /* fallthrough */
236
237
12.7M
        case STATE_HEADER:
238
            /* Get MPGA frame header (MPGA_HEADER_SIZE bytes) */
239
12.7M
            if( block_PeekBytes( &p_sys->bytestream, p_header,
240
12.7M
                                 MPGA_HEADER_SIZE ) != VLC_SUCCESS )
241
4.26k
            {
242
                /* Need more data */
243
4.26k
                return NULL;
244
4.26k
            }
245
246
            /* Build frame header */
247
12.7M
            i_header = GetDWBE(p_header);
248
249
            /* Check if frame is valid and get frame info */
250
12.7M
            if( mpga_decode_frameheader( i_header, &p_sys->header ) )
251
10.0M
            {
252
10.0M
                msg_Dbg( p_dec, "emulated startcode" );
253
10.0M
                block_SkipByte( &p_sys->bytestream );
254
10.0M
                p_sys->i_state = STATE_NOSYNC;
255
10.0M
                break;
256
10.0M
            }
257
258
2.70M
            if( p_sys->header.i_bit_rate == 0 )
259
279k
            {
260
                /* Free bitrate, but 99% emulated startcode :( */
261
279k
                if( p_sys->i_free_frame_size == MPGA_HEADER_SIZE )
262
59.1k
                {
263
59.1k
                    msg_Dbg( p_dec, "free bitrate mode");
264
59.1k
                }
265
                /* The -1 below is to account for the frame padding */
266
279k
                p_sys->header.i_frame_size = p_sys->i_free_frame_size - 1;
267
279k
            }
268
269
2.70M
            p_sys->i_state = STATE_NEXT_SYNC;
270
            /* fallthrough */
271
272
13.5M
        case STATE_NEXT_SYNC:
273
            /* Check if next expected frame contains the sync word */
274
13.5M
            if( block_PeekOffsetBytes( &p_sys->bytestream,
275
13.5M
                                       p_sys->header.i_frame_size, p_header,
276
13.5M
                                       MAD_BUFFER_GUARD ) != VLC_SUCCESS )
277
34.1k
            {
278
34.1k
                if( p_block == NULL ) /* drain */
279
702
                {
280
702
                    p_sys->i_state = STATE_SEND_DATA;
281
702
                    break;
282
702
                }
283
                /* Need more data */
284
33.4k
                return NULL;
285
34.1k
            }
286
287
13.4M
            if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
288
4.12M
            {
289
                /* Startcode is fine, let's try the header as an extra check */
290
291
                /* Build frame header */
292
4.12M
                i_header = GetDWBE(p_header);
293
294
4.12M
                struct mpga_frameheader_s nextheader;
295
4.12M
                if(mpga_decode_frameheader( i_header, &nextheader ))
296
2.94M
                {
297
                    /* Free bitrate only */
298
2.94M
                    if( p_sys->header.i_bit_rate == 0 )
299
2.53M
                    {
300
2.53M
                        if( p_sys->header.i_frame_size > p_sys->header.i_max_frame_size )
301
2.42k
                        {
302
2.42k
                            msg_Dbg( p_dec, "frame too big %u > %u "
303
2.42k
                                     "(emulated startcode ?)",
304
2.42k
                                     p_sys->header.i_frame_size,
305
2.42k
                                     p_sys->header.i_max_frame_size );
306
2.42k
                            block_SkipByte( &p_sys->bytestream );
307
2.42k
                            p_sys->i_state = STATE_NOSYNC;
308
2.42k
                            p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
309
2.42k
                        }
310
2.53M
                        else
311
2.53M
                        {
312
2.53M
                            p_sys->header.i_frame_size++;
313
2.53M
                        }
314
2.53M
                    }
315
409k
                    else
316
409k
                    {
317
409k
                        msg_Dbg( p_dec, "emulated startcode on next frame" );
318
409k
                        block_SkipByte( &p_sys->bytestream );
319
409k
                        p_sys->i_state = STATE_NOSYNC;
320
409k
                    }
321
2.94M
                    break;
322
2.94M
                }
323
324
                /* Check info is in sync with previous one */
325
1.17M
                if( nextheader.i_channels_conf != p_sys->header.i_channels_conf ||
326
732k
                    nextheader.i_chan_mode != p_sys->header.i_chan_mode ||
327
632k
                    nextheader.i_sample_rate != p_sys->header.i_sample_rate ||
328
349k
                    nextheader.i_layer != p_sys->header.i_layer ||
329
333k
                    nextheader.i_samples_per_frame != p_sys->header.i_samples_per_frame )
330
845k
                {
331
                    /* Free bitrate only */
332
845k
                    if( p_sys->header.i_bit_rate == 0 )
333
248k
                    {
334
248k
                        p_sys->header.i_frame_size++;
335
248k
                        break;
336
248k
                    }
337
338
596k
                    msg_Dbg( p_dec, "parameters changed unexpectedly "
339
596k
                             "(emulated startcode ?)" );
340
596k
                    block_SkipByte( &p_sys->bytestream );
341
596k
                    p_sys->i_state = STATE_NOSYNC;
342
596k
                    break;
343
845k
                }
344
345
                /* Free bitrate only */
346
333k
                if( p_sys->header.i_bit_rate == 0 )
347
292k
                {
348
292k
                    if( nextheader.i_bit_rate != 0 )
349
22.0k
                    {
350
22.0k
                        p_sys->header.i_frame_size++;
351
22.0k
                        break;
352
22.0k
                    }
353
292k
                }
354
355
333k
            }
356
9.35M
            else
357
9.35M
            {
358
                /* Free bitrate only */
359
9.35M
                if( p_sys->header.i_bit_rate == 0 )
360
7.96M
                {
361
7.96M
                    if( p_sys->header.i_frame_size > p_sys->header.i_max_frame_size )
362
5.88k
                    {
363
5.88k
                        msg_Dbg( p_dec, "frame too big %u > %u "
364
5.88k
                                 "(emulated startcode ?)",
365
5.88k
                                 p_sys->header.i_frame_size,
366
5.88k
                                 p_sys->header.i_max_frame_size );
367
5.88k
                        block_SkipByte( &p_sys->bytestream );
368
5.88k
                        p_sys->i_state = STATE_NOSYNC;
369
5.88k
                        p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
370
5.88k
                        break;
371
5.88k
                    }
372
373
7.96M
                    p_sys->header.i_frame_size++;
374
7.96M
                    break;
375
7.96M
                }
376
377
1.38M
                msg_Dbg( p_dec, "emulated startcode "
378
1.38M
                         "(no startcode on following frame)" );
379
1.38M
                p_sys->i_state = STATE_NOSYNC;
380
1.38M
                block_SkipByte( &p_sys->bytestream );
381
1.38M
                break;
382
9.35M
            }
383
384
311k
            p_sys->i_state = STATE_GET_DATA;
385
311k
            break;
386
387
311k
        case STATE_GET_DATA:
388
            /* Make sure we have enough data.
389
             * (Not useful if we went through NEXT_SYNC) */
390
311k
            if( block_WaitBytes( &p_sys->bytestream,
391
311k
                                 p_sys->header.i_frame_size ) != VLC_SUCCESS )
392
0
            {
393
                /* Need more data */
394
0
                return NULL;
395
0
            }
396
311k
            p_sys->i_state = STATE_SEND_DATA;
397
            /* fallthrough */
398
399
312k
        case STATE_SEND_DATA:
400
312k
            if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
401
0
            {
402
0
                return NULL;
403
0
            }
404
405
            /* Free bitrate only */
406
312k
            if( p_sys->header.i_bit_rate == 0 )
407
270k
            {
408
270k
                p_sys->i_free_frame_size = p_sys->header.i_frame_size;
409
270k
            }
410
411
            /* Copy the whole frame into the buffer. */
412
312k
            if ( block_GetBytes( &p_sys->bytestream, p_buf,
413
312k
                                 __MIN( p_sys->header.i_frame_size,
414
312k
                                        p_out_buffer->i_buffer ) ) )
415
372
            {
416
372
                block_Release(p_out_buffer);
417
372
                return NULL;
418
372
            }
419
420
311k
            p_sys->i_state = STATE_NOSYNC;
421
422
            /* Make sure we don't reuse the same pts twice */
423
311k
            if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
424
311k
                p_sys->i_pts = p_sys->bytestream.p_block->i_pts = VLC_TICK_INVALID;
425
426
311k
            if( p_sys->b_discontinuity )
427
0
            {
428
0
                p_out_buffer->i_flags |= BLOCK_FLAG_DISCONTINUITY;
429
0
                p_sys->b_discontinuity = false;
430
0
            }
431
432
            /* So p_block doesn't get re-added several times */
433
311k
            p_block = block_BytestreamPop( &p_sys->bytestream );
434
311k
            if (pp_block)
435
311k
                *pp_block = p_block;
436
516
            else if (p_block)
437
468
                block_Release(p_block);
438
439
311k
            return p_out_buffer;
440
23.8M
        }
441
23.8M
    }
442
443
0
    return NULL;
444
357k
}
445
446
/*****************************************************************************
447
 * Close: clean up the decoder
448
 *****************************************************************************/
449
static void Close( vlc_object_t *p_this )
450
4.61k
{
451
4.61k
    decoder_t *p_dec = (decoder_t *)p_this;
452
4.61k
    decoder_sys_t *p_sys = p_dec->p_sys;
453
454
4.61k
    block_BytestreamRelease( &p_sys->bytestream );
455
456
4.61k
    free( p_sys );
457
4.61k
}
458
459
/*****************************************************************************
460
 * Open: probe the decoder and return score
461
 *****************************************************************************/
462
static int Open( vlc_object_t *p_this )
463
2.03M
{
464
2.03M
    decoder_t *p_dec = (decoder_t*)p_this;
465
2.03M
    decoder_sys_t *p_sys;
466
467
2.03M
    if( p_dec->fmt_in->i_codec != VLC_CODEC_MPGA &&
468
2.03M
        p_dec->fmt_in->i_codec != VLC_CODEC_MP2 &&
469
2.02M
        p_dec->fmt_in->i_codec != VLC_CODEC_MP3 )
470
2.02M
    {
471
2.02M
        return VLC_EGENERIC;
472
2.02M
    }
473
474
    /* Allocate the memory needed to store the decoder's structure */
475
4.61k
    if( ( p_dec->p_sys = p_sys =
476
4.61k
          (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
477
0
        return VLC_ENOMEM;
478
479
    /* Misc init */
480
4.61k
    p_sys->i_state = STATE_NOSYNC;
481
4.61k
    date_Init( &p_sys->end_date, 1, 1 );
482
4.61k
    block_BytestreamInit( &p_sys->bytestream );
483
4.61k
    p_sys->i_pts = VLC_TICK_INVALID;
484
4.61k
    p_sys->b_discontinuity = false;
485
486
4.61k
    memset(&p_sys->header, 0, sizeof(p_sys->header));
487
488
    /* Set output properties */
489
4.61k
    p_dec->fmt_out.i_codec = p_dec->fmt_in->i_codec;
490
4.61k
    p_dec->fmt_out.audio.i_rate = 0; /* So end_date gets initialized */
491
492
    /* Set callback */
493
4.61k
    p_dec->pf_packetize    = DecodeBlock;
494
4.61k
    p_dec->pf_flush        = Flush;
495
4.61k
    p_dec->pf_get_cc       = NULL;
496
497
    /* Start with the minimum size for a free bitrate frame */
498
4.61k
    p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
499
500
4.61k
    return VLC_SUCCESS;
501
4.61k
}