Coverage Report

Created: 2025-11-11 06:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vlc/modules/packetizer/mpeg4audio.c
Line
Count
Source
1
/*****************************************************************************
2
 * mpeg4audio.c: parse and packetize an MPEG 4 audio stream
3
 *****************************************************************************
4
 * Copyright (C) 2001, 2002, 2006 VLC authors and VideoLAN
5
 *
6
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7
 *          Gildas Bazin <gbazin@netcourrier.com>
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
28
#ifdef HAVE_CONFIG_H
29
# include "config.h"
30
#endif
31
32
#include <vlc_common.h>
33
#include <vlc_plugin.h>
34
#include <vlc_codec.h>
35
#include <vlc_block.h>
36
#include <vlc_bits.h>
37
38
#include <vlc_block_helper.h>
39
#include "packetizer_helper.h"
40
#include "mpeg4audio.h"
41
42
#include <assert.h>
43
44
/* AAC Config in ES:
45
 *
46
 * AudioObjectType          5 bits
47
 * samplingFrequencyIndex   4 bits
48
 * if (samplingFrequencyIndex == 0xF)
49
 *  samplingFrequency   24 bits
50
 * channelConfiguration     4 bits
51
 * GA_SpecificConfig
52
 *  FrameLengthFlag         1 bit 1024 or 960
53
 *  DependsOnCoreCoder      1 bit (always 0)
54
 *  ExtensionFlag           1 bit (always 0)
55
 */
56
57
/*****************************************************************************
58
 * decoder_sys_t : decoder descriptor
59
 *****************************************************************************/
60
61
typedef struct
62
{
63
    /*
64
     * Input properties
65
     */
66
    int i_state;
67
    int i_type;
68
69
    block_bytestream_t bytestream;
70
71
    /*
72
     * Common properties
73
     */
74
    date_t  end_date;
75
    vlc_tick_t i_pts;
76
    bool b_discontuinity;
77
78
    int i_frame_size;
79
    unsigned int i_channels;
80
    unsigned int i_rate, i_frame_length, i_header_size;
81
    int i_aac_profile;
82
83
    int i_input_rate;
84
85
    /* LOAS */
86
    bool b_latm_cfg;
87
    MPEG4_streammux_config_t latm;
88
89
    int i_warnings;
90
} decoder_sys_t;
91
92
enum
93
{
94
    WARN_CRC_UNSUPPORTED = 1
95
};
96
97
0
#define WARN_ONCE(warn, msg) do{\
98
0
        decoder_sys_t *p_sys = p_dec->p_sys;\
99
0
        if( (p_sys->i_warnings & warn) == 0 )\
100
0
        {\
101
0
            p_sys->i_warnings |= warn;\
102
0
            msg_Warn( p_dec, msg );\
103
0
        }\
104
0
    } while(0)
105
106
enum {
107
    TYPE_UNKNOWN, /* AAC samples with[out] headers */
108
    TYPE_UNKNOWN_NONRAW, /* [un]packetized ADTS or LOAS */
109
    TYPE_RAW,    /* RAW AAC frames */
110
    TYPE_ADTS,
111
    TYPE_LOAS
112
};
113
114
static int ChannelConfigurationToVLC(uint8_t i_channel)
115
1.09k
{
116
1.09k
    if (i_channel == 7)
117
338
        return 8; // 7.1
118
756
    if (i_channel >= 8)
119
234
        return -1;
120
522
    return i_channel;
121
756
}
122
123
static int AOTtoAACProfile(uint8_t i_object_type)
124
0
{
125
0
    switch(i_object_type)
126
0
    {
127
0
        case MPEG4_AOT_AAC_MAIN:
128
0
        case MPEG4_AOT_AAC_LC:
129
0
        case MPEG4_AOT_AAC_SSR:
130
0
        case MPEG4_AOT_AAC_LTP:
131
0
        case MPEG4_AOT_AAC_SBR:
132
0
        case MPEG4_AOT_AAC_SC:
133
0
        case MPEG4_AOT_ER_AAC_LD:
134
0
        case MPEG4_AOT_AAC_PS:
135
0
        case MPEG4_AOT_ER_AAC_ELD:
136
0
            {
137
0
            static_assert(MPEG4_AOT_AAC_MAIN == AAC_PROFILE_MAIN + 1,
138
0
                          "invalid profile to object mapping");
139
0
            return i_object_type - 1;
140
0
            }
141
0
        default:
142
0
            return -1;
143
0
    }
144
0
}
145
146
0
#define ADTS_HEADER_SIZE 9
147
0
#define LOAS_HEADER_SIZE 3
148
149
/****************************************************************************
150
 * Local prototypes
151
 ****************************************************************************/
152
static int  OpenPacketizer(vlc_object_t *);
153
static void ClosePacketizer(vlc_object_t *);
154
155
static block_t *Packetize    (decoder_t *, block_t **);
156
static void     Flush( decoder_t * );
157
158
/*****************************************************************************
159
 * Module descriptor
160
 *****************************************************************************/
161
108
vlc_module_begin ()
162
54
    set_subcategory(SUBCAT_SOUT_PACKETIZER)
163
54
    set_description(N_("MPEG4 audio packetizer"))
164
54
    set_capability("audio packetizer", 50)
