annotate ffmpeg/libavformat/latmenc.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents f445c3017523
children
rev   line source
yading@11 1 /*
yading@11 2 * LATM/LOAS muxer
yading@11 3 * Copyright (c) 2011 Kieran Kunhya <kieran@kunhya.com>
yading@11 4 *
yading@11 5 * This file is part of Libav.
yading@11 6 *
yading@11 7 * Libav is free software; you can redistribute it and/or
yading@11 8 * modify it under the terms of the GNU Lesser General Public
yading@11 9 * License as published by the Free Software Foundation; either
yading@11 10 * version 2.1 of the License, or (at your option) any later version.
yading@11 11 *
yading@11 12 * Libav is distributed in the hope that it will be useful,
yading@11 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@11 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@11 15 * Lesser General Public License for more details.
yading@11 16 *
yading@11 17 * You should have received a copy of the GNU Lesser General Public
yading@11 18 * License along with Libav; if not, write to the Free Software
yading@11 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@11 20 */
yading@11 21
yading@11 22 #include "libavcodec/get_bits.h"
yading@11 23 #include "libavcodec/put_bits.h"
yading@11 24 #include "libavcodec/avcodec.h"
yading@11 25 #include "libavcodec/mpeg4audio.h"
yading@11 26 #include "libavutil/opt.h"
yading@11 27 #include "avformat.h"
yading@11 28 #include "rawenc.h"
yading@11 29
yading@11 30 #define MAX_EXTRADATA_SIZE 1024
yading@11 31
yading@11 32 typedef struct {
yading@11 33 AVClass *av_class;
yading@11 34 int off;
yading@11 35 int channel_conf;
yading@11 36 int object_type;
yading@11 37 int counter;
yading@11 38 int mod;
yading@11 39 uint8_t buffer[0x1fff + MAX_EXTRADATA_SIZE + 1024];
yading@11 40 } LATMContext;
yading@11 41
yading@11 42 static const AVOption options[] = {
yading@11 43 {"smc-interval", "StreamMuxConfig interval.",
yading@11 44 offsetof(LATMContext, mod), AV_OPT_TYPE_INT, {.i64 = 0x0014}, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM},
yading@11 45 {NULL},
yading@11 46 };
yading@11 47
yading@11 48 static const AVClass latm_muxer_class = {
yading@11 49 .class_name = "LATM/LOAS muxer",
yading@11 50 .item_name = av_default_item_name,
yading@11 51 .option = options,
yading@11 52 .version = LIBAVUTIL_VERSION_INT,
yading@11 53 };
yading@11 54
yading@11 55 static int latm_decode_extradata(LATMContext *ctx, uint8_t *buf, int size)
yading@11 56 {
yading@11 57 MPEG4AudioConfig m4ac;
yading@11 58
yading@11 59 if (size > MAX_EXTRADATA_SIZE) {
yading@11 60 av_log(ctx, AV_LOG_ERROR, "Extradata is larger than currently supported.\n");
yading@11 61 return AVERROR_INVALIDDATA;
yading@11 62 }
yading@11 63 ctx->off = avpriv_mpeg4audio_get_config(&m4ac, buf, size * 8, 1);
yading@11 64 if (ctx->off < 0)
yading@11 65 return ctx->off;
yading@11 66
yading@11 67 if (ctx->object_type == AOT_ALS && (ctx->off & 7)) {
yading@11 68 // as long as avpriv_mpeg4audio_get_config works correctly this is impossible
yading@11 69 av_log(ctx, AV_LOG_ERROR, "BUG: ALS offset is not byte-aligned\n");
yading@11 70 return AVERROR_INVALIDDATA;
yading@11 71 }
yading@11 72 /* FIXME: are any formats not allowed in LATM? */
yading@11 73
yading@11 74 if (m4ac.object_type > AOT_SBR && m4ac.object_type != AOT_ALS) {
yading@11 75 av_log(ctx, AV_LOG_ERROR, "Muxing MPEG-4 AOT %d in LATM is not supported\n", m4ac.object_type);
yading@11 76 return AVERROR_INVALIDDATA;
yading@11 77 }
yading@11 78 ctx->channel_conf = m4ac.chan_config;
yading@11 79 ctx->object_type = m4ac.object_type;
yading@11 80
yading@11 81 return 0;
yading@11 82 }
yading@11 83
yading@11 84 static int latm_write_header(AVFormatContext *s)
yading@11 85 {
yading@11 86 LATMContext *ctx = s->priv_data;
yading@11 87 AVCodecContext *avctx = s->streams[0]->codec;
yading@11 88
yading@11 89 if (avctx->codec_id == AV_CODEC_ID_AAC_LATM)
yading@11 90 return 0;
yading@11 91
yading@11 92 if (avctx->extradata_size > 0 &&
yading@11 93 latm_decode_extradata(ctx, avctx->extradata, avctx->extradata_size) < 0)
yading@11 94 return AVERROR_INVALIDDATA;
yading@11 95
yading@11 96 return 0;
yading@11 97 }
yading@11 98
yading@11 99 static void latm_write_frame_header(AVFormatContext *s, PutBitContext *bs)
yading@11 100 {
yading@11 101 LATMContext *ctx = s->priv_data;
yading@11 102 AVCodecContext *avctx = s->streams[0]->codec;
yading@11 103 int header_size;
yading@11 104
yading@11 105 /* AudioMuxElement */
yading@11 106 put_bits(bs, 1, !!ctx->counter);
yading@11 107
yading@11 108 if (!ctx->counter) {
yading@11 109 /* StreamMuxConfig */
yading@11 110 put_bits(bs, 1, 0); /* audioMuxVersion */
yading@11 111 put_bits(bs, 1, 1); /* allStreamsSameTimeFraming */
yading@11 112 put_bits(bs, 6, 0); /* numSubFrames */
yading@11 113 put_bits(bs, 4, 0); /* numProgram */
yading@11 114 put_bits(bs, 3, 0); /* numLayer */
yading@11 115
yading@11 116 /* AudioSpecificConfig */
yading@11 117 if (ctx->object_type == AOT_ALS) {
yading@11 118 header_size = avctx->extradata_size-(ctx->off >> 3);
yading@11 119 avpriv_copy_bits(bs, &avctx->extradata[ctx->off >> 3], header_size);
yading@11 120 } else {
yading@11 121 // + 3 assumes not scalable and dependsOnCoreCoder == 0,
yading@11 122 // see decode_ga_specific_config in libavcodec/aacdec.c
yading@11 123 avpriv_copy_bits(bs, avctx->extradata, ctx->off + 3);
yading@11 124
yading@11 125 if (!ctx->channel_conf) {
yading@11 126 GetBitContext gb;
yading@11 127 init_get_bits(&gb, avctx->extradata, avctx->extradata_size * 8);
yading@11 128 skip_bits_long(&gb, ctx->off + 3);
yading@11 129 avpriv_copy_pce_data(bs, &gb);
yading@11 130 }
yading@11 131 }
yading@11 132
yading@11 133 put_bits(bs, 3, 0); /* frameLengthType */
yading@11 134 put_bits(bs, 8, 0xff); /* latmBufferFullness */
yading@11 135
yading@11 136 put_bits(bs, 1, 0); /* otherDataPresent */
yading@11 137 put_bits(bs, 1, 0); /* crcCheckPresent */
yading@11 138 }
yading@11 139
yading@11 140 ctx->counter++;
yading@11 141 ctx->counter %= ctx->mod;
yading@11 142 }
yading@11 143
yading@11 144 static int latm_write_packet(AVFormatContext *s, AVPacket *pkt)
yading@11 145 {
yading@11 146 LATMContext *ctx = s->priv_data;
yading@11 147 AVIOContext *pb = s->pb;
yading@11 148 PutBitContext bs;
yading@11 149 int i, len;
yading@11 150 uint8_t loas_header[] = "\x56\xe0\x00";
yading@11 151
yading@11 152 if (s->streams[0]->codec->codec_id == AV_CODEC_ID_AAC_LATM)
yading@11 153 return ff_raw_write_packet(s, pkt);
yading@11 154
yading@11 155 if (pkt->size > 2 && pkt->data[0] == 0xff && (pkt->data[1] >> 4) == 0xf) {
yading@11 156 av_log(s, AV_LOG_ERROR, "ADTS header detected - ADTS will not be incorrectly muxed into LATM\n");
yading@11 157 return AVERROR_INVALIDDATA;
yading@11 158 }
yading@11 159
yading@11 160 if (!s->streams[0]->codec->extradata) {
yading@11 161 if(pkt->size > 2 && pkt->data[0] == 0x56 && (pkt->data[1] >> 4) == 0xe &&
yading@11 162 (AV_RB16(pkt->data + 1) & 0x1FFF) + 3 == pkt->size)
yading@11 163 return ff_raw_write_packet(s, pkt);
yading@11 164 else
yading@11 165 return AVERROR_INVALIDDATA;
yading@11 166 }
yading@11 167
yading@11 168 if (pkt->size > 0x1fff)
yading@11 169 goto too_large;
yading@11 170
yading@11 171 init_put_bits(&bs, ctx->buffer, pkt->size+1024+MAX_EXTRADATA_SIZE);
yading@11 172
yading@11 173 latm_write_frame_header(s, &bs);
yading@11 174
yading@11 175 /* PayloadLengthInfo() */
yading@11 176 for (i = 0; i <= pkt->size-255; i+=255)
yading@11 177 put_bits(&bs, 8, 255);
yading@11 178
yading@11 179 put_bits(&bs, 8, pkt->size-i);
yading@11 180
yading@11 181 /* The LATM payload is written unaligned */
yading@11 182
yading@11 183 /* PayloadMux() */
yading@11 184 if (pkt->size && (pkt->data[0] & 0xe1) == 0x81) {
yading@11 185 // Convert byte-aligned DSE to non-aligned.
yading@11 186 // Due to the input format encoding we know that
yading@11 187 // it is naturally byte-aligned in the input stream,
yading@11 188 // so there are no padding bits to account for.
yading@11 189 // To avoid having to add padding bits and rearrange
yading@11 190 // the whole stream we just remove the byte-align flag.
yading@11 191 // This allows us to remux our FATE AAC samples into latm
yading@11 192 // files that are still playable with minimal effort.
yading@11 193 put_bits(&bs, 8, pkt->data[0] & 0xfe);
yading@11 194 avpriv_copy_bits(&bs, pkt->data + 1, 8*pkt->size - 8);
yading@11 195 } else
yading@11 196 avpriv_copy_bits(&bs, pkt->data, 8*pkt->size);
yading@11 197
yading@11 198 avpriv_align_put_bits(&bs);
yading@11 199 flush_put_bits(&bs);
yading@11 200
yading@11 201 len = put_bits_count(&bs) >> 3;
yading@11 202
yading@11 203 if (len > 0x1fff)
yading@11 204 goto too_large;
yading@11 205
yading@11 206 loas_header[1] |= (len >> 8) & 0x1f;
yading@11 207 loas_header[2] |= len & 0xff;
yading@11 208
yading@11 209 avio_write(pb, loas_header, 3);
yading@11 210 avio_write(pb, ctx->buffer, len);
yading@11 211
yading@11 212 return 0;
yading@11 213
yading@11 214 too_large:
yading@11 215 av_log(s, AV_LOG_ERROR, "LATM packet size larger than maximum size 0x1fff\n");
yading@11 216 return AVERROR_INVALIDDATA;
yading@11 217 }
yading@11 218
yading@11 219 AVOutputFormat ff_latm_muxer = {
yading@11 220 .name = "latm",
yading@11 221 .long_name = NULL_IF_CONFIG_SMALL("LOAS/LATM"),
yading@11 222 .mime_type = "audio/MP4A-LATM",
yading@11 223 .extensions = "latm,loas",
yading@11 224 .priv_data_size = sizeof(LATMContext),
yading@11 225 .audio_codec = AV_CODEC_ID_AAC,
yading@11 226 .video_codec = AV_CODEC_ID_NONE,
yading@11 227 .write_header = latm_write_header,
yading@11 228 .write_packet = latm_write_packet,
yading@11 229 .priv_class = &latm_muxer_class,
yading@11 230 };