aboutsummaryrefslogtreecommitdiff
path: root/lib/ffmpeg/libavcodec/latmaac.c
blob: c2241649fcf9dc49fb0b15b018e422d291dc6845 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
 * copyright (c) 2008 Paul Kendall <paul@kcbbs.gen.nz>
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
 * @file latmaac.c
 * LATM wrapped AAC decoder
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <sys/types.h>

#include "parser.h"
#include "get_bits.h"
#include "put_bits.h"
#include "mpeg4audio.h"
#include "neaacdec.h"

#define min(a,b) ((a)<(b) ? (a) : (b))

/*
    Note: This decoder filter is intended to decode LATM streams transferred
    in MPEG transport streams which only contain one program.
    To do a more complex LATM demuxing a separate LATM demuxer should be used.
*/

#define SYNC_LATM   0x2b7       // 11 bits
#define MAX_SIZE    8*1024

typedef struct AACDecoder
{
    faacDecHandle   aac_decoder;
    uint8_t         initialized;

    // parser data
    uint8_t         audio_mux_version_A;
    uint8_t         frameLengthType;
    uint8_t         extra[64];            // should be way enough
    int             extrasize;
} AACDecoder;

static inline int64_t latm_get_value(GetBitContext *b)
{
    uint8_t bytesForValue = get_bits(b, 2);
    int64_t value = 0;
    int i;
    for (i=0; i<=bytesForValue; i++) {
        value <<= 8;
        value |= get_bits(b, 8);
    }
    return value;
}

static void readGASpecificConfig(int audioObjectType, GetBitContext *b, PutBitContext *o)
{
    int framelen_flag;
    int dependsOnCoder;
    int ext_flag;

    framelen_flag = get_bits(b, 1);
    put_bits(o, 1, framelen_flag);
    dependsOnCoder = get_bits(b, 1);
    put_bits(o, 1, dependsOnCoder);
    if (dependsOnCoder) {
        int delay = get_bits(b, 14);
        put_bits(o, 14, delay);
    }
    ext_flag = get_bits(b, 1);
    put_bits(o, 1, ext_flag);
    
    if (audioObjectType == 6 || audioObjectType == 20) {
        int layerNr = get_bits(b, 3);
        put_bits(o, 3, layerNr);
    }
    if (ext_flag) {
        if (audioObjectType == 22) {
            skip_bits(b, 5);                    // numOfSubFrame
            skip_bits(b, 11);                   // layer_length

            put_bits(o, 16, 0);
        }
        if (audioObjectType == 17 ||
                audioObjectType == 19 ||
                audioObjectType == 20 ||
                audioObjectType == 23) {

            skip_bits(b, 3);                    // stuff
            put_bits(o, 3, 0);
        }

        skip_bits(b, 1);                        // extflag3
        put_bits(o, 1, 0);
    }
}

static int readAudioSpecificConfig(struct AACDecoder *decoder, GetBitContext *b)
{
    PutBitContext o;
    int ret = 0;
    int audioObjectType;
    int samplingFrequencyIndex;
    int channelConfiguration;

    init_put_bits(&o, decoder->extra, sizeof(decoder->extra));

    audioObjectType = get_bits(b, 5);
    put_bits(&o, 5, audioObjectType);
    if (audioObjectType == 31) {
        uint8_t extended = get_bits(b, 6);
        put_bits(&o, 6, extended);
        audioObjectType = 32 + extended;
    }

    samplingFrequencyIndex = get_bits(b, 4);
    put_bits(&o, 4, samplingFrequencyIndex);
    if (samplingFrequencyIndex == 0x0f) {
        uint32_t f = get_bits_long(b, 24);
        put_bits(&o, 24, f);
    }
    channelConfiguration = get_bits(b, 4);
    put_bits(&o, 4, channelConfiguration);

    if (audioObjectType == 1 || audioObjectType == 2 || audioObjectType == 3
            || audioObjectType == 4 || audioObjectType == 6 || audioObjectType == 7) {
        readGASpecificConfig(audioObjectType, b, &o);
    } else if (audioObjectType == 5) {
        int sbr_present = 1;
        samplingFrequencyIndex = get_bits(b, 4);
        if (samplingFrequencyIndex == 0x0f) {
            uint32_t f = get_bits_long(b, 24);
            put_bits(&o, 24, f);
        }
        audioObjectType = get_bits(b, 5);
        put_bits(&o, 5, audioObjectType);
    } else if (audioObjectType >= 17) {
        int epConfig;
        readGASpecificConfig(audioObjectType, b, &o);
        epConfig = get_bits(b, 2);
        put_bits(&o, 2, epConfig);
    }

    // count the extradata
    ret = put_bits_count(&o);
    decoder->extrasize = (ret + 7) / 8;

    flush_put_bits(&o);
    return ret;
}

