Coverage Report

Created: 2026-03-31 07:49

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
38
#include "mp4/coreaudio.h"
39
40
/* TODO:
41
 *  - ...
42
 */
43
44
/*****************************************************************************
45
 * Module descriptor
46
 *****************************************************************************/
47
static int  Open    ( vlc_object_t * );
48
static void Close   ( vlc_object_t * );
49
50
96
vlc_module_begin ()
51
48
    set_subcategory( SUBCAT_INPUT_DEMUX )
52
48
    set_description( N_("AIFF demuxer" ) )
53
48
    set_capability( "demux", 10 )
54
96
    set_callbacks( Open, Close )
55
48
    add_shortcut( "aiff" )
56
48
    add_file_extension("aiff")
57
48
vlc_module_end ()
58
59
/*****************************************************************************
60
 * Local prototypes
61
 *****************************************************************************/
62
63
typedef struct
64
{
65
    es_format_t  fmt;
66
    es_out_id_t *es;
67
68
    int64_t     i_ssnd_pos;
69
    int64_t     i_ssnd_size;
70
    int         i_ssnd_offset;
71
    int         i_ssnd_blocksize;
72
73
    /* real data start */
74
    int64_t     i_ssnd_start;
75
    int64_t     i_ssnd_end;
76
77
    int         i_ssnd_fsize;
78
79
    vlc_tick_t  i_time;
80
81
    uint8_t     i_chans_to_reorder;
82
    uint8_t     pi_chan_table[AOUT_CHAN_MAX];
83
    vlc_meta_t  *p_meta;
84
    vlc_fourcc_t audio_fourcc;
85
} demux_sys_t;
86
87
static int Demux  ( demux_t *p_demux );
88
static int Control( demux_t *p_demux, int i_query, va_list args );
89
90
/* GetF80BE: read a 80 bits float in big endian */
91
static unsigned int GetF80BE( const uint8_t p[10] )
92
0
{
93
0
    unsigned int i_mantissa = GetDWBE( &p[2] );
94
0
    int          i_exp = 30 - p[1];
95
0
    unsigned int i_last = 0;
96
97
0
    while( i_exp-- > 0 )
98
0
    {
99
0
        i_last = i_mantissa;
100
0
        i_mantissa >>= 1;
101
0
    }
102
0
    if( i_last&0x01 )
103
0
    {
104
0
        i_mantissa++;
105
0
    }
106
0
    return i_mantissa;
107
0
}
108
109
/*****************************************************************************
110
 * ReadTextChunk: reads aiff text chunks - name, author, copyright, annotation
111
 *****************************************************************************/
112
static int ReadTextChunk( demux_t *p_demux,  uint64_t i_chunk_size, uint32_t i_data_size )
113
0
{
114
0
    demux_sys_t *p_sys = p_demux->p_sys;
115
0
    const uint8_t *p_peek;
116
117
0
    if ( i_data_size >= SIZE_MAX )
118
0
        return VLC_ENOMEM;
119
120
0
    ssize_t ret = vlc_stream_Peek( p_demux->s, &p_peek, i_chunk_size );
121
0
    if( ret == -1 || (size_t) ret != i_chunk_size )
122
0
        return VLC_EGENERIC;
123
124
0
    static const struct {
125
0
        const char  chunk_id[4];
126
0
        int         i_meta;
127
0
    } p_dsc[4] = {
128
0
        { {'N', 'A', 'M', 'E'}, vlc_meta_Title },
129
0
        { {'A', 'U', 'T', 'H'}, vlc_meta_Artist },
130
0
        { {'(', 'c', ')', ' '}, vlc_meta_Copyright },
131
0
        { {'A', 'N', 'N', 'O'}, vlc_meta_Description }
132
0
    };
133
134
0
    for( size_t i = 0; i < ARRAY_SIZE( p_dsc ); i++ )
135
0
    {
136
0
        if( memcmp(p_peek, p_dsc[i].chunk_id, 4) )
137
0
            continue;
138
139
0
        char *psz_value = malloc( i_data_size + 1 );
140
141
0
        if( unlikely(psz_value == NULL) )
142
0
            return VLC_ENOMEM;
143
144
0
        if( !p_sys->p_meta )
145
0
        {
146
0
            p_sys->p_meta = vlc_meta_New();
147
0
            if( unlikely(p_sys->p_meta == NULL) )
148
0
            {
149
0
                free( psz_value );
150
0
                return VLC_ENOMEM;
151
0
            }
152
0
        }
153
154
0
        memcpy( psz_value, (char*)&p_peek[8], i_data_size );
155
0
        psz_value[i_data_size] = '\0';
156
0
        vlc_meta_Set( p_sys->p_meta, p_dsc[i].i_meta, psz_value );
157
0
        free( psz_value );
158
159
0
        return VLC_SUCCESS;
160
0
    }
161
162
0
    return VLC_EGENERIC;
163
0
}
164
165
/*****************************************************************************
166
 * Open
167
 *****************************************************************************/
