Coverage Report

Created: 2026-08-31 08:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vlc/modules/demux/aiff.c
Line
Count
Source
1
/*****************************************************************************
2
 * aiff.c: Audio Interchange File Format demuxer
3
 *****************************************************************************
4
 * Copyright (C) 2004-2007 VLC authors and VideoLAN
5
 *
6
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7
 *
8
 * This program is free software; you can redistribute it and/or modify it
9
 * under the terms of the GNU Lesser General Public License as published by
10
 * the Free Software Foundation; either version 2.1 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with this program; if not, write to the Free Software Foundation,
20
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21
 *****************************************************************************/
22
23
/*****************************************************************************
24
 * Preamble
25
 *****************************************************************************/
26
27
#ifdef HAVE_CONFIG_H
28
# include "config.h"
29
#endif
30
31
#include <vlc_common.h>
32
#include <vlc_plugin.h>
33
#include <vlc_demux.h>
34
#include <vlc_aout.h>
35
#include <vlc_meta.h>
36
#include <limits.h>
37
#include <stdint.h>
38
39
#include "mp4/coreaudio.h"
40
41
/* TODO:
42
 *  - ...
43
 */
44
45
/*****************************************************************************
46
 * Module descriptor
47
 *****************************************************************************/
48
static int  Open    ( vlc_object_t * );
49
static void Close   ( vlc_object_t * );
50
51
170
vlc_module_begin ()
52
85
    set_subcategory( SUBCAT_INPUT_DEMUX )
53
85
    set_description( N_("AIFF demuxer" ) )
54
85
    set_capability( "demux", 10 )
55
170
    set_callbacks( Open, Close )
56
85
    add_shortcut( "aiff" )
57
85
    add_file_extension("aiff")
58
85
vlc_module_end ()
59
60
/*****************************************************************************
61
 * Local prototypes
62
 *****************************************************************************/
63
64
typedef struct
65
{
66
    es_out_id_t *es;
67
    unsigned int i_rate;
68
69
    /* real data start */
70
    uint64_t    i_ssnd_start;
71
    uint64_t    i_ssnd_end;
72
73
    unsigned    i_ssnd_fsize;
74
75
    vlc_tick_t  i_time;
76
77
    uint8_t     i_chans_to_reorder;
78
    uint8_t     pi_chan_table[AOUT_CHAN_MAX];
79
    vlc_meta_t  *p_meta;
80
    vlc_fourcc_t audio_fourcc;
81
} demux_sys_t;
82
83
static int Demux  ( demux_t *p_demux );
84
static int Control( demux_t *p_demux, int i_query, va_list args );
85
86
/* GetF80BE: read a 80 bits float in big endian */
87
static unsigned int GetF80BE( const uint8_t p[10] )
88
63
{
89
63
    unsigned int i_mantissa = GetDWBE( &p[2] );
90
63
    int          i_exp = 30 - p[1];
91
63
    unsigned int i_last = 0;
92
93
758
    while( i_exp-- > 0 )
94
695
    {
95
695
        i_last = i_mantissa;
96
695
        i_mantissa >>= 1;
97
695
    }
98
63
    if( i_last&0x01 )
99
23
    {
100
23
        i_mantissa++;
101
23
    }
102
63
    return i_mantissa;
103
63
}
104
105
/*****************************************************************************
106
 * ReadTextChunk: reads aiff text chunks - name, author, copyright, annotation
107
 *****************************************************************************/