static void readStreamMuxConfig(struct AACDecoder *parser, GetBitContext *b)
{
    int audio_mux_version = get_bits(b, 1);
    parser->audio_mux_version_A = 0;
    if (audio_mux_version == 1) {                // audioMuxVersion
        parser->audio_mux_version_A = get_bits(b, 1);
    }

    if (parser->audio_mux_version_A == 0) {
        int frame_length_type;

        if (audio_mux_version == 1) {
            // taraFullness
            latm_get_value(b);
        }
        get_bits(b, 1);                    // allStreamSameTimeFraming = 1
        get_bits(b, 6);                    // numSubFrames = 0
        get_bits(b, 4);                    // numPrograms = 0

        // for each program (which there is only on in DVB)
        get_bits(b, 3);                    // numLayer = 0

        // for each layer (which there is only on in DVB)
        if (audio_mux_version == 0) {
            readAudioSpecificConfig(parser, b);
        } else {
            int ascLen = latm_get_value(b);
            ascLen -= readAudioSpecificConfig(parser, b);

            // skip left over bits
            while (ascLen > 16) {
                skip_bits(b, 16);
                ascLen -= 16;
            }
            skip_bits(b, ascLen);
        }

        // these are not needed... perhaps
        frame_length_type = get_bits(b, 3);
        parser->frameLengthType = frame_length_type;
        if (frame_length_type == 0) {
            get_bits(b, 8);
        } else if (frame_length_type == 1) {
            get_bits(b, 9);
        } else if (frame_length_type == 3 || frame_length_type == 4 || frame_length_type == 5) {
            // celp_table_index
            get_bits(b, 6);
        } else if (frame_length_type == 6 || frame_length_type == 7) {
            // hvxc_table_index
            get_bits(b, 1);
        }

        // other data
        if (get_bits(b, 1)) {
            // other data present
            if (audio_mux_version == 1) {
                // other_data_bits
                latm_get_value(b);
            } else {
                int esc, tmp;
                // other data bits
                int64_t other_data_bits = 0;
                do {
                    esc = get_bits(b, 1);
                    tmp = get_bits(b, 8);
                    other_data_bits = other_data_bits << 8 | tmp;
                } while (esc);
            }
        }

        // CRC if necessary
        if (get_bits(b, 1)) {
            // config_crc
            get_bits(b, 8);
        }
    } else {
        // TBD
    }
}

static int readPayloadLengthInfo(struct AACDecoder *parser, GetBitContext *b)
{
    if (parser->frameLengthType == 0) {
        uint8_t tmp;
        int muxSlotLengthBytes = 0;
        do {
            tmp = get_bits(b, 8);
            muxSlotLengthBytes += tmp;
        } while (tmp == 255);
        return muxSlotLengthBytes;
    } else {
        if (parser->frameLengthType == 3 ||
                parser->frameLengthType == 5 ||
                parser->frameLengthType == 7) {
            get_bits(b, 2);
        }
        return 0;
    }
}

static void readAudioMuxElement(struct AACDecoder *parser, GetBitContext *b, uint8_t *payload, int *payloadsize)
{
    uint8_t use_same_mux = get_bits(b, 1);
    if (!use_same_mux) {
        readStreamMuxConfig(parser, b);
    }
    if (parser->audio_mux_version_A == 0) {
        int j;
        int muxSlotLengthBytes = readPayloadLengthInfo(parser, b);
        muxSlotLengthBytes = min(muxSlotLengthBytes, *payloadsize);
        for (j=0; j<muxSlotLengthBytes; j++) {
            *payload++ = get_bits(b, 8);
        }
        *payloadsize = muxSlotLengthBytes;
    }
}