168
static int Open( vlc_object_t *p_this )
169
0
{
170
0
    demux_t     *p_demux = (demux_t*)p_this;
171
0
    demux_sys_t *p_sys;
172
173
0
    const uint8_t *p_peek;
174
175
0
    if( vlc_stream_Peek( p_demux->s, &p_peek, 12 ) < 12 )
176
0
        return VLC_EGENERIC;
177
0
    if( memcmp( p_peek, "FORM", 4 ) || memcmp( &p_peek[8], "AIFF", 4 ) )
178
0
        return VLC_EGENERIC;
179
180
    /* skip aiff header */
181
0
    if( vlc_stream_Read( p_demux->s, NULL, 12 ) != 12 )
182
0
        return VLC_EGENERIC;
183
184
0
    p_sys = vlc_obj_calloc( p_this, 1, sizeof( *p_sys ) );
185
186
0
    if( unlikely(p_sys == NULL) )
187
0
        return VLC_ENOMEM;
188
189
0
    p_demux->p_sys = p_sys;
190
191
0
    p_sys->i_time = 0;
192
0
    p_sys->i_ssnd_pos = -1;
193
0
    p_sys->i_chans_to_reorder = 0;
194
0
    es_format_Init( &p_sys->fmt, AUDIO_ES, VLC_FOURCC( 't', 'w', 'o', 's' ) );
195
196
0
    const uint32_t *pi_channels_in = NULL;
197
198
0
    for( ;; )
199
0
    {
200
0
        if( vlc_stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
201
0
            break;
202
203
0
        uint32_t i_data_size = GetDWBE( &p_peek[4] );
204
0
        uint64_t i_chunk_size = UINT64_C( 8 ) + i_data_size + ( i_data_size & 1 );
205
206
0
        msg_Dbg( p_demux, "chunk fcc=%4.4s size=%" PRIu64 " data_size=%" PRIu32,
207
0
            p_peek, i_chunk_size, i_data_size );
208
209
0
        if( !memcmp( p_peek, "COMM", 4 ) )
210
0
        {
211
0
            if( vlc_stream_Peek( p_demux->s, &p_peek, 18+8 ) < 18+8 )
212
0
                goto error;
213
214
0
            p_sys->fmt.audio.i_channels = GetWBE( &p_peek[8] );
215
0
            p_sys->fmt.audio.i_bitspersample = GetWBE( &p_peek[14] );
216
0
            p_sys->fmt.audio.i_rate     = GetF80BE( &p_peek[16] );
217
218
0
            msg_Dbg( p_demux, "COMM: channels=%d samples_frames=%d bits=%d rate=%d",
219
0
                     GetWBE( &p_peek[8] ), GetDWBE( &p_peek[10] ), GetWBE( &p_peek[14] ),
220
0
                     GetF80BE( &p_peek[16] ) );
221
0
        }
222
0
        else if( !memcmp( p_peek, "SSND", 4 ) )
223
0
        {
224
0
            if( vlc_stream_Peek( p_demux->s, &p_peek, 8+8 ) < 8+8 )
225
0
                goto error;
226
227
0
            p_sys->i_ssnd_pos = vlc_stream_Tell( p_demux->s );
228
0
            p_sys->i_ssnd_size = i_data_size;
229
0
            p_sys->i_ssnd_offset = GetDWBE( &p_peek[8] );
230
0
            p_sys->i_ssnd_blocksize = GetDWBE( &p_peek[12] );
231
232
0
            msg_Dbg( p_demux, "SSND: (offset=%d blocksize=%d)",
233
0
                     p_sys->i_ssnd_offset, p_sys->i_ssnd_blocksize );
234
0
        }
235
0
        else if( !memcmp( p_peek, "CHAN", 4 ) && i_chunk_size > 8 + 12 )
236
0
        {
237
0
            ssize_t ret = vlc_stream_Peek( p_demux->s, &p_peek, i_chunk_size );
238
0
            if( ret == -1 || (size_t) ret != i_chunk_size )
239
0
                goto error;
240
241
0
            struct CoreAudio_layout_s layout;
242
0
            layout.i_channels_layout_tag = GetDWBE( &p_peek[8] );
243
0
            layout.i_channels_bitmap = GetDWBE( &p_peek[8+4] );
244
0
            layout.i_channels_description_count = GetDWBE( &p_peek[8+8] );
245
            /* TODO: handle CoreAudio_layout_s.p_descriptions */
246
247
0
            if ( CoreAudio_Layout_to_vlc( &layout,
248
0
                                          &p_sys->fmt.audio.i_physical_channels,
249
0
                                          &p_sys->fmt.audio.i_channels,
250
0
                                          &pi_channels_in ) != VLC_SUCCESS )
251
0
                msg_Warn( p_demux, "discarding chan mapping" );
252
0
        }
253
0
        else
254
0
        {
255
0
            int i_ret = ReadTextChunk( p_demux, i_chunk_size, i_data_size );
256
257
0
            if( unlikely(i_ret == VLC_ENOMEM) )
258
0
                goto error;
259
0
        }
260
261
        /* consume chunk data */
262
0
        for( ssize_t i_req; i_chunk_size; i_chunk_size -= i_req )
263
0
        {
264
0
#if SSIZE_MAX < UINT64_MAX
265
0
            i_req = __MIN( SSIZE_MAX, i_chunk_size );
266
#else
267
            i_req = i_chunk_size;
268
#endif
269
0
            if( vlc_stream_Read( p_demux->s, NULL, i_req ) != i_req )
270
0
            {
271
0
                msg_Warn( p_demux, "incomplete file" );
272
0
                goto error;
273
0
            }
274
0
        }
275
0
    }
276
277
0
    if( pi_channels_in != NULL )
278
0
    {
279
0
        p_sys->i_chans_to_reorder =
280
0
            aout_CheckChannelReorder( pi_channels_in, NULL,
281
0
                                      p_sys->fmt.audio.i_physical_channels,
282
0
                                      p_sys->pi_chan_table ) > 0;
283
0
        p_sys->audio_fourcc =
284
0
            vlc_fourcc_GetCodecAudio( p_sys->fmt.i_codec,
285
0
                                      p_sys->fmt.audio.i_bitspersample );
286
0
        if( p_sys->audio_fourcc == 0 )
287
0
            p_sys->i_chans_to_reorder = 0;
288
0
    }
289
290
0
    p_sys->i_ssnd_start = p_sys->i_ssnd_pos + 16 + p_sys->i_ssnd_offset;
291
0
    p_sys->i_ssnd_end   = p_sys->i_ssnd_start + p_sys->i_ssnd_size;
292
293
0
    p_sys->i_ssnd_fsize = p_sys->fmt.audio.i_channels *
294
0
                          ((p_sys->fmt.audio.i_bitspersample + 7) / 8);
295
296
0
    if( p_sys->i_ssnd_fsize <= 0 || p_sys->fmt.audio.i_rate == 0 || p_sys->i_ssnd_pos < 12 )
297
0
    {
298
0
        msg_Err( p_demux, "invalid audio parameters" );
299
0
        goto error;
300
0
    }
301
302
0
    if( p_sys->i_ssnd_size <= 0 )
303
0
    {
304
        /* unknown */
305
0
        p_sys->i_ssnd_end = 0;
306
0
    }
307
308
    /* seek into SSND chunk */
309
0
    if( vlc_stream_Seek( p_demux->s, p_sys->i_ssnd_start ) )
310
0
    {
311
0
        msg_Err( p_demux, "cannot seek to data chunk" );
312
0
        goto error;
313
0
    }
314
315
    /* */
316
0
    p_sys->fmt.i_id = 0;
317
0
    p_sys->es = es_out_Add( p_demux->out, &p_sys->fmt );
318
0
    if( unlikely(p_sys->es == NULL) )
319
0
        goto error;
320
321
0
    p_demux->pf_demux = Demux;
322
0
    p_demux->pf_control = Control;
323
324
0
    return VLC_SUCCESS;
325
326
0
error:
327
0
    Close( p_this );
328
0
    return VLC_EGENERIC;
329
0
}
330
331
/*****************************************************************************
332
 * Demux:
333
 *****************************************************************************/
334
static int Demux( demux_t *p_demux )
335
0
{
336
0
    demux_sys_t *p_sys = p_demux->p_sys;
337
0
    int64_t     i_tell = vlc_stream_Tell( p_demux->s );
338
339
0
    block_t     *p_block;
340
0
    int         i_read;
341
342
0
    if( p_sys->i_ssnd_end > 0 && i_tell >= p_sys->i_ssnd_end )
343
0
    {
344
        /* EOF */
345
0
        return VLC_DEMUXER_EOF;
346
0
    }
347
348
    /* Set PCR */
349
0
    es_out_SetPCR( p_demux->out, VLC_TICK_0 + p_sys->i_time);
350
351
    /* we will read 100ms at once */
352
0
    i_read = p_sys->i_ssnd_fsize * ( p_sys->fmt.audio.i_rate / 10 );
353
0
    if( p_sys->i_ssnd_end > 0 && p_sys->i_ssnd_end - i_tell < i_read )
354
0
    {
355
0
        i_read = p_sys->i_ssnd_end - i_tell;
356
0
    }
357
0
    if( ( p_block = vlc_stream_Block( p_demux->s, i_read ) ) == NULL )
358
0
    {
359
0
        return VLC_DEMUXER_EOF;
360
0
    }
361
362
0
    p_block->i_dts =
363
0
    p_block->i_pts = VLC_TICK_0 + p_sys->i_time;
364
365
0
    p_sys->i_time += vlc_tick_from_samples(p_block->i_buffer,
366
0
                                           p_sys->i_ssnd_fsize) /
367
0
                     p_sys->fmt.audio.i_rate;
368
369
0
    if( p_sys->i_chans_to_reorder )
370
0
        aout_ChannelReorder( p_block->p_buffer, p_block->i_buffer,
371
0
                             p_sys->i_chans_to_reorder, p_sys->pi_chan_table,
372
0
                             p_sys->audio_fourcc );
373
    /* */
374
0
    es_out_Send( p_demux->out, p_sys->es, p_block );
375
0
    return VLC_DEMUXER_SUCCESS;
376
0
}
377
378
/*****************************************************************************
379
 * Close: frees unused data
380
 *****************************************************************************/
381
static void Close( vlc_object_t * p_this )
382
0
{
383
0
    demux_t     *p_demux = (demux_t*)p_this;
384
0
    demux_sys_t *p_sys = p_demux->p_sys;
385
386
0
    if( p_sys->p_meta )
387
0
        vlc_meta_Delete( p_sys->p_meta );
388
0
}
389
390
/*****************************************************************************
391
 * Control:
392
 *****************************************************************************/
393
static int Control( demux_t *p_demux, int i_query, va_list args )
394
0
{
395
0
    demux_sys_t *p_sys = p_demux->p_sys;
396
0
    double f, *pf;
397
398
0
    switch( i_query )
399
0
    {
400
0
        case DEMUX_CAN_SEEK:
401
0
            return vlc_stream_vaControl( p_demux->s, i_query, args );
402
403
0
        case DEMUX_GET_POSITION:
404
0
        {
405
0
            int64_t i_start = p_sys->i_ssnd_start;
406
0
            int64_t i_end   = p_sys->i_ssnd_end > 0 ? p_sys->i_ssnd_end : stream_Size( p_demux->s );
407
0
            int64_t i_tell  = vlc_stream_Tell( p_demux->s );
408
409
0
            pf = va_arg( args, double * );
410
411
0
            if( i_start < i_end )
412
0
            {
413
0
                *pf = (double)(i_tell - i_start)/(double)(i_end - i_start);
414
0
                return VLC_SUCCESS;
415
0
            }
416
0
            return VLC_EGENERIC;
417
0
        }
418
419
0
        case DEMUX_SET_POSITION:
420
0
        {
421
0
            int64_t i_start = p_sys->i_ssnd_start;
422
0
            int64_t i_end  = p_sys->i_ssnd_end > 0 ? p_sys->i_ssnd_end : stream_Size( p_demux->s );
423
424
0
            f = va_arg( args, double );
425
426
0
            if( i_start < i_end )
427
0
            {
428
0
                int     i_frame = (f * ( i_end - i_start )) / p_sys->i_ssnd_fsize;
429
0
                int64_t i_new   = i_start + i_frame * p_sys->i_ssnd_fsize;
430
431
0
                if( vlc_stream_Seek( p_demux->s, i_new ) )
432
0
                {
433
0
                    return VLC_EGENERIC;
434
0
                }
435
0
                p_sys->i_time = vlc_tick_from_samples( i_frame, p_sys->fmt.audio.i_rate );
436
0
                return VLC_SUCCESS;
437
0
            }
438
0
            return VLC_EGENERIC;
439
0
        }
440
441
0
        case DEMUX_GET_TIME:
442
0
            *va_arg( args, vlc_tick_t * ) = p_sys->i_time;
443
0
            return VLC_SUCCESS;
444
445
0
        case DEMUX_GET_LENGTH:
446
0
        {
447
0
            int64_t i_end  = p_sys->i_ssnd_end > 0 ? p_sys->i_ssnd_end : stream_Size( p_demux->s );
448
449
0
            if( p_sys->i_ssnd_start < i_end )
450
0
            {
451
0
                *va_arg( args, vlc_tick_t * ) =
452
0
                    vlc_tick_from_samples( i_end - p_sys->i_ssnd_start, p_sys->i_ssnd_fsize) / p_sys->fmt.audio.i_rate;
453
0
                return VLC_SUCCESS;
454
0
            }
455
0
            return VLC_EGENERIC;
456
0
        }
457
0
        case DEMUX_GET_META:
458
0
        {
459
0
            vlc_meta_t *p_meta = va_arg( args, vlc_meta_t * );
460
0
            if( !p_sys->p_meta )
461
0
                return VLC_EGENERIC;
462
0
            vlc_meta_Merge( p_meta, p_sys->p_meta );
463
0
            return VLC_SUCCESS;
464
0
        }
465
466
0
        case DEMUX_SET_TIME:
467
0
        case DEMUX_GET_FPS:
468
0
            return VLC_EGENERIC;
469
470
0
        case DEMUX_CAN_PAUSE:
471
0
        case DEMUX_SET_PAUSE_STATE:
472
0
        case DEMUX_CAN_CONTROL_PACE:
473
0
        case DEMUX_GET_PTS_DELAY:
474
0
             return demux_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args );
475
476
0
        default:
477
0
            return VLC_EGENERIC;
478
0
    }
479
0
}