108
static int ReadTextChunk( demux_t *p_demux,  uint64_t i_chunk_size, uint32_t i_data_size )
109
210k
{
110
210k
    demux_sys_t *p_sys = p_demux->p_sys;
111
210k
    const uint8_t *p_peek;
112
113
#if UINT32_MAX >= SIZE_MAX
114
    if ( i_data_size >= SIZE_MAX )
115
        return VLC_ENOMEM;
116
#endif
117
118
210k
    ssize_t ret = vlc_stream_Peek( p_demux->s, &p_peek, i_chunk_size );
119
210k
    if( ret == -1 || (size_t) ret != i_chunk_size )
120
22
        return VLC_EGENERIC;
121
122
210k
    static const struct {
123
210k
        const char  chunk_id[4];
124
210k
        int         i_meta;
125
210k
    } p_dsc[4] = {
126
210k
        { {'N', 'A', 'M', 'E'}, vlc_meta_Title },
127
210k
        { {'A', 'U', 'T', 'H'}, vlc_meta_Artist },
128
210k
        { {'(', 'c', ')', ' '}, vlc_meta_Copyright },
129
210k
        { {'A', 'N', 'N', 'O'}, vlc_meta_Description }
130
210k
    };
131
132
1.05M
    for( size_t i = 0; i < ARRAY_SIZE( p_dsc ); i++ )
133
841k
    {
134
841k
        if( memcmp(p_peek, p_dsc[i].chunk_id, 4) )
135
840k
            continue;
136
137
852
        char *psz_value = malloc( i_data_size + 1 );
138
139
852
        if( unlikely(psz_value == NULL) )
140
0
            return VLC_ENOMEM;
141
142
852
        if( !p_sys->p_meta )
143
10
        {
144
10
            p_sys->p_meta = vlc_meta_New();
145
10
            if( unlikely(p_sys->p_meta == NULL) )
146
0
            {
147
0
                free( psz_value );
148
0
                return VLC_ENOMEM;
149
0
            }
150
10
        }
151
152
852
        memcpy( psz_value, (char*)&p_peek[8], i_data_size );
153
852
        psz_value[i_data_size] = '\0';
154
852
        vlc_meta_Set( p_sys->p_meta, p_dsc[i].i_meta, psz_value );
155
852
        free( psz_value );
156
157
852
        return VLC_SUCCESS;
158
852
    }
159
160
209k
    return VLC_EGENERIC;
161
210k
}
162
163
/*****************************************************************************
164
 * Open
165
 *****************************************************************************/
166
static int Open( vlc_object_t *p_this )
167
139
{
168
139
    demux_t     *p_demux = (demux_t*)p_this;
169
139
    demux_sys_t *p_sys;
170
171
139
    bool b_canseek = false;
172
139
    vlc_stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_canseek );
173
139
    if (!b_canseek)
174
0
    {
175
0
        msg_Err( p_demux, "AIFF parsing requires seeking" );
176
0
        return VLC_EGENERIC;
177
0
    }
178
179
139
    const uint8_t *p_peek;
180
181
139
    if( vlc_stream_Peek( p_demux->s, &p_peek, 12 ) < 12 )
182
0
        return VLC_EGENERIC;
183
139
    if( memcmp( p_peek, "FORM", 4 ) || memcmp( &p_peek[8], "AIFF", 4 ) )
184
12
        return VLC_EGENERIC;
185
186
    /* skip aiff header */
187
127
    if( vlc_stream_Read( p_demux->s, NULL, 12 ) != 12 )
188
0
        return VLC_EGENERIC;
189
190
127
    p_sys = vlc_obj_calloc( p_this, 1, sizeof( *p_sys ) );
191
192
127
    if( unlikely(p_sys == NULL) )
193
0
        return VLC_ENOMEM;
194
195
127
    p_demux->p_sys = p_sys;
196
197
127
    p_sys->i_time = 0;
198
127
    p_sys->i_chans_to_reorder = 0;
199
200
127
    es_format_t fmt;
201
127
    es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 't', 'w', 'o', 's' ) );
202
203
127
    const uint32_t *pi_channels_in = NULL;
204
127
    uint64_t i_ssnd_pos = UINT64_C(-1);
205
127
    uint32_t i_ssnd_size = 0;
206
127
    uint32_t i_ssnd_offset = 0;
207
208
127
    for( ;; )