165
108
    set_callbacks(OpenPacketizer, ClosePacketizer)
166
54
vlc_module_end ()
167
168
/*****************************************************************************
169
 * OpenPacketizer: probe the packetizer and return score
170
 *****************************************************************************/
171
static int OpenPacketizer(vlc_object_t *p_this)
172
2.83M
{
173
2.83M
    decoder_t *p_dec = (decoder_t*)p_this;
174
2.83M
    decoder_sys_t *p_sys;
175
176
2.83M
    if (p_dec->fmt_in->i_codec != VLC_CODEC_MP4A)
177
2.83M
        return VLC_EGENERIC;
178
179
    /* Allocate the memory needed to store the decoder's structure */
180
2.99k
    if ((p_dec->p_sys = p_sys = (decoder_sys_t *)malloc(sizeof(decoder_sys_t))) == NULL)
181
0
        return VLC_ENOMEM;
182
183
    /* Misc init */
184
2.99k
    p_sys->i_state = STATE_NOSYNC;
185
2.99k
    p_sys->b_discontuinity = false;
186
2.99k
    block_BytestreamInit(&p_sys->bytestream);
187
2.99k
    p_sys->i_aac_profile = -1;
188
2.99k
    p_sys->b_latm_cfg = false;
189
2.99k
    p_sys->i_warnings = 0;
190
191
    /* Set output properties */
192
2.99k
    p_dec->fmt_out.i_codec = VLC_CODEC_MP4A;
193
194
2.99k
    msg_Dbg(p_dec, "running MPEG4 audio packetizer");
195
196
    /*
197
     * We need to handle 3 cases.
198
     * Case 1 : RAW AAC samples without sync header
199
     *          The demuxer shouldn't need packetizer, see next case.
200
     * Case 2 : AAC samples with ADTS or LOAS/LATM header
201
     *          Some mux (avi) can't distinguish the both
202
     *          cases above, and then forwards to packetizer
203
     *          which should check for header and rewire to case below
204
     * Case 3 : Non packetized ADTS or LOAS/LATM
205
     *          The demuxer needs to set original_codec for hardwiring
206
     */
207
208
2.99k
    switch (p_dec->fmt_in->i_original_fourcc)
209
2.99k
    {
210
161
        case VLC_FOURCC('L','A','T','M'):
211
161
            p_sys->i_type = TYPE_LOAS;
212
161
            msg_Dbg(p_dec, "LOAS/LATM Mode");
213
161
            break;
214
215
258
        case VLC_FOURCC('A','D','T','S'):
216
258
            p_sys->i_type = TYPE_ADTS;
217
258
            msg_Dbg(p_dec, "ADTS Mode");
218
258
            break;
219
220
0
        case VLC_FOURCC('H','E','A','D'):
221
0
            p_sys->i_type = TYPE_UNKNOWN_NONRAW;
222
0
            break;
223
224
2.57k
        default:
225
2.57k
            p_sys->i_type = TYPE_UNKNOWN;
226
2.57k
            break;
227
2.99k
    }
228
229
    /* Some mux (avi) do send RAW AAC without extradata,
230
       and LATM can be sent with out-of-band audioconfig,
231
       (avformat sets m4a extradata in both cases)
232
       so we can't rely on extradata to guess multiplexing */
233
2.99k
    p_dec->fmt_out.audio.i_rate = p_dec->fmt_in->audio.i_rate;
234
235
2.99k
    if(p_dec->fmt_in->i_extra)
236
2.42k
    {
237
2.42k
        MPEG4_asc_t asc;
238
2.42k
        bs_t s;
239
2.42k
        bs_init(&s, p_dec->fmt_in->p_extra, p_dec->fmt_in->i_extra);
240
2.42k
        if(MPEG4_read_AudioSpecificConfig(&s, &asc, true) == VLC_SUCCESS)
241
1.09k
        {
242
1.09k
            p_dec->fmt_out.audio.i_rate = asc.i_samplerate;
243
1.09k
            p_dec->fmt_out.audio.i_frame_length = asc.i_frame_length;
244
1.09k
            p_dec->fmt_out.audio.i_channels =
245
1.09k
                    ChannelConfigurationToVLC(asc.i_channel_configuration);
246
1.09k
            if(p_dec->fmt_out.i_profile != -1)
247
0
                p_dec->fmt_out.i_profile = AOTtoAACProfile(asc.i_object_type);
248
249
1.09k
            msg_Dbg(p_dec, "%sAAC%s %dHz %d samples/frame",
250
1.09k
                    (asc.i_sbr) ? "HE-" : "",
251
1.09k
                    (asc.i_ps) ? "v2" : "",
252
1.09k
                    (asc.i_sbr) ? p_dec->fmt_out.audio.i_rate << 1
253
1.09k
                                : p_dec->fmt_out.audio.i_rate,
254
1.09k
                    p_dec->fmt_out.audio.i_frame_length);
255
1.09k
        }
256
257
2.42k
        p_dec->fmt_out.p_extra = malloc(p_dec->fmt_in->i_extra);
258
2.42k
        if (!p_dec->fmt_out.p_extra)
259
0
            return VLC_ENOMEM;
260
2.42k
        p_dec->fmt_out.i_extra = p_dec->fmt_in->i_extra;
261
2.42k
        memcpy(p_dec->fmt_out.p_extra, p_dec->fmt_in->p_extra,
262
2.42k
                p_dec->fmt_in->i_extra);
263
2.42k
    }
264
    /* else() We will try to create a AAC Config from adts/loas */
265
266
2.99k
    date_Init(&p_sys->end_date, p_dec->fmt_out.audio.i_rate ?
267
2.48k
                                p_dec->fmt_out.audio.i_rate : 48000, 1);
268
269
    /* Set callbacks */
270
2.99k
    p_dec->pf_packetize = Packetize;
271
2.99k
    p_dec->pf_flush = Flush;
272
2.99k
    p_dec->pf_get_cc = NULL;
273
274
2.99k
    return VLC_SUCCESS;
275
2.99k
}
276
277
/*****************************************************************************
278
 * ClosePacketizer: clean up the packetizer
279
 *****************************************************************************/
