2011-09-06 22:05:27 +03:00
|
|
|
/*
|
|
|
|
* LATM/LOAS muxer
|
|
|
|
* Copyright (c) 2011 Kieran Kunhya <kieran@kunhya.com>
|
|
|
|
*
|
2013-08-16 00:12:51 +03:00
|
|
|
* This file is part of FFmpeg.
|
2011-09-06 22:05:27 +03:00
|
|
|
*
|
2013-08-16 00:12:51 +03:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2011-09-06 22:05:27 +03:00
|
|
|
* 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.
|
|
|
|
*
|
2013-08-16 00:12:51 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2011-09-06 22:05:27 +03:00
|
|
|
* 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
|
2013-08-16 00:12:51 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2011-09-06 22:05:27 +03:00
|
|
|
* 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"
|
2016-03-26 06:42:35 +02:00
|
|
|
#include "internal.h"
|
2011-09-08 09:22:50 +03:00
|
|
|
#include "rawenc.h"
|
2011-09-06 22:05:27 +03:00
|
|
|
|
2012-04-10 22:49:46 +03:00
|
|
|
#define MAX_EXTRADATA_SIZE 1024
|
|
|
|
|
2014-09-22 10:19:33 +03:00
|
|
|
typedef struct LATMContext {
|
2011-09-28 15:46:10 +03:00
|
|
|
AVClass *av_class;
|
2011-09-06 22:05:27 +03:00
|
|
|
int off;
|
|
|
|
int channel_conf;
|
|
|
|
int object_type;
|
|
|
|
int counter;
|
|
|
|
int mod;
|
2012-04-12 01:23:28 +03:00
|
|
|
uint8_t buffer[0x1fff + MAX_EXTRADATA_SIZE + 1024];
|
2011-09-06 22:05:27 +03:00
|
|
|
} LATMContext;
|
|
|
|
|
|
|
|
static const AVOption options[] = {
|
|
|
|
{"smc-interval", "StreamMuxConfig interval.",
|
2012-08-31 13:22:31 +03:00
|
|
|
offsetof(LATMContext, mod), AV_OPT_TYPE_INT, {.i64 = 0x0014}, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM},
|
2011-09-06 22:05:27 +03:00
|
|
|
{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;
|
|
|
|
|
2012-04-10 22:49:46 +03:00
|
|
|
if (size > MAX_EXTRADATA_SIZE) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Extradata is larger than currently supported.\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2011-10-21 18:25:30 +03:00
|
|
|
ctx->off = avpriv_mpeg4audio_get_config(&m4ac, buf, size * 8, 1);
|
2011-09-06 22:05:27 +03:00
|
|
|
if (ctx->off < 0)
|
|
|
|
return ctx->off;
|
|
|
|
|
2012-04-10 21:59:05 +03:00
|
|
|
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;
|
|
|
|
}
|
2011-09-06 22:05:27 +03:00
|
|
|
/* 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;
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
AVCodecParameters *par = s->streams[0]->codecpar;
|
2011-09-06 22:05:27 +03:00
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
if (par->codec_id == AV_CODEC_ID_AAC_LATM)
|
2011-09-08 09:22:50 +03:00
|
|
|
return 0;
|
2017-11-01 03:15:10 +02:00
|
|
|
if (par->codec_id != AV_CODEC_ID_AAC && par->codec_id != AV_CODEC_ID_MP4ALS) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Only AAC, LATM and ALS are supported\n");
|
2019-03-27 15:54:05 +02:00
|
|
|
return AVERROR(EINVAL);
|
2017-11-01 03:15:10 +02:00
|
|
|
}
|
2011-09-08 09:22:50 +03:00
|
|
|
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
if (par->extradata_size > 0 &&
|
|
|
|
latm_decode_extradata(ctx, par->extradata, par->extradata_size) < 0)
|
2011-09-06 22:05:27 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-10 22:46:56 +03:00
|
|
|
static void latm_write_frame_header(AVFormatContext *s, PutBitContext *bs)
|
2011-09-06 22:05:27 +03:00
|
|
|
{
|
|
|
|
LATMContext *ctx = s->priv_data;
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
AVCodecParameters *par = s->streams[0]->codecpar;
|
2011-09-06 22:05:27 +03:00
|
|
|
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) {
|
2016-04-10 21:58:15 +02:00
|
|
|
header_size = par->extradata_size-(ctx->off >> 3);
|
|
|
|
avpriv_copy_bits(bs, &par->extradata[ctx->off >> 3], header_size);
|
2011-09-06 22:05:27 +03:00
|
|
|
} else {
|
2012-04-09 19:31:50 +03:00
|
|
|
// + 3 assumes not scalable and dependsOnCoreCoder == 0,
|
|
|
|
// see decode_ga_specific_config in libavcodec/aacdec.c
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
avpriv_copy_bits(bs, par->extradata, ctx->off + 3);
|
2011-09-06 22:05:27 +03:00
|
|
|
|
|
|
|
if (!ctx->channel_conf) {
|
2012-04-09 01:57:11 +03:00
|
|
|
GetBitContext gb;
|
2016-04-10 21:58:15 +02:00
|
|
|
int ret = init_get_bits8(&gb, par->extradata, par->extradata_size);
|
2015-09-03 10:37:13 +02:00
|
|
|
av_assert0(ret >= 0); // extradata size has been checked already, so this should not fail
|
2012-04-09 01:57:11 +03:00
|
|
|
skip_bits_long(&gb, ctx->off + 3);
|
2017-03-16 18:34:12 +02:00
|
|
|
ff_copy_pce_data(bs, &gb);
|
2011-09-06 22:05:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
put_bits(bs, 3, 0); /* frameLengthType */
|
2011-09-23 18:51:07 +03:00
|
|
|
put_bits(bs, 8, 0xff); /* latmBufferFullness */
|
2011-09-06 22:05:27 +03:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2012-04-12 01:23:28 +03:00
|
|
|
LATMContext *ctx = s->priv_data;
|
2017-04-13 21:06:02 +02:00
|
|
|
AVCodecParameters *par = s->streams[0]->codecpar;
|
2011-09-06 22:05:27 +03:00
|
|
|
AVIOContext *pb = s->pb;
|
|
|
|
PutBitContext bs;
|
|
|
|
int i, len;
|
|
|
|
uint8_t loas_header[] = "\x56\xe0\x00";
|
|
|
|
|
2017-04-13 21:06:02 +02:00
|
|
|
if (par->codec_id == AV_CODEC_ID_AAC_LATM)
|
2011-09-08 09:22:50 +03:00
|
|
|
return ff_raw_write_packet(s, pkt);
|
|
|
|
|
2017-04-13 21:06:02 +02:00
|
|
|
if (!par->extradata) {
|
2013-01-17 00:27:14 +03:00
|
|
|
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);
|
2017-04-13 21:06:02 +02:00
|
|
|
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) {
|
|
|
|
if (latm_decode_extradata(ctx, side_data, side_data_size) < 0)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
ret = ff_alloc_extradata(par, side_data_size);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
memcpy(par->extradata, side_data, side_data_size);
|
|
|
|
}
|
|
|
|
}
|
2013-01-17 00:27:14 +03:00
|
|
|
}
|
|
|
|
|
2012-04-10 22:11:50 +03:00
|
|
|
if (pkt->size > 0x1fff)
|
|
|
|
goto too_large;
|
2011-09-06 22:05:27 +03:00
|
|
|
|
2012-04-12 01:23:28 +03:00
|
|
|
init_put_bits(&bs, ctx->buffer, pkt->size+1024+MAX_EXTRADATA_SIZE);
|
2011-09-06 22:05:27 +03:00
|
|
|
|
|
|
|
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 */
|
|
|
|
|
2012-04-10 22:42:17 +03:00
|
|
|
/* PayloadMux() */
|
2012-04-09 01:10:07 +03:00
|
|
|
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);
|
2012-04-10 22:42:17 +03:00
|
|
|
avpriv_copy_bits(&bs, pkt->data + 1, 8*pkt->size - 8);
|
|
|
|
} else
|
|
|
|
avpriv_copy_bits(&bs, pkt->data, 8*pkt->size);
|
2011-09-06 22:05:27 +03:00
|
|
|
|
2011-10-17 11:10:42 +03:00
|
|
|
avpriv_align_put_bits(&bs);
|
2011-09-06 22:05:27 +03:00
|
|
|
flush_put_bits(&bs);
|
|
|
|
|
|
|
|
len = put_bits_count(&bs) >> 3;
|
|
|
|
|
2012-04-10 22:11:50 +03:00
|
|
|
if (len > 0x1fff)
|
|
|
|
goto too_large;
|
|
|
|
|
2011-09-06 22:05:27 +03:00
|
|
|
loas_header[1] |= (len >> 8) & 0x1f;
|
|
|
|
loas_header[2] |= len & 0xff;
|
|
|
|
|
|
|
|
avio_write(pb, loas_header, 3);
|
2012-04-12 01:23:28 +03:00
|
|
|
avio_write(pb, ctx->buffer, len);
|
2011-09-06 22:05:27 +03:00
|
|
|
|
|
|
|
return 0;
|
2012-04-10 22:11:50 +03:00
|
|
|
|
|
|
|
too_large:
|
|
|
|
av_log(s, AV_LOG_ERROR, "LATM packet size larger than maximum size 0x1fff\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
2011-09-06 22:05:27 +03:00
|
|
|
}
|
|
|
|
|
2016-03-26 06:42:35 +02:00
|
|
|
static int latm_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
|
|
|
|
{
|
|
|
|
int ret = 1;
|
|
|
|
AVStream *st = s->streams[pkt->stream_index];
|
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
if (st->codecpar->codec_id == AV_CODEC_ID_AAC) {
|
2016-03-26 06:42:35 +02:00
|
|
|
if (pkt->size > 2 && (AV_RB16(pkt->data) & 0xfff0) == 0xfff0)
|
|
|
|
ret = ff_stream_add_bitstream_filter(st, "aac_adtstoasc", NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-09-06 22:05:27 +03:00
|
|
|
AVOutputFormat ff_latm_muxer = {
|
|
|
|
.name = "latm",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("LOAS/LATM"),
|
|
|
|
.mime_type = "audio/MP4A-LATM",
|
2011-09-08 09:24:17 +03:00
|
|
|
.extensions = "latm,loas",
|
2011-09-06 22:05:27 +03:00
|
|
|
.priv_data_size = sizeof(LATMContext),
|
2012-08-05 12:11:04 +03:00
|
|
|
.audio_codec = AV_CODEC_ID_AAC,
|
|
|
|
.video_codec = AV_CODEC_ID_NONE,
|
2011-09-06 22:05:27 +03:00
|
|
|
.write_header = latm_write_header,
|
|
|
|
.write_packet = latm_write_packet,
|
|
|
|
.priv_class = &latm_muxer_class,
|
2016-03-26 06:42:35 +02:00
|
|
|
.check_bitstream= latm_check_bitstream,
|
2014-05-29 08:06:52 +03:00
|
|
|
.flags = AVFMT_NOTIMESTAMPS,
|
2011-09-06 22:05:27 +03:00
|
|
|
};
|