latmenc.c 8.42 KB
Newer Older
Kieran Kunhya's avatar
Kieran Kunhya committed
1 2 3 4
/*
 * LATM/LOAS muxer
 * Copyright (c) 2011 Kieran Kunhya <kieran@kunhya.com>
 *
5
 * This file is part of FFmpeg.
Kieran Kunhya's avatar
Kieran Kunhya committed
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
Kieran Kunhya's avatar
Kieran Kunhya committed
8 9 10 11
 * 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.
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
Kieran Kunhya's avatar
Kieran Kunhya committed
13 14 15 16 17
 * 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
18
 * License along with FFmpeg; if not, write to the Free Software
Kieran Kunhya's avatar
Kieran Kunhya committed
19 20 21 22 23 24 25 26 27
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "libavcodec/get_bits.h"
#include "libavcodec/put_bits.h"
#include "libavcodec/avcodec.h"
#include "libavcodec/mpeg4audio.h"
#include "libavutil/opt.h"
#include "avformat.h"
28
#include "internal.h"
29
#include "rawenc.h"
Kieran Kunhya's avatar
Kieran Kunhya committed
30

31 32
#define MAX_EXTRADATA_SIZE 1024

33
typedef struct LATMContext {
34
    AVClass *av_class;
Kieran Kunhya's avatar
Kieran Kunhya committed
35 36 37 38 39
    int off;
    int channel_conf;
    int object_type;
    int counter;
    int mod;
40
    uint8_t buffer[0x1fff + MAX_EXTRADATA_SIZE + 1024];
Kieran Kunhya's avatar
Kieran Kunhya committed
41 42 43 44
} LATMContext;

static const AVOption options[] = {
    {"smc-interval", "StreamMuxConfig interval.",
45
     offsetof(LATMContext, mod), AV_OPT_TYPE_INT, {.i64 = 0x0014}, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM},
Kieran Kunhya's avatar
Kieran Kunhya committed
46 47 48 49 50 51 52 53 54 55
    {NULL},
};

static const AVClass latm_muxer_class = {
    .class_name = "LATM/LOAS muxer",
    .item_name  = av_default_item_name,
    .option     = options,
    .version    = LIBAVUTIL_VERSION_INT,
};

56
static int latm_decode_extradata(AVFormatContext *s, uint8_t *buf, int size)
Kieran Kunhya's avatar
Kieran Kunhya committed
57
{
58
    LATMContext *ctx = s->priv_data;
Kieran Kunhya's avatar
Kieran Kunhya committed
59 60
    MPEG4AudioConfig m4ac;

61
    if (size > MAX_EXTRADATA_SIZE) {
62
        av_log(s, AV_LOG_ERROR, "Extradata is larger than currently supported.\n");
63 64
        return AVERROR_INVALIDDATA;
    }
65
    ctx->off = avpriv_mpeg4audio_get_config2(&m4ac, buf, size, 1, s);
Kieran Kunhya's avatar
Kieran Kunhya committed
66 67 68
    if (ctx->off < 0)
        return ctx->off;

69 70
    if (ctx->object_type == AOT_ALS && (ctx->off & 7)) {
        // as long as avpriv_mpeg4audio_get_config works correctly this is impossible
71
        av_log(s, AV_LOG_ERROR, "BUG: ALS offset is not byte-aligned\n");
72 73
        return AVERROR_INVALIDDATA;
    }
Kieran Kunhya's avatar
Kieran Kunhya committed
74 75 76
    /* FIXME: are any formats not allowed in LATM? */

    if (m4ac.object_type > AOT_SBR && m4ac.object_type != AOT_ALS) {
77
        av_log(s, AV_LOG_ERROR, "Muxing MPEG-4 AOT %d in LATM is not supported\n", m4ac.object_type);
Kieran Kunhya's avatar
Kieran Kunhya committed
78 79 80 81 82 83 84 85 86 87
        return AVERROR_INVALIDDATA;
    }
    ctx->channel_conf = m4ac.chan_config;
    ctx->object_type  = m4ac.object_type;

    return 0;
}