280
static void ClosePacketizer(vlc_object_t *p_this)
281
2.99k
{
282
2.99k
    decoder_t *p_dec = (decoder_t *)p_this;
283
2.99k
    decoder_sys_t *p_sys = p_dec->p_sys;
284
285
2.99k
    block_BytestreamRelease(&p_sys->bytestream);
286
2.99k
    free(p_sys);
287
2.99k
}
288
289
/****************************************************************************
290
 * ForwardRawBlock:
291
 ****************************************************************************
292
 * This function must be fed with complete frames.
293
 ****************************************************************************/
294
static block_t *ForwardRawBlock(decoder_t *p_dec, block_t **pp_block)
295
0
{
296
0
    decoder_sys_t *p_sys = p_dec->p_sys;
297
0
    block_t *p_block;
298
299
0
    if (!pp_block || !*pp_block)
300
0
        return NULL;
301
302
0
    p_block = *pp_block;
303
0
    *pp_block = NULL; /* Don't reuse this block */
304
305
0
    vlc_tick_t i_diff = 0;
306
0
    if (p_block->i_pts != VLC_TICK_INVALID &&
307
0
        p_block->i_pts != date_Get(&p_sys->end_date))
308
0
    {
309
0
        if(date_Get(&p_sys->end_date) != VLC_TICK_INVALID)
310
0
            i_diff = llabs( date_Get(&p_sys->end_date) - p_block->i_pts );
311
0
        date_Set(&p_sys->end_date, p_block->i_pts);
312
0
    }
313
314
0
    p_block->i_pts = p_block->i_dts = date_Get(&p_sys->end_date);
315
316
    /* Might not be known due to missing extradata,
317
       will be set to block pts above */
318
0
    if(p_dec->fmt_out.audio.i_frame_length && p_block->i_pts != VLC_TICK_INVALID)
319
0
    {
320
0
        p_block->i_length = date_Increment(&p_sys->end_date,
321
0
            p_dec->fmt_out.audio.i_frame_length) - p_block->i_pts;
322
323
0
        if( i_diff > p_block->i_length )
324
0
            p_sys->b_discontuinity = true;
325
0
    }
326
327
0
    return p_block;
328
0
}
329
330
/****************************************************************************
331
 * ADTS helpers
332
 ****************************************************************************/
333
static int ADTSSyncInfo(decoder_t * p_dec, const uint8_t * p_buf,
334
                         unsigned int * pi_channels,
335
                         unsigned int * pi_sample_rate,
336
                         unsigned int * pi_frame_length,
337
                         unsigned int * pi_header_size)