209
212k
    {
210
212k
        if( vlc_stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
211
83
            break;
212
213
212k
        uint32_t i_data_size = GetDWBE( &p_peek[4] );
214
212k
        uint64_t i_chunk_size = UINT64_C( 8 ) + i_data_size + ( i_data_size & 1 );
215
216
212k
        msg_Dbg( p_demux, "chunk fcc=%4.4s size=%" PRIu64 " data_size=%" PRIu32,
217
212k
            p_peek, i_chunk_size, i_data_size );
218
219
212k
        if( !memcmp( p_peek, "COMM", 4 ) )
220
64
        {
221
64
            if( vlc_stream_Peek( p_demux->s, &p_peek, 18+8 ) < 18+8 )
222
0
                goto error;
223
224
64
            uint16_t channels = GetWBE( &p_peek[8] );
225
64
            if (channels > 32)
226
1
            {
227
1
                msg_Err( p_demux, "Unsupported number of channels %" PRIu16, channels );
228
1
                goto error;
229
1
            }
230
63
            fmt.audio.i_channels = channels;
231
63
            fmt.audio.i_bitspersample = GetWBE( &p_peek[14] );
232
63
            fmt.audio.i_rate = p_sys->i_rate = GetF80BE( &p_peek[16] );
233
234
63
            msg_Dbg( p_demux, "COMM: channels=%" PRIu16 " samples_frames=%d bits=%u rate=%u",
235
63
                     channels, GetDWBE( &p_peek[10] ), fmt.audio.i_bitspersample,
236
63
                     fmt.audio.i_rate );
237
63
        }
238
212k
        else if( !memcmp( p_peek, "SSND", 4 ) )
239
685
        {
240
685
            if( vlc_stream_Peek( p_demux->s, &p_peek, 8+8 ) < 8+8 )
241
1
                goto error;
242
243
684
            i_ssnd_pos = vlc_stream_Tell( p_demux->s );
244
684
            i_ssnd_size = i_data_size;
245
684
            i_ssnd_offset = GetDWBE( &p_peek[8] );
246
684
            uint32_t i_ssnd_blocksize = GetDWBE( &p_peek[12] );
247
248
684
            msg_Dbg( p_demux, "SSND: (offset=%" PRIu32 " blocksize=%" PRIu32 ")",
249
684
                     i_ssnd_offset, i_ssnd_blocksize );
250
684
        }
251
211k
        else if( !memcmp( p_peek, "CHAN", 4 ) && i_chunk_size > 8 + 12 )
252
848
        {
253
848
            ssize_t ret = vlc_stream_Peek( p_demux->s, &p_peek, i_chunk_size );
254
848
            if( ret == -1 || (size_t) ret != i_chunk_size )
255
15
                goto error;
256
257
833
            struct CoreAudio_layout_s layout;
258
833
            layout.i_channels_layout_tag = GetDWBE( &p_peek[8] );
259
833
            layout.i_channels_bitmap = GetDWBE( &p_peek[8+4] );
260
833
            layout.i_channels_description_count = GetDWBE( &p_peek[8+8] );
261
            /* TODO: handle CoreAudio_layout_s.p_descriptions */
262
263
833
            if ( CoreAudio_Layout_to_vlc( &layout,
264
833
                                          &fmt.audio.i_physical_channels,
265
833
                                          &fmt.audio.i_channels,
266
833
                                          &pi_channels_in ) != VLC_SUCCESS )
267
833
                msg_Warn( p_demux, "discarding chan mapping" );
268
833
        }
269
210k
        else
270
210k
        {
271
210k
            int i_ret = ReadTextChunk( p_demux, i_chunk_size, i_data_size );
272
273
210k
            if( unlikely(i_ret == VLC_ENOMEM) )
274
0
                goto error;
275
210k
        }
276
277
        /* consume chunk data */
278
424k
        for( ssize_t i_req; i_chunk_size; i_chunk_size -= i_req )
279
212k
        {
280
212k
#if SSIZE_MAX < UINT64_MAX
281
212k
            i_req = __MIN( SSIZE_MAX, i_chunk_size );
282
#else
283
            i_req = i_chunk_size;
284
#endif
285
212k
            if( vlc_stream_Read( p_demux->s, NULL, i_req ) != i_req )
286
27
            {
287
27
                msg_Warn( p_demux, "incomplete file" );
288
27
                goto error;
289
27
            }
290
212k
        }
291
212k
    }
292
293
83
    if ( i_ssnd_pos == UINT64_C(-1) )
294
27
    {
295
27
        msg_Err( p_demux, "Missing SSND chunk" );
296
27
        goto error;
297
27
    }
298
299
56
    if( pi_channels_in != NULL )
300
10
    {
301
10
        p_sys->i_chans_to_reorder =
302
10
            aout_CheckChannelReorder( pi_channels_in, NULL,
303
10
                                      fmt.audio.i_physical_channels,
304
10
                                      p_sys->pi_chan_table ) > 0;
305
10
        p_sys->audio_fourcc =
306
10
            vlc_fourcc_GetCodecAudio( fmt.i_codec,
307
10
                                      fmt.audio.i_bitspersample );
308
10
        if( p_sys->audio_fourcc == 0 )
309
0
            p_sys->i_chans_to_reorder = 0;
310
10
    }
311
312
56
    p_sys->i_ssnd_start = i_ssnd_pos + 16 + i_ssnd_offset;
313
56
    p_sys->i_ssnd_end   = p_sys->i_ssnd_start + i_ssnd_size;
314
315
56
    p_sys->i_ssnd_fsize = fmt.audio.i_channels *
316
56
                          ((fmt.audio.i_bitspersample + 7) / 8);
317
318
56
    if( p_sys->i_ssnd_fsize == 0 || p_sys->i_rate == 0 )
319
2
    {
320
2
        msg_Err( p_demux, "invalid audio parameters" );
321
2
        goto error;
322
2
    }
323
324
54
    if( i_ssnd_size == 0 )
325
41
    {
326
        /* unknown */
327
41
        p_sys->i_ssnd_end = 0;
328
41
    }
329
330
    /* seek into SSND chunk */
331
54
    if( vlc_stream_Seek( p_demux->s, p_sys->i_ssnd_start ) )
332
0
    {
333
0
        msg_Err( p_demux, "cannot seek to data chunk" );
334
0
        goto error;
335
0
    }
336
337
    /* */
338
54
    fmt.i_id = 0;
339
54
    p_sys->es = es_out_Add( p_demux->out, &fmt );
340
54
    if( unlikely(p_sys->es == NULL) )
341
0
        goto error;
342
343
54
    p_demux->pf_demux = Demux;
344
54
    p_demux->pf_control = Control;
345
346
54
    return VLC_SUCCESS;
347
348
73
error:
349
73
    Close( p_this );
350
73
    return VLC_EGENERIC;
351
54
}
352
353
/*****************************************************************************
354
 * Demux:
355
 *****************************************************************************/