static int latm_write_header(AVFormatContext *s)
{
88
    AVCodecParameters *par = s->streams[0]->codecpar;
Kieran Kunhya's avatar
Kieran Kunhya committed
89

90
    if (par->codec_id == AV_CODEC_ID_AAC_LATM)
91
        return 0;
92
    if (par->codec_id != AV_CODEC_ID_AAC && par->codec_id != AV_CODEC_ID_MP4ALS) {
93
        av_log(s, AV_LOG_ERROR, "Only AAC, LATM and ALS are supported\n");
94
        return AVERROR(EINVAL);
95
    }
96

97
    if (par->extradata_size > 0 &&
98
        latm_decode_extradata(s, par->extradata, par->extradata_size) < 0)
Kieran Kunhya's avatar
Kieran Kunhya committed
99 100 101 102 103
        return AVERROR_INVALIDDATA;

    return 0;
}

104
static void latm_write_frame_header(AVFormatContext *s, PutBitContext *bs)
Kieran Kunhya's avatar
Kieran Kunhya committed
105 106
{
    LATMContext *ctx = s->priv_data;
107
    AVCodecParameters *par = s->streams[0]->codecpar;
Kieran Kunhya's avatar
Kieran Kunhya committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    int header_size;

    /* AudioMuxElement */
    put_bits(bs, 1, !!ctx->counter);

    if (!ctx->counter) {
        /* StreamMuxConfig */
        put_bits(bs, 1, 0); /* audioMuxVersion */
        put_bits(bs, 1, 1); /* allStreamsSameTimeFraming */
        put_bits(bs, 6, 0); /* numSubFrames */
        put_bits(bs, 4, 0); /* numProgram */
        put_bits(bs, 3, 0); /* numLayer */

        /* AudioSpecificConfig */
        if (ctx->object_type == AOT_ALS) {
123 124
            header_size = par->extradata_size-(ctx->off >> 3);
            avpriv_copy_bits(bs, &par->extradata[ctx->off >> 3], header_size);
Kieran Kunhya's avatar
Kieran Kunhya committed
125
        } else {
126 127
            // + 3 assumes not scalable and dependsOnCoreCoder == 0,
            // see decode_ga_specific_config in libavcodec/aacdec.c
128
            avpriv_copy_bits(bs, par->extradata, ctx->off + 3);
Kieran Kunhya's avatar
Kieran Kunhya committed
129 130

            if (!ctx->channel_conf) {
131
                GetBitContext gb;
132
                int ret = init_get_bits8(&gb, par->extradata, par->extradata_size);
133
                av_assert0(ret >= 0); // extradata size has been checked already, so this should not fail
134
                skip_bits_long(&gb, ctx->off + 3);
135
                ff_copy_pce_data(bs, &gb);
Kieran Kunhya's avatar
Kieran Kunhya committed
136 137 138 139
            }
        }

        put_bits(bs, 3, 0); /* frameLengthType */
140
        put_bits(bs, 8, 0xff); /* latmBufferFullness */
Kieran Kunhya's avatar
Kieran Kunhya committed
141 142 143 144 145 146 147 148 149 150 151

        put_bits(bs, 1, 0); /* otherDataPresent */
        put_bits(bs, 1, 0); /* crcCheckPresent */
    }

    ctx->counter++;
    ctx->counter %= ctx->mod;
}

static int latm_write_packet(AVFormatContext *s, AVPacket *pkt)
{
152
    LATMContext *ctx = s->priv_data;
153
    AVCodecParameters *par = s->streams[0]->codecpar;
Kieran Kunhya's avatar
Kieran Kunhya committed
154 155 156 157 158
    AVIOContext *pb = s->pb;
    PutBitContext bs;
    int i, len;
    uint8_t loas_header[] = "\x56\xe0\x00";

159
    if (par->codec_id == AV_CODEC_ID_AAC_LATM)
160 161
        return ff_raw_write_packet(s, pkt);

162
    if (!par->extradata) {
163 164 165
        if(pkt->size > 2 && pkt->data[0] == 0x56 && (pkt->data[1] >> 4) == 0xe &&
            (AV_RB16(pkt->data + 1) & 0x1FFF) + 3 == pkt->size)
            return ff_raw_write_packet(s, pkt);
166 167 168 169 170 171 172
        else {
            uint8_t *side_data;
            int side_data_size = 0, ret;

            side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
                                                &side_data_size);
            if (side_data_size) {
173
                if (latm_decode_extradata(s, side_data, side_data_size) < 0)
174 175 176 177 178
                    return AVERROR_INVALIDDATA;
                ret = ff_alloc_extradata(par, side_data_size);
                if (ret < 0)
                    return ret;
                memcpy(par->extradata, side_data, side_data_size);
179 180
            } else
                return AVERROR_INVALIDDATA;
181
        }
182 183
    }