338
0
{
339
0
    int i_profile, i_sample_rate_idx, i_frame_size;
340
0
    bool b_crc;
341
342
    /* Fixed header between frames */
343
    //int i_id = ((p_buf[1] >> 3) & 0x01) ? 2 : 4; /* MPEG-2 or 4 */
344
0
    b_crc = !(p_buf[1] & 0x01);
345
0
    i_profile = p_buf[2] >> 6;
346
0
    i_sample_rate_idx = (p_buf[2] >> 2) & 0x0f;
347
0
    *pi_sample_rate = pi_sample_rates[i_sample_rate_idx];
348
    //private_bit = (p_buf[2] >> 1) & 0x01;
349
0
    *pi_channels = ((p_buf[2] & 0x01) << 2) | ((p_buf[3] >> 6) & 0x03);
350
0
    if (*pi_channels == 0) /* workaround broken streams */
351
0
        *pi_channels = 2;
352
    //original_copy = (p_buf[3] >> 5) & 0x01;
353
    //home = (p_buf[3] >> 4) & 0x01;
354
355
    /* Variable header */
356
    //copyright_id_bit = (p_buf[3] >> 3) & 0x01;
357
    //copyright_id_start = (p_buf[3] >> 2) & 0x01;
358
0
    i_frame_size = ((p_buf[3] & 0x03) << 11) | (p_buf[4] << 3) |
359
0
                   ((p_buf[5] >> 5) /*& 0x7*/);
360
    //uint16_t buffer_fullness = ((p_buf[5] & 0x1f) << 6) | (p_buf[6] >> 2);
361
0
    unsigned short i_raw_blocks_in_frame = p_buf[6] & 0x03;
362
363
0
    if (!*pi_sample_rate || !i_frame_size) {
364
0
        msg_Warn(p_dec, "Invalid ADTS header");
365
0
        return 0;
366
0
    }
367
368
0
    *pi_frame_length = 1024;
369
370
0
    if (i_raw_blocks_in_frame == 0) {
371
0
        if (b_crc) {
372
0
            WARN_ONCE(WARN_CRC_UNSUPPORTED, "ADTS CRC not supported");
373
            //uint16_t crc = (p_buf[7] << 8) | p_buf[8];
374
0
        }
375
0
    } else {
376
0
        msg_Err(p_dec, "Multiple blocks per frame in ADTS not supported");
377
0
        return 0;
378
#if 0
379
        int i;
380
        const uint8_t *p_pos = p_buf + 7;
381
        uint16_t crc_block;
382
        uint16_t i_block_pos[3];
383
        if (b_crc) {
384
            for (i = 0 ; i < i_raw_blocks_in_frame ; i++) {
385
                /* the 1st block's position is known ... */
386
                i_block_pos[i] = (*p_pos << 8) | *(p_pos+1);
387
                p_pos += 2;
388
            }
389
            crc_block = (*p_pos << 8) | *(p_pos+1);
390
            p_pos += 2;
391
        }
392
        for (i = 0 ; i <= i_raw_blocks_in_frame ; i++) {
393
            //read 1 block
394
            if (b_crc) {
395
                WARN_ONCE(WARN_CRC_UNSUPPORTED, "ADTS CRC not supported");
396
                //uint16_t crc = (*p_pos << 8) | *(p_pos+1);
397
                //p_pos += 2;
398
            }
399
        }
400
#endif
401
0
    }
402
403
404
    /* Build the decoder specific info header */
405
0
    if (!p_dec->fmt_out.i_extra) {
406
0
        p_dec->fmt_out.p_extra = malloc(2);
407
0
        if (!p_dec->fmt_out.p_extra)
408
0
            return 0;
409
0
        p_dec->fmt_out.i_extra = 2;
410
0
        ((uint8_t *)p_dec->fmt_out.p_extra)[0] =
411
0
            (i_profile + 1) << 3 | (i_sample_rate_idx >> 1);
412
0
        ((uint8_t *)p_dec->fmt_out.p_extra)[1] =
413
0
            ((i_sample_rate_idx & 0x01) << 7) | (*pi_channels <<3);
414
0
    }
415
416
    /* ADTS header length */
417
0
    *pi_header_size = b_crc ? 9 : 7;
418
419
0
    return i_frame_size - *pi_header_size;
420
0
}
421
422
/****************************************************************************
423
 * LOAS helpers
424
 ****************************************************************************/