static int readAudioSyncStream(struct AACDecoder *parser, GetBitContext *b, int size, uint8_t *payload, int *payloadsize)
{
    int muxlength;

    if (get_bits(b, 11) != SYNC_LATM) return -1;    // not LATM

    muxlength = get_bits(b, 13);
    if (muxlength+3 > size) return -1;          // not enough data, the parser should have sorted this

    readAudioMuxElement(parser, b, payload, payloadsize);

    return 0;
}

static void channel_setup(AVCodecContext *avctx)
{
    AACDecoder *decoder = avctx->priv_data;
    
    if (avctx->request_channels == 2 && avctx->channels > 2) {
        NeAACDecConfigurationPtr faac_cfg;
        avctx->channels = 2;
        faac_cfg = NeAACDecGetCurrentConfiguration(decoder->aac_decoder);
        if (faac_cfg) {
            faac_cfg->downMatrix = 1;
            faac_cfg->defSampleRate = (!avctx->sample_rate) ? 44100 : avctx->sample_rate;
            NeAACDecSetConfiguration(decoder->aac_decoder, faac_cfg);
        }
    }
}

static int latm_decode_frame(AVCodecContext *avctx, void *out, int *out_size, AVPacket *avpkt)
{
    AACDecoder          *decoder = avctx->priv_data;
    uint8_t             tempbuf[MAX_SIZE];
    int                 bufsize = sizeof(tempbuf);
    int                 max_size = *out_size;
    NeAACDecFrameInfo   info;
    GetBitContext       b;
    
    init_get_bits(&b, avpkt->data, avpkt->size * 8);
    if (readAudioSyncStream(decoder, &b, avpkt->size, tempbuf, &bufsize)) {
        return -1;
    }

    if (!decoder->initialized) {
        // we are going to initialize from decoder specific info when available
        if (decoder->extrasize > 0) {
            if (NeAACDecInit2(decoder->aac_decoder, decoder->extra, decoder->extrasize, &avctx->sample_rate, &avctx->channels)) {
                return -1;
            }
            channel_setup(avctx);
            decoder->initialized = 1;
        } else {
            *out_size = 0;
            return avpkt->size;
        }
    }

    if (!NeAACDecDecode2(decoder->aac_decoder, &info, tempbuf, bufsize, &out, max_size)) {
        return -1;
    }
    *out_size = info.samples * sizeof(short);
    return avpkt->size;
}

static int latm_decode_init(AVCodecContext *avctx)
{
    AACDecoder *decoder = avctx->priv_data;
    NeAACDecConfigurationPtr faac_cfg;

    avctx->bit_rate = 0;
    avctx->sample_fmt = SAMPLE_FMT_S16;
    decoder->aac_decoder = NeAACDecOpen();
    if (!decoder->aac_decoder) {
        return -1;
    }

    faac_cfg = NeAACDecGetCurrentConfiguration(decoder->aac_decoder);
    if (faac_cfg) {
        faac_cfg->outputFormat = FAAD_FMT_16BIT;
        faac_cfg->defSampleRate = (!avctx->sample_rate) ? 44100 : avctx->sample_rate;
        faac_cfg->defObjectType = LC;
        NeAACDecSetConfiguration(decoder->aac_decoder, faac_cfg);
    }

    decoder->initialized = 0;
    return 0;
}

static int latm_decode_end(AVCodecContext *avctx)
{
    AACDecoder *decoder = avctx->priv_data;
    NeAACDecClose(decoder->aac_decoder);
    return 0;
}

AVCodec libfaad_latm_decoder = {
    .name = "AAC/LATM",
    .type = CODEC_TYPE_AUDIO,
    .id = CODEC_ID_AAC_LATM,
    .priv_data_size = sizeof (AACDecoder),
    .init = latm_decode_init,
    .close = latm_decode_end,
    .decode = latm_decode_frame,
    .long_name = "AAC over LATM",
};