356
static int Demux( demux_t *p_demux )
357
315k
{
358
315k
    demux_sys_t *p_sys = p_demux->p_sys;
359
315k
    uint64_t    i_tell = vlc_stream_Tell( p_demux->s );
360
361
315k
    block_t     *p_block;
362
315k
    size_t      i_read;
363
364
315k
    if( p_sys->i_ssnd_end != 0 && i_tell >= p_sys->i_ssnd_end )
365
1
    {
366
        /* EOF */
367
1
        return VLC_DEMUXER_EOF;
368
1
    }
369
370
    /* Set PCR */
371
315k
    es_out_SetPCR( p_demux->out, VLC_TICK_0 + p_sys->i_time);
372
373
    /* we will read 100ms at once */
374
315k
    i_read = p_sys->i_ssnd_fsize * ( p_sys->i_rate / 10 );
375
315k
    if( p_sys->i_ssnd_end != 0 && p_sys->i_ssnd_end > i_tell && p_sys->i_ssnd_end - i_tell < i_read )
376
8
    {
377
8
        i_read = p_sys->i_ssnd_end - i_tell;
378
8
    }
379
315k
    if( ( p_block = vlc_stream_Block( p_demux->s, i_read ) ) == NULL )
380
53
    {
381
53
        return VLC_DEMUXER_EOF;
382
53
    }
383
384
315k
    p_block->i_dts =
385
315k
    p_block->i_pts = VLC_TICK_0 + p_sys->i_time;
386
387
315k
    p_sys->i_time += vlc_tick_from_samples(p_block->i_buffer,
388
315k
                                           p_sys->i_ssnd_fsize) /
389
315k
                     p_sys->i_rate;
390
391
315k
    if( p_sys->i_chans_to_reorder )
392
7
        aout_ChannelReorder( p_block->p_buffer, p_block->i_buffer,
393
7
                             p_sys->i_chans_to_reorder, p_sys->pi_chan_table,
394
7
                             p_sys->audio_fourcc );
395
    /* */
396
315k
    es_out_Send( p_demux->out, p_sys->es, p_block );
397
315k
    return VLC_DEMUXER_SUCCESS;
398
315k
}
399
400
/*****************************************************************************
401
 * Close: frees unused data
402
 *****************************************************************************/