425
static int LOASSyncInfo(uint8_t p_header[LOAS_HEADER_SIZE], unsigned int *pi_header_size)
426
0
{
427
0
    *pi_header_size = 3;
428
0
    return ((p_header[1] & 0x1f) << 8) + p_header[2];
429
0
}
430
431
static int LOASParse(decoder_t *p_dec, uint8_t *p_buffer, int i_buffer)
432
0
{
433
0
    decoder_sys_t *p_sys = p_dec->p_sys;
434
0
    bs_t s;
435
0
    int i_accumulated = 0;
436
437
0
    bs_init(&s, p_buffer, i_buffer);
438
439
    /* Read the stream mux configuration if present */
440
0
    if (!bs_read1(&s) && !MPEG4_parse_StreamMuxConfig(&s, &p_sys->latm) &&
441
0
            p_sys->latm.i_streams > 0) {
442
0
        const MPEG4_audio_stream_t *st = &p_sys->latm.stream[0];
443
444
0
        if(st->cfg.i_samplerate == 0 || st->cfg.i_frame_length == 0 ||
445
0
           ChannelConfigurationToVLC(st->cfg.i_channel_configuration) == 0)
446
0
            return 0;
447
448
0
        p_sys->i_channels = ChannelConfigurationToVLC(st->cfg.i_channel_configuration);
449
0
        p_sys->i_rate = st->cfg.i_samplerate;
450
0
        p_sys->i_frame_length = st->cfg.i_frame_length;
451
0
        p_sys->i_aac_profile = AOTtoAACProfile(st->cfg.i_object_type);
452
453
0
        if (p_sys->i_channels && p_sys->i_rate && p_sys->i_frame_length > 0)
454
0
        {
455
0
            if((size_t)p_dec->fmt_out.i_extra != st->i_extra ||
456
0
               (p_dec->fmt_out.i_extra > 0 &&
457
0
                memcmp(p_dec->fmt_out.p_extra, st->extra, st->i_extra)) )
458
0
            {
459
0
                if(p_dec->fmt_out.i_extra)
460
0
                    free(p_dec->fmt_out.p_extra);
461
0
                p_dec->fmt_out.p_extra = malloc(st->i_extra);
462
0
                if(p_dec->fmt_out.p_extra)
463
0
                {
464
0
                    p_dec->fmt_out.i_extra = st->i_extra;
465
0
                    memcpy(p_dec->fmt_out.p_extra, st->extra, st->i_extra);
466
0
                    p_sys->b_latm_cfg = true;
467
0
                }
468
0
                else
469
0
                {
470
0
                    p_dec->fmt_out.i_extra = 0;
471
0
                    p_sys->b_latm_cfg = false;
472
0
                }
473
0
            }
474
0
        }
475
0
    }
476
477
    /* Wait for the configuration */
478
0
    if (!p_sys->b_latm_cfg)
479
0
    {
480
        /* WAVE_FORMAT_MPEG_LOAS, configuration provided as AAC header :/ */
481
0
        if( p_dec->fmt_in->i_extra > 0 &&
482
0
            p_sys->i_channels && p_sys->i_rate && p_sys->i_frame_length )
483
0
        {
484
0
            p_sys->b_latm_cfg = true;
485
0
        }
486
0
        else return 0;
487
0
    }
488
489
0
    if(bs_eof(&s) && i_buffer)
490
0
        goto truncated;
491
492
    /* FIXME do we need to split the subframe into independent packet ? */
493
0
    if (p_sys->latm.i_sub_frames > 1)
494
0
        msg_Err(p_dec, "latm sub frames not yet supported, please send a sample");
495
496
0
    for (uint8_t i_sub = 0; i_sub < p_sys->latm.i_sub_frames; i_sub++) {
497
0
        unsigned pi_payload[MPEG4_STREAMMUX_MAX_PROGRAM][MPEG4_STREAMMUX_MAX_LAYER];
498
0
        if (p_sys->latm.b_same_time_framing) {
499
            /* Payload length */
500
0
            for (uint8_t i_program = 0; i_program < p_sys->latm.i_programs; i_program++) {
501
0
                for (uint8_t i_layer = 0; i_layer < p_sys->latm.pi_layers[i_program]; i_layer++) {
502
0
                    MPEG4_audio_stream_t *st = &p_sys->latm.stream[p_sys->latm.pi_stream[i_program][i_layer]];
503
0
                    if (st->i_frame_length_type == 0) {
504
0
                        unsigned i_payload = 0;
505
0
                        for (;;) {
506
0
                            uint8_t i_tmp = bs_read(&s, 8);
507
0
                            i_payload += i_tmp;
508
0
                            if (i_tmp != 255)
509
0
                                break;
510
0
                        }
511
0
                        pi_payload[i_program][i_layer] = i_payload;
512
0
                    } else if (st->i_frame_length_type == 1) {
513
0
                        pi_payload[i_program][i_layer] = st->i_frame_length / 8; /* XXX not correct */
514
0
                    } else if ((st->i_frame_length_type == 3) ||
515
0
                             (st->i_frame_length_type == 5) ||
516
0
                             (st->i_frame_length_type == 7)) {
517
0
                        bs_skip(&s, 2); // muxSlotLengthCoded
518
0
                        pi_payload[i_program][i_layer] = 0; /* TODO */
519
0
                    } else {
520
0
                        pi_payload[i_program][i_layer] = 0; /* TODO */
521
0
                    }
522
0
                }
523
0
            }
524
525
            /* Payload Data */
526
0
            for (uint8_t i_program = 0; i_program < p_sys->latm.i_programs; i_program++) {
527
0
                for (uint8_t i_layer = 0; i_layer < p_sys->latm.pi_layers[i_program]; i_layer++) {
528
                    /* XXX we only extract 1 stream */
529
0
                    if (i_program != 0 || i_layer != 0)
530
0
                        break;
531
532
0
                    if (pi_payload[i_program][i_layer] <= 0)
533
0
                        continue;
534
535
                    /* FIXME that's slow (and a bit ugly to write in place) */
536
0
                    for (unsigned i = 0; i < pi_payload[i_program][i_layer]; i++) {
537
0
                        if (i_accumulated >= i_buffer)
538
0
                            return 0;
539
0
                        p_buffer[i_accumulated++] = bs_read(&s, 8);
540
0
                        if(bs_error(&s))
541
0
                            goto truncated;
542
0
                    }
543
0
                }
544
0
            }
545
0
        } else {
546
0
            const int i_chunks = bs_read(&s, 4);
547
#if 0
548
            int pi_program[16];
549
            int pi_layer[16];
550
#endif
551
552
0
            msg_Err(p_dec, "latm without same time frameing not yet supported, please send a sample");
553
554
0
            for (int i_chunk = 0; i_chunk < i_chunks; i_chunk++) {
555
0
                const int streamIndex = bs_read(&s, 4);
556
0
                MPEG4_audio_stream_t *st = &p_sys->latm.stream[streamIndex];
557
0
                const int i_program = st->i_program;
558
0
                const int i_layer = st->i_layer;
559
560
#if 0
561
                pi_program[i_chunk] = i_program;
562
                pi_layer[i_chunk] = i_layer;
563
#endif
564
565
0
                if (st->i_frame_length_type == 0) {
566
0
                    int i_payload = 0;
567
0
                    for (;;) {
568
0
                        int i_tmp = bs_read(&s, 8);
569
0
                        i_payload += i_tmp;
570
0
                        if (i_tmp != 255)
571
0
                            break;
572
0
                    }
573
0
                    pi_payload[i_program][i_layer] = i_payload;
574
0
                    bs_skip(&s, 1); // auEndFlag
575
0
                } else if (st->i_frame_length_type == 1) {
576
0
                    pi_payload[i_program][i_layer] = st->i_frame_length / 8; /* XXX not correct */
577
0
                } else if ((st->i_frame_length_type == 3) ||
578
0
                         (st->i_frame_length_type == 5) ||
579
0
                         (st->i_frame_length_type == 7)) {
580
0
                    bs_read(&s, 2); // muxSlotLengthCoded
581
0
                }
582
0
            }
583
#if 0
584
            for (int i_chunk = 0; i_chunk < i_chunks; i_chunk++) {
585
                //const int i_program = pi_program[i_chunk];
586
                //const int i_layer = pi_layer[i_chunk];
587
588
                /* TODO ? Payload */
589
            }
590
#endif
591
0
        }
592
0
    }
593
594
#if 0
595
    if (p_sys->latm.i_other_data > 0)
596
        ; // TODO
597
#endif
598
0
    bs_align(&s);
599
600
0
    return i_accumulated;
601
602
0
truncated:
603
0
    msg_Warn(p_dec,"Truncated LOAS packet. Wrong format ?");
604
0
    return 0;
605
0
}
606
607
/*****************************************************************************
608
 *
609
 *****************************************************************************/
