00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavcodec/get_bits.h"
00023 #include "libavcodec/put_bits.h"
00024 #include "libavcodec/avcodec.h"
00025 #include "libavcodec/mpeg4audio.h"
00026 #include "libavutil/opt.h"
00027 #include "avformat.h"
00028 #include "rawenc.h"
00029
00030 typedef struct {
00031 AVClass *av_class;
00032 int off;
00033 int channel_conf;
00034 int object_type;
00035 int counter;
00036 int mod;
00037 } LATMContext;
00038
00039 static const AVOption options[] = {
00040 {"smc-interval", "StreamMuxConfig interval.",
00041 offsetof(LATMContext, mod), AV_OPT_TYPE_INT, {.dbl = 0x0014}, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM},
00042 {NULL},
00043 };
00044
00045 static const AVClass latm_muxer_class = {
00046 .class_name = "LATM/LOAS muxer",
00047 .item_name = av_default_item_name,
00048 .option = options,
00049 .version = LIBAVUTIL_VERSION_INT,
00050 };
00051
00052 static int latm_decode_extradata(LATMContext *ctx, uint8_t *buf, int size)
00053 {
00054 GetBitContext gb;
00055 MPEG4AudioConfig m4ac;
00056
00057 init_get_bits(&gb, buf, size * 8);
00058 ctx->off = avpriv_mpeg4audio_get_config(&m4ac, buf, size * 8, 1);
00059 if (ctx->off < 0)
00060 return ctx->off;
00061 skip_bits_long(&gb, ctx->off);
00062
00063
00064
00065 if (m4ac.object_type > AOT_SBR && m4ac.object_type != AOT_ALS) {
00066 av_log(ctx, AV_LOG_ERROR, "Muxing MPEG-4 AOT %d in LATM is not supported\n", m4ac.object_type);
00067 return AVERROR_INVALIDDATA;
00068 }
00069 ctx->channel_conf = m4ac.chan_config;
00070 ctx->object_type = m4ac.object_type;
00071
00072 return 0;
00073 }
00074
00075 static int latm_write_header(AVFormatContext *s)
00076 {
00077 LATMContext *ctx = s->priv_data;
00078 AVCodecContext *avctx = s->streams[0]->codec;
00079
00080 if (avctx->codec_id == CODEC_ID_AAC_LATM)
00081 return 0;
00082
00083 if (avctx->extradata_size > 0 &&
00084 latm_decode_extradata(ctx, avctx->extradata, avctx->extradata_size) < 0)
00085 return AVERROR_INVALIDDATA;
00086
00087 return 0;
00088 }
00089
00090 static int latm_write_frame_header(AVFormatContext *s, PutBitContext *bs)
00091 {
00092 LATMContext *ctx = s->priv_data;
00093 AVCodecContext *avctx = s->streams[0]->codec;
00094 GetBitContext gb;
00095 int header_size;
00096
00097
00098 put_bits(bs, 1, !!ctx->counter);
00099
00100 if (!ctx->counter) {
00101 init_get_bits(&gb, avctx->extradata, avctx->extradata_size * 8);
00102
00103
00104 put_bits(bs, 1, 0);
00105 put_bits(bs, 1, 1);
00106 put_bits(bs, 6, 0);
00107 put_bits(bs, 4, 0);
00108 put_bits(bs, 3, 0);
00109
00110
00111 if (ctx->object_type == AOT_ALS) {
00112 header_size = avctx->extradata_size-(ctx->off + 7) >> 3;
00113 avpriv_copy_bits(bs, &avctx->extradata[ctx->off], header_size);
00114 } else {
00115 avpriv_copy_bits(bs, avctx->extradata, ctx->off + 3);
00116
00117 if (!ctx->channel_conf) {
00118 avpriv_copy_pce_data(bs, &gb);
00119 }
00120 }
00121
00122 put_bits(bs, 3, 0);
00123 put_bits(bs, 8, 0xff);
00124
00125 put_bits(bs, 1, 0);
00126 put_bits(bs, 1, 0);
00127 }
00128
00129 ctx->counter++;
00130 ctx->counter %= ctx->mod;
00131
00132 return 0;
00133 }
00134
00135 static int latm_write_packet(AVFormatContext *s, AVPacket *pkt)
00136 {
00137 AVIOContext *pb = s->pb;
00138 PutBitContext bs;
00139 int i, len;
00140 uint8_t loas_header[] = "\x56\xe0\x00";
00141 uint8_t *buf;
00142
00143 if (s->streams[0]->codec->codec_id == CODEC_ID_AAC_LATM)
00144 return ff_raw_write_packet(s, pkt);
00145
00146 if (pkt->size > 2 && pkt->data[0] == 0xff && (pkt->data[1] >> 4) == 0xf) {
00147 av_log(s, AV_LOG_ERROR, "ADTS header detected - ADTS will not be incorrectly muxed into LATM\n");
00148 return AVERROR_INVALIDDATA;
00149 }
00150
00151 buf = av_malloc(pkt->size+1024);
00152 if (!buf)
00153 return AVERROR(ENOMEM);
00154
00155 init_put_bits(&bs, buf, pkt->size+1024);
00156
00157 latm_write_frame_header(s, &bs);
00158
00159
00160 for (i = 0; i <= pkt->size-255; i+=255)
00161 put_bits(&bs, 8, 255);
00162
00163 put_bits(&bs, 8, pkt->size-i);
00164
00165
00166
00167
00168 for (i = 0; i < pkt->size; i++)
00169 put_bits(&bs, 8, pkt->data[i]);
00170
00171 avpriv_align_put_bits(&bs);
00172 flush_put_bits(&bs);
00173
00174 len = put_bits_count(&bs) >> 3;
00175
00176 loas_header[1] |= (len >> 8) & 0x1f;
00177 loas_header[2] |= len & 0xff;
00178
00179 avio_write(pb, loas_header, 3);
00180 avio_write(pb, buf, len);
00181
00182 av_free(buf);
00183
00184 return 0;
00185 }
00186
00187 AVOutputFormat ff_latm_muxer = {
00188 .name = "latm",
00189 .long_name = NULL_IF_CONFIG_SMALL("LOAS/LATM"),
00190 .mime_type = "audio/MP4A-LATM",
00191 .extensions = "latm,loas",
00192 .priv_data_size = sizeof(LATMContext),
00193 .audio_codec = CODEC_ID_AAC,
00194 .video_codec = CODEC_ID_NONE,
00195 .write_header = latm_write_header,
00196 .write_packet = latm_write_packet,
00197 .priv_class = &latm_muxer_class,
00198 };