184 185
    if (pkt->size > 0x1fff)
        goto too_large;
Kieran Kunhya's avatar
Kieran Kunhya committed
186

187
    init_put_bits(&bs, ctx->buffer, pkt->size+1024+MAX_EXTRADATA_SIZE);
Kieran Kunhya's avatar
Kieran Kunhya committed
188 189 190 191 192 193 194 195 196 197 198

    latm_write_frame_header(s, &bs);

    /* PayloadLengthInfo() */
    for (i = 0; i <= pkt->size-255; i+=255)
        put_bits(&bs, 8, 255);

    put_bits(&bs, 8, pkt->size-i);

    /* The LATM payload is written unaligned */

199
    /* PayloadMux() */
200 201 202 203 204 205 206 207 208 209
    if (pkt->size && (pkt->data[0] & 0xe1) == 0x81) {
        // Convert byte-aligned DSE to non-aligned.
        // Due to the input format encoding we know that
        // it is naturally byte-aligned in the input stream,
        // so there are no padding bits to account for.
        // To avoid having to add padding bits and rearrange
        // the whole stream we just remove the byte-align flag.
        // This allows us to remux our FATE AAC samples into latm
        // files that are still playable with minimal effort.
        put_bits(&bs, 8, pkt->data[0] & 0xfe);
210 211 212
        avpriv_copy_bits(&bs, pkt->data + 1, 8*pkt->size - 8);
    } else
        avpriv_copy_bits(&bs, pkt->data, 8*pkt->size);
Kieran Kunhya's avatar
Kieran Kunhya committed
213

214
    avpriv_align_put_bits(&bs);
Kieran Kunhya's avatar
Kieran Kunhya committed
215 216 217 218
    flush_put_bits(&bs);

    len = put_bits_count(&bs) >> 3;

219 220 221
    if (len > 0x1fff)
        goto too_large;

Kieran Kunhya's avatar
Kieran Kunhya committed
222 223 224 225
    loas_header[1] |= (len >> 8) & 0x1f;
    loas_header[2] |= len & 0xff;

    avio_write(pb, loas_header, 3);
226
    avio_write(pb, ctx->buffer, len);
Kieran Kunhya's avatar
Kieran Kunhya committed
227 228

    return 0;
229 230 231 232

too_large:
    av_log(s, AV_LOG_ERROR, "LATM packet size larger than maximum size 0x1fff\n");
    return AVERROR_INVALIDDATA;
Kieran Kunhya's avatar
Kieran Kunhya committed
233 234
}

235 236 237 238 239
static int latm_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
{
    int ret = 1;
    AVStream *st = s->streams[pkt->stream_index];

240
    if (st->codecpar->codec_id == AV_CODEC_ID_AAC) {
241 242 243 244 245 246 247
        if (pkt->size > 2 && (AV_RB16(pkt->data) & 0xfff0) == 0xfff0)
            ret = ff_stream_add_bitstream_filter(st, "aac_adtstoasc", NULL);
    }

    return ret;
}

Kieran Kunhya's avatar
Kieran Kunhya committed
248 249 250 251
AVOutputFormat ff_latm_muxer = {
    .name           = "latm",
    .long_name      = NULL_IF_CONFIG_SMALL("LOAS/LATM"),
    .mime_type      = "audio/MP4A-LATM",
252
    .extensions     = "latm,loas",
Kieran Kunhya's avatar
Kieran Kunhya committed
253
    .priv_data_size = sizeof(LATMContext),
254 255
    .audio_codec    = AV_CODEC_ID_AAC,
    .video_codec    = AV_CODEC_ID_NONE,
Kieran Kunhya's avatar
Kieran Kunhya committed
256 257 258
    .write_header   = latm_write_header,
    .write_packet   = latm_write_packet,
    .priv_class     = &latm_muxer_class,
259
    .check_bitstream= latm_check_bitstream,
260
    .flags          = AVFMT_NOTIMESTAMPS,
Kieran Kunhya's avatar
Kieran Kunhya committed
261
};