610
static void SetupOutput(decoder_t *p_dec, block_t *p_block)
611
0
{
612
0
    decoder_sys_t *p_sys = p_dec->p_sys;
613
614
0
    if (p_dec->fmt_out.audio.i_rate != p_sys->i_rate && p_sys->i_rate > 0)
615
0
    {
616
0
        msg_Info(p_dec, "AAC channels: %d samplerate: %d",
617
0
                  p_sys->i_channels, p_sys->i_rate);
618
0
        date_Change(&p_sys->end_date, p_sys->i_rate, 1);
619
0
    }
620
621
0
    p_dec->fmt_out.audio.i_rate     = p_sys->i_rate;
622
0
    p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
623
0
    p_dec->fmt_out.audio.i_bytes_per_frame = p_sys->i_frame_size;
624
0
    p_dec->fmt_out.audio.i_frame_length = p_sys->i_frame_length;
625
    /* Will reload extradata on change */
626
0
    p_dec->fmt_out.i_profile = p_sys->i_aac_profile;
627
628
#if 0
629
    p_dec->fmt_out.audio.i_physical_channels = p_sys->i_channels_conf;
630
#endif
631
632
0
    p_block->i_pts = p_block->i_dts = date_Get(&p_sys->end_date);
633
634
0
    p_block->i_length =
635
0
        date_Increment(&p_sys->end_date, p_sys->i_frame_length) - p_block->i_pts;
636
0
}
637
638
/*****************************************************************************
639
 * FlushStreamBlock:
640
 *****************************************************************************/
641
static void Flush(decoder_t *p_dec)
642
0
{
643
0
    decoder_sys_t *p_sys = p_dec->p_sys;
644
645
0
    p_sys->i_state = STATE_NOSYNC;
646
0
    block_BytestreamEmpty(&p_sys->bytestream);
647
0
    date_Set(&p_sys->end_date, VLC_TICK_INVALID);
648
0
    p_sys->b_discontuinity = true;
649
0
}
650
651
static inline bool HasADTSHeader( const uint8_t *p_header )
652
0
{
653
0
    return p_header[0] == 0xff && (p_header[1] & 0xf6) == 0xf0;
654
0
}
655
656
static inline bool HasLoasHeader( const uint8_t *p_header )
657
0
{
658
0
    return p_header[0] == 0x56 && (p_header[1] & 0xe0) == 0xe0;
659
0
}
660
661
/****************************************************************************
662
 * PacketizeStreamBlock: ADTS/LOAS packetizer
663
 ****************************************************************************/
