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