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