664
static block_t *PacketizeStreamBlock(decoder_t *p_dec, block_t **pp_block)
665
0
{
666
0
    decoder_sys_t *p_sys = p_dec->p_sys;
667
0
    uint8_t p_header[ADTS_HEADER_SIZE + LOAS_HEADER_SIZE];
668
0
    block_t *p_out_buffer;
669
0
    uint8_t *p_buf;
670
671
0
    block_t *p_block = pp_block ? *pp_block : NULL;
672
673
0
    if(p_block)
674
0
    {
675
0
        block_BytestreamPush(&p_sys->bytestream, p_block);
676
0
        *pp_block = NULL;
677
0
    }
678
679
0
    for (;;) switch(p_sys->i_state) {
680
0
    case STATE_NOSYNC:
681
0
        while (block_PeekBytes(&p_sys->bytestream, p_header, 2) == VLC_SUCCESS) {
682
            /* Look for sync word - should be 0xfff(adts) or 0x2b7(loas) */
683
0
            if ((p_sys->i_type == TYPE_ADTS || p_sys->i_type == TYPE_UNKNOWN_NONRAW) &&
684
0
                HasADTSHeader( p_header ) )
685
0
            {
686
0
                if (p_sys->i_type != TYPE_ADTS)
687
0
                    msg_Dbg(p_dec, "detected ADTS format");
688
689
0
                p_sys->i_state = STATE_SYNC;
690
0
                p_sys->i_type = TYPE_ADTS;
691
0
                break;
692
0
            }
693
0
            else if ((p_sys->i_type == TYPE_LOAS || p_sys->i_type == TYPE_UNKNOWN_NONRAW) &&
694
0
                      HasLoasHeader( p_header ) )
695
0
            {
696
0
                if (p_sys->i_type != TYPE_LOAS)
697
0
                    msg_Dbg(p_dec, "detected LOAS format");
698
699
0
                p_sys->i_state = STATE_SYNC;
700
0
                p_sys->i_type = TYPE_LOAS;
701
0
                break;
702
0
            }
703
0
            block_SkipByte(&p_sys->bytestream);
704
0
        }
705
0
        if (p_sys->i_state != STATE_SYNC) {
706
0
            block_BytestreamFlush(&p_sys->bytestream);
707
708
            /* Need more data */
709
0
            return NULL;
710
0
        }
711
        /* fallthrough */
712
713
0
    case STATE_SYNC:
714
        /* New frame, set the Presentation Time Stamp */
715
0
        p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
716
0
        if (p_sys->i_pts != VLC_TICK_INVALID &&
717
0
            p_sys->i_pts != date_Get(&p_sys->end_date))
718
0
            date_Set(&p_sys->end_date, p_sys->i_pts);
719
0
        p_sys->i_state = STATE_HEADER;
720
0
        break;
721
722
0
    case STATE_HEADER:
723
0
        if (p_sys->i_type == TYPE_ADTS) {
724
            /* Get ADTS frame header (ADTS_HEADER_SIZE bytes) */
725
0
            if (block_PeekBytes(&p_sys->bytestream, p_header,
726
0
                        ADTS_HEADER_SIZE) != VLC_SUCCESS)
727
0
                return NULL; /* Need more data */
728
729
            /* Check if frame is valid and get frame info */
730
0
            p_sys->i_frame_size = ADTSSyncInfo(p_dec, p_header,
731
0
                    &p_sys->i_channels,
732
0
                    &p_sys->i_rate,
733
0
                    &p_sys->i_frame_length,
734
0
                    &p_sys->i_header_size);
735
0
        } else {
736
0
            assert(p_sys->i_type == TYPE_LOAS);
737
            /* Get LOAS frame header (LOAS_HEADER_SIZE bytes) */
738
0
            if (block_PeekBytes(&p_sys->bytestream, p_header,
739
0
                        LOAS_HEADER_SIZE) != VLC_SUCCESS)
740
0
                return NULL; /* Need more data */
741
742
            /* Check if frame is valid and get frame info */
743
0
            p_sys->i_frame_size = LOASSyncInfo(p_header, &p_sys->i_header_size);
744
0
        }
745
746
0
        if (p_sys->i_frame_size <= 0) {
747
0
            msg_Dbg(p_dec, "emulated sync word");
748
0
            block_SkipByte(&p_sys->bytestream);
749
0
            p_sys->i_state = STATE_NOSYNC;
750
0
            break;
751
0
        }
752
753
0
        p_sys->i_state = STATE_NEXT_SYNC;
754
        /* fallthrough */
755
756
0
    case STATE_NEXT_SYNC:
757
0
        if (p_sys->bytestream.p_block == NULL) {
758
0
            p_sys->i_state = STATE_NOSYNC;
759
0
            block_BytestreamFlush(&p_sys->bytestream);
760
0
            return NULL;
761
0
        }
762
763
        /* Check if next expected frame contains the sync word */
764
0
        if (block_PeekOffsetBytes(&p_sys->bytestream, p_sys->i_frame_size
765
0
                    + p_sys->i_header_size, p_header, 2) != VLC_SUCCESS)
766
0
        {
767
0
            if(p_block == NULL) /* drain */
768
0
            {
769
0
                p_sys->i_state = STATE_SEND_DATA;
770
0
                break;
771
0
            }
772
0
            return NULL; /* Need more data */
773
0
        }
774
775
0
        assert((p_sys->i_type == TYPE_ADTS) || (p_sys->i_type == TYPE_LOAS));
776
0
        if ( (p_sys->i_type == TYPE_ADTS && !HasADTSHeader( p_header )) ||
777
0
             (p_sys->i_type == TYPE_LOAS && !HasLoasHeader( p_header )) )
778
0
        {
779
            /* Check spacial padding case. Failing if need more bytes is ok since
780
               that should have been sent as a whole block */
781
0
            if( block_PeekOffsetBytes(&p_sys->bytestream,
782
0
                                      p_sys->i_frame_size + p_sys->i_header_size,
783
0
                                      p_header, 3) == VLC_SUCCESS &&
784
0
                p_header[0] == 0x00 &&
785
0
               ((p_sys->i_type == TYPE_ADTS && HasADTSHeader( &p_header[1] )) ||
786
0
                (p_sys->i_type == TYPE_LOAS && !HasLoasHeader( &p_header[1] ))))
787
0
            {
788
0
                p_sys->i_state = STATE_SEND_DATA;
789
0
            }
790
0
            else
791
0
            {
792
0
                msg_Dbg(p_dec, "emulated sync word (no sync on following frame)"
793
0
                               " 0x%"PRIx8" 0x%"PRIx8, p_header[0], p_header[1] );
794
0
                p_sys->i_state = STATE_NOSYNC;
795
0
                block_SkipByte(&p_sys->bytestream);
796
0
            }
797
0
            break;
798
0
        }
799
800
0
        p_sys->i_state = STATE_SEND_DATA;
801
0
        break;
802
803
0
    case STATE_GET_DATA:
804
        /* Make sure we have enough data.
805
         * (Not useful if we went through NEXT_SYNC) */
806
0
        if (block_WaitBytes(&p_sys->bytestream, p_sys->i_frame_size +
807
0
                    p_sys->i_header_size) != VLC_SUCCESS)
808
0
            return NULL; /* Need more data */
809
0
        p_sys->i_state = STATE_SEND_DATA;
810
        /* fallthrough */
811
812
0
    case STATE_SEND_DATA:
813
        /* When we reach this point we already know we have enough
814
         * data available. */
815
816
0
        p_out_buffer = block_Alloc(p_sys->i_frame_size);
817
0
        if (!p_out_buffer) {
818
0
            return NULL;
819
0
        }
820
0
        p_buf = p_out_buffer->p_buffer;
821
822
        /* Skip the ADTS/LOAS header */
823
0
        block_SkipBytes(&p_sys->bytestream, p_sys->i_header_size);
824
825
        /* Copy the whole frame into the buffer */
826
0
        block_GetBytes(&p_sys->bytestream, p_buf, p_sys->i_frame_size);
827
0
        if (p_sys->i_type != TYPE_ADTS) { /* parse/extract the whole frame */
828
0
            assert(p_sys->i_type == TYPE_LOAS);
829
0
            p_out_buffer->i_buffer = LOASParse(p_dec, p_buf, p_sys->i_frame_size);
830
0
            if (p_out_buffer->i_buffer <= 0)
831
0
            {
832
0
                if (!p_sys->b_latm_cfg)
833
0
                    msg_Warn(p_dec, "waiting for header");
834
835
0
                block_Release(p_out_buffer);
836
0
                p_out_buffer = NULL;
837
0
                p_sys->i_state = STATE_NOSYNC;
838
0
                break;
839
0
            }
840
0
        }
841
0
        SetupOutput(p_dec, p_out_buffer);
842
        /* Make sure we don't reuse the same pts twice */
843
0
        if (p_sys->i_pts == p_sys->bytestream.p_block->i_pts)
844
0
            p_sys->i_pts = p_sys->bytestream.p_block->i_pts = VLC_TICK_INVALID;
845
846
        /* So p_block doesn't get re-added several times */
847
0
        if( pp_block )
848
0
            *pp_block = block_BytestreamPop(&p_sys->bytestream);
849
850
0
        p_sys->i_state = STATE_NOSYNC;
851
852
0
        return p_out_buffer;
853
0
    }
854
855
0
    return NULL;
856
0
}
857
858
/****************************************************************************
859
 * Packetize: just forwards raw blocks, or packetizes LOAS/ADTS
860
 *            and strips headers
861
 ****************************************************************************/