403
static void Close( vlc_object_t * p_this )
404
127
{
405
127
    demux_t     *p_demux = (demux_t*)p_this;
406
127
    demux_sys_t *p_sys = p_demux->p_sys;
407
408
127
    if( p_sys->p_meta )
409
10
        vlc_meta_Delete( p_sys->p_meta );
410
127
}
411
412
/*****************************************************************************
413
 * Control:
414
 *****************************************************************************/
415
static int Control( demux_t *p_demux, int i_query, va_list args )
416
0
{
417
0
    demux_sys_t *p_sys = p_demux->p_sys;
418
0
    double f, *pf;
419
420
0
    switch( i_query )
421
0
    {
422
0
        case DEMUX_CAN_SEEK:
423
0
            return vlc_stream_vaControl( p_demux->s, i_query, args );
424
425
0
        case DEMUX_GET_POSITION:
426
0
        {
427
0
            uint64_t i_start = p_sys->i_ssnd_start;
428
0
            uint64_t i_end   = p_sys->i_ssnd_end != 0 ? p_sys->i_ssnd_end : stream_Size( p_demux->s );
429
0
            uint64_t i_tell  = vlc_stream_Tell( p_demux->s );
430
431
0
            pf = va_arg( args, double * );
432
433
0
            if( i_start < i_end && i_start <= i_tell )
434
0
            {
435
0
                *pf = (double)(i_tell - i_start)/(double)(i_end - i_start);
436
0
                return VLC_SUCCESS;
437
0
            }
438
0
            return VLC_EGENERIC;
439
0
        }
440
441
0
        case DEMUX_SET_POSITION:
442
0
        {
443
0
            uint64_t i_start = p_sys->i_ssnd_start;
444
0
            uint64_t i_end  = p_sys->i_ssnd_end != 0 ? p_sys->i_ssnd_end : stream_Size( p_demux->s );
445
446
0
            f = va_arg( args, double );
447
448
0
            if( i_start < i_end )
449
0
            {
450
0
                unsigned i_frame = (f * ( i_end - i_start )) / p_sys->i_ssnd_fsize;
451
0
                uint64_t i_new   = i_start + i_frame * p_sys->i_ssnd_fsize;
452
453
0
                if( vlc_stream_Seek( p_demux->s, i_new ) )
454
0
                {
455
0
                    return VLC_EGENERIC;
456
0
                }
457
0
                p_sys->i_time = vlc_tick_from_samples( i_frame, p_sys->i_rate );
458
0
                return VLC_SUCCESS;
459
0
            }
460
0
            return VLC_EGENERIC;
461
0
        }
462
463
0
        case DEMUX_GET_TIME:
464
0
            *va_arg( args, vlc_tick_t * ) = p_sys->i_time;
465
0
            return VLC_SUCCESS;
466
467
0
        case DEMUX_GET_LENGTH:
468
0
        {
469
0
            uint64_t i_end  = p_sys->i_ssnd_end != 0 ? p_sys->i_ssnd_end : stream_Size( p_demux->s );
470
471
0
            if( p_sys->i_ssnd_start < i_end )
472
0
            {
473
0
                *va_arg( args, vlc_tick_t * ) =
474
0
                    vlc_tick_from_samples( i_end - p_sys->i_ssnd_start, p_sys->i_ssnd_fsize) / p_sys->i_rate;
475
0
                return VLC_SUCCESS;
476
0
            }
477
0
            return VLC_EGENERIC;
478
0
        }
479
0
        case DEMUX_GET_META:
480
0
        {
481
0
            vlc_meta_t *p_meta = va_arg( args, vlc_meta_t * );
482
0
            if( !p_sys->p_meta )
483
0
                return VLC_EGENERIC;
484
0
            vlc_meta_Merge( p_meta, p_sys->p_meta );
485
0
            return VLC_SUCCESS;
486
0
        }
487
488
0
        case DEMUX_SET_TIME:
489
0
        case DEMUX_GET_FPS:
490
0
            return VLC_EGENERIC;
491
492
0
        case DEMUX_CAN_PAUSE:
493
0
        case DEMUX_SET_PAUSE_STATE:
494
0
        case DEMUX_CAN_CONTROL_PACE:
495
0
        case DEMUX_GET_PTS_DELAY:
496
0
             return demux_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args );
497
498
0
        default:
499
0
            return VLC_EGENERIC;
500
0
    }
501
0
}