0ca36b4d |
/*
* LATM/LOAS muxer
* Copyright (c) 2011 Kieran Kunhya <kieran@kunhya.com>
*
* This file is part of Libav.
*
* Libav 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.
*
* Libav 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 Libav; if not, write to the Free Software
* 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" |
c1fcd7e1 |
#include "rawenc.h" |
0ca36b4d |
|
d1a58afb |
#define MAX_EXTRADATA_SIZE 1024
|
0ca36b4d |
typedef struct { |
32dfd8ca |
AVClass *av_class; |
0ca36b4d |
int off;
int channel_conf;
int object_type;
int counter;
int mod; |
b44a5ec7 |
uint8_t buffer[0x1fff + MAX_EXTRADATA_SIZE + 1024]; |
0ca36b4d |
} LATMContext;
static const AVOption options[] = {
{"smc-interval", "StreamMuxConfig interval.", |
e6153f17 |
offsetof(LATMContext, mod), AV_OPT_TYPE_INT, {.i64 = 0x0014}, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM}, |
0ca36b4d |
{NULL},
};
static const AVClass latm_muxer_class = {
.class_name = "LATM/LOAS muxer",
.item_name = av_default_item_name,
.option = options,
.version = LIBAVUTIL_VERSION_INT,
};
static int latm_decode_extradata(LATMContext *ctx, uint8_t *buf, int size)
{
MPEG4AudioConfig m4ac;
|
d1a58afb |
if (size > MAX_EXTRADATA_SIZE) {
av_log(ctx, AV_LOG_ERROR, "Extradata is larger than currently supported.\n");
return AVERROR_INVALIDDATA;
} |
fd095539 |
ctx->off = avpriv_mpeg4audio_get_config(&m4ac, buf, size * 8, 1); |
0ca36b4d |
if (ctx->off < 0)
return ctx->off;
|
a736eb4a |
if (ctx->object_type == AOT_ALS && (ctx->off & 7)) {
// as long as avpriv_mpeg4audio_get_config works correctly this is impossible
av_log(ctx, AV_LOG_ERROR, "BUG: ALS offset is not byte-aligned\n");
return AVERROR_INVALIDDATA;
} |
0ca36b4d |
/* FIXME: are any formats not allowed in LATM? */
if (m4ac.object_type > AOT_SBR && m4ac.object_type != AOT_ALS) {
av_log(ctx, AV_LOG_ERROR, "Muxing MPEG-4 AOT %d in LATM is not supported\n", m4ac.object_type);
return AVERROR_INVALIDDATA;
}
ctx->channel_conf = m4ac.chan_config;
ctx->object_type = m4ac.object_type;
return 0;
}
static int latm_write_header(AVFormatContext *s)
{
LATMContext *ctx = s->priv_data;
AVCodecContext *avctx = s->streams[0]->codec;
|
7a72695c |
if (avctx->codec_id == AV_CODEC_ID_AAC_LATM) |
c1fcd7e1 |
return 0;
|
0ca36b4d |
if (avctx->extradata_size > 0 &&
latm_decode_extradata(ctx, avctx->extradata, avctx->extradata_size) < 0)
return AVERROR_INVALIDDATA;
return 0;
}
|
fa8a6385 |
static void latm_write_frame_header(AVFormatContext *s, PutBitContext *bs) |
0ca36b4d |
{
LATMContext *ctx = s->priv_data;
AVCodecContext *avctx = s->streams[0]->codec;
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) { |
a736eb4a |
header_size = avctx->extradata_size-(ctx->off >> 3);
avpriv_copy_bits(bs, &avctx->extradata[ctx->off >> 3], header_size); |
0ca36b4d |
} else { |
eb24def4 |
// + 3 assumes not scalable and dependsOnCoreCoder == 0,
// see decode_ga_specific_config in libavcodec/aacdec.c |
9f51c682 |
avpriv_copy_bits(bs, avctx->extradata, ctx->off + 3); |
0ca36b4d |
if (!ctx->channel_conf) { |
0c1e9c2d |
GetBitContext gb;
init_get_bits(&gb, avctx->extradata, avctx->extradata_size * 8);
skip_bits_long(&gb, ctx->off + 3); |
59a9a235 |
avpriv_copy_pce_data(bs, &gb); |
0ca36b4d |
}
}
put_bits(bs, 3, 0); /* frameLengthType */ |
d4a544cb |
put_bits(bs, 8, 0xff); /* latmBufferFullness */ |
0ca36b4d |
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)
{ |
b44a5ec7 |
LATMContext *ctx = s->priv_data; |
0ca36b4d |
AVIOContext *pb = s->pb;
PutBitContext bs;
int i, len;
uint8_t loas_header[] = "\x56\xe0\x00";
|
7a72695c |
if (s->streams[0]->codec->codec_id == AV_CODEC_ID_AAC_LATM) |
c1fcd7e1 |
return ff_raw_write_packet(s, pkt);
|
0ca36b4d |
if (pkt->size > 2 && pkt->data[0] == 0xff && (pkt->data[1] >> 4) == 0xf) {
av_log(s, AV_LOG_ERROR, "ADTS header detected - ADTS will not be incorrectly muxed into LATM\n");
return AVERROR_INVALIDDATA;
} |
8d3bc52a |
if (!s->streams[0]->codec->extradata) {
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);
else
return AVERROR_INVALIDDATA;
}
|
8e357e8e |
if (pkt->size > 0x1fff)
goto too_large; |
0ca36b4d |
|
b44a5ec7 |
init_put_bits(&bs, ctx->buffer, pkt->size+1024+MAX_EXTRADATA_SIZE); |
0ca36b4d |
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 */
|
9540476b |
/* PayloadMux() */ |
912a838e |
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); |
9540476b |
avpriv_copy_bits(&bs, pkt->data + 1, 8*pkt->size - 8);
} else
avpriv_copy_bits(&bs, pkt->data, 8*pkt->size); |
0ca36b4d |
|
9f51c682 |
avpriv_align_put_bits(&bs); |
0ca36b4d |
flush_put_bits(&bs);
len = put_bits_count(&bs) >> 3;
|
8e357e8e |
if (len > 0x1fff)
goto too_large;
|
0ca36b4d |
loas_header[1] |= (len >> 8) & 0x1f;
loas_header[2] |= len & 0xff;
avio_write(pb, loas_header, 3); |
b44a5ec7 |
avio_write(pb, ctx->buffer, len); |
0ca36b4d |
return 0; |
8e357e8e |
too_large:
av_log(s, AV_LOG_ERROR, "LATM packet size larger than maximum size 0x1fff\n");
return AVERROR_INVALIDDATA; |
0ca36b4d |
}
AVOutputFormat ff_latm_muxer = {
.name = "latm",
.long_name = NULL_IF_CONFIG_SMALL("LOAS/LATM"),
.mime_type = "audio/MP4A-LATM", |
f6e99bf5 |
.extensions = "latm,loas", |
0ca36b4d |
.priv_data_size = sizeof(LATMContext), |
36ef5369 |
.audio_codec = AV_CODEC_ID_AAC,
.video_codec = AV_CODEC_ID_NONE, |
0ca36b4d |
.write_header = latm_write_header,
.write_packet = latm_write_packet,
.priv_class = &latm_muxer_class,
}; |