862
static block_t *Packetize(decoder_t *p_dec, block_t **pp_block)
863
0
{
864
0
    decoder_sys_t *p_sys = p_dec->p_sys;
865
0
    block_t *p_block = pp_block ? *pp_block : NULL;
866
867
0
    if(p_block)
868
0
    {
869
0
        if (p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED))
870
0
        {
871
0
            if(p_sys->i_type == TYPE_ADTS || p_sys->i_type == TYPE_LOAS)
872
0
            {
873
                /* First always drain complete blocks before discontinuity */
874
0
                block_t *p_drain = PacketizeStreamBlock(p_dec, NULL);
875
0
                if(p_drain)
876
0
                    return p_drain;
877
0
            }
878
879
0
            Flush(p_dec);
880
881
0
            if (p_block->i_flags & BLOCK_FLAG_CORRUPTED)
882
0
            {
883
0
                block_Release(p_block);
884
0
                return NULL;
885
0
            }
886
0
        }
887
888
0
        if ( p_block->i_pts == VLC_TICK_INVALID &&
889
0
             date_Get(&p_sys->end_date) == VLC_TICK_INVALID )
890
0
        {
891
            /* We've just started the stream, wait for the first PTS. */
892
0
            block_Release(p_block);
893
0
            return NULL;
894
0
        }
895
0
    }
896
897
0
    if(p_block && p_sys->i_type == TYPE_UNKNOWN)
898
0
    {
899
0
        p_sys->i_type = TYPE_RAW;
900
0
        if(p_block->i_buffer > 1)
901
0
        {
902
0
            if(p_block->p_buffer[0] == 0xff && (p_block->p_buffer[1] & 0xf6) == 0xf0)
903
0
            {
904
0
                p_sys->i_type = TYPE_ADTS;
905
0
            }
906
0
            else if(p_block->p_buffer[0] == 0x56 && (p_block->p_buffer[1] & 0xe0) == 0xe0)
907
0
            {
908
0
                p_sys->i_type = TYPE_LOAS;
909
0
            }
910
0
        }
911
0
    }
912
913
0
    if(p_sys->i_type == TYPE_RAW)
914
0
        p_block = ForwardRawBlock(p_dec, pp_block);
915
0
    else
916
0
        p_block = PacketizeStreamBlock(p_dec, pp_block);
917
918
0
    if(p_block && p_sys->b_discontuinity)
919
0
    {
920
0
        p_block->i_flags |= BLOCK_FLAG_DISCONTINUITY;
921
0
        p_sys->b_discontuinity = false;
922
0
    }
923
924
0
    return p_block;
925
0
}