FFmpeg  4.4
latmenc.c
Go to the documentation of this file.
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/avcodec.h"
25 #include "libavcodec/mpeg4audio.h"
26 #include "libavutil/opt.h"
27 #include "avformat.h"
28 #include "internal.h"
29 #include "rawenc.h"
30 
31 #define MAX_EXTRADATA_SIZE 1024
32 
33 typedef struct LATMContext {
35  int off;
38  int counter;
39  int mod;
40  uint8_t buffer[0x1fff + MAX_EXTRADATA_SIZE + 1024];
41 } LATMContext;
42 
43 static const AVOption options[] = {
44  {"smc-interval", "StreamMuxConfig interval.",
45  offsetof(LATMContext, mod), AV_OPT_TYPE_INT, {.i64 = 0x0014}, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM},
46  {NULL},
47 };
48 
49 static const AVClass latm_muxer_class = {
50  .class_name = "LATM/LOAS muxer",
51  .item_name = av_default_item_name,
52  .option = options,
53  .version = LIBAVUTIL_VERSION_INT,
54 };
55 
57 {
58  LATMContext *ctx = s->priv_data;
59  MPEG4AudioConfig m4ac;
60 
61  if (size > MAX_EXTRADATA_SIZE) {
62  av_log(s, AV_LOG_ERROR, "Extradata is larger than currently supported.\n");
63  return AVERROR_INVALIDDATA;
64  }
65  ctx->off = avpriv_mpeg4audio_get_config2(&m4ac, buf, size, 1, s);
66  if (ctx->off < 0)
67  return ctx->off;
68 
69  if (ctx->object_type == AOT_ALS && (ctx->off & 7)) {
70  // as long as avpriv_mpeg4audio_get_config works correctly this is impossible
71  av_log(s, AV_LOG_ERROR, "BUG: ALS offset is not byte-aligned\n");
72  return AVERROR_INVALIDDATA;
73  }
74  /* FIXME: are any formats not allowed in LATM? */
75 
76  if (m4ac.object_type > AOT_SBR && m4ac.object_type != AOT_ALS) {
77  av_log(s, AV_LOG_ERROR, "Muxing MPEG-4 AOT %d in LATM is not supported\n", m4ac.object_type);
78  return AVERROR_INVALIDDATA;
79  }
80  ctx->channel_conf = m4ac.chan_config;
81  ctx->object_type = m4ac.object_type;
82 
83  return 0;
84 }
85 
87 {
88  AVCodecParameters *par = s->streams[0]->codecpar;
89 
90  if (par->codec_id == AV_CODEC_ID_AAC_LATM)
91  return 0;
92  if (par->codec_id != AV_CODEC_ID_AAC && par->codec_id != AV_CODEC_ID_MP4ALS) {
93  av_log(s, AV_LOG_ERROR, "Only AAC, LATM and ALS are supported\n");
94  return AVERROR(EINVAL);
95  }
96 
97  if (par->extradata_size > 0 &&
99  return AVERROR_INVALIDDATA;
100 
101  return 0;
102 }
103 
104 static void copy_bits(PutBitContext *pb, const uint8_t *src, int length)
105 {
106  int words = length >> 4;
107  int bits = length & 15;
108  int i;
109  for (i = 0; i < words; i++)
110  put_bits(pb, 16, AV_RB16(src + 2 * i));
111  if (bits)
112  put_bits(pb, bits, AV_RB16(src + 2 * words) >> (16 - bits));
113 }
114 
116 {
117  LATMContext *ctx = s->priv_data;
118  AVCodecParameters *par = s->streams[0]->codecpar;
119  int header_size;
120 
121  /* AudioMuxElement */
122  put_bits(bs, 1, !!ctx->counter);
123 
124  if (!ctx->counter) {
125  /* StreamMuxConfig */
126  put_bits(bs, 1, 0); /* audioMuxVersion */
127  put_bits(bs, 1, 1); /* allStreamsSameTimeFraming */
128  put_bits(bs, 6, 0); /* numSubFrames */
129  put_bits(bs, 4, 0); /* numProgram */
130  put_bits(bs, 3, 0); /* numLayer */
131 
132  /* AudioSpecificConfig */
133  if (ctx->object_type == AOT_ALS) {
134  header_size = (par->extradata_size - (ctx->off >> 3)) * 8;
135  copy_bits(bs, &par->extradata[ctx->off >> 3], header_size);
136  } else {
137  // + 3 assumes not scalable and dependsOnCoreCoder == 0,
138  // see decode_ga_specific_config in libavcodec/aacdec.c
139  copy_bits(bs, par->extradata, ctx->off + 3);
140 
141  if (!ctx->channel_conf) {
142  GetBitContext gb;
143  int ret = init_get_bits8(&gb, par->extradata, par->extradata_size);
144  av_assert0(ret >= 0); // extradata size has been checked already, so this should not fail
145  skip_bits_long(&gb, ctx->off + 3);
146  ff_copy_pce_data(bs, &gb);
147  }
148  }
149 
150  put_bits(bs, 3, 0); /* frameLengthType */
151  put_bits(bs, 8, 0xff); /* latmBufferFullness */
152 
153  put_bits(bs, 1, 0); /* otherDataPresent */
154  put_bits(bs, 1, 0); /* crcCheckPresent */
155  }
156 
157  ctx->counter++;
158  ctx->counter %= ctx->mod;
159 }
160 
162 {
163  LATMContext *ctx = s->priv_data;
164  AVCodecParameters *par = s->streams[0]->codecpar;
165  AVIOContext *pb = s->pb;
166  PutBitContext bs;
167  int i, len;
168  uint8_t loas_header[] = "\x56\xe0\x00";
169 
170  if (par->codec_id == AV_CODEC_ID_AAC_LATM)
171  return ff_raw_write_packet(s, pkt);
172 
173  if (!par->extradata) {
174  if(pkt->size > 2 && pkt->data[0] == 0x56 && (pkt->data[1] >> 4) == 0xe &&
175  (AV_RB16(pkt->data + 1) & 0x1FFF) + 3 == pkt->size)
176  return ff_raw_write_packet(s, pkt);
177  else {
178  uint8_t *side_data;
179  buffer_size_t side_data_size;
180  int ret;
181 
183  &side_data_size);
184  if (side_data_size) {
185  if (latm_decode_extradata(s, side_data, side_data_size) < 0)
186  return AVERROR_INVALIDDATA;
187  ret = ff_alloc_extradata(par, side_data_size);
188  if (ret < 0)
189  return ret;
190  memcpy(par->extradata, side_data, side_data_size);
191  } else
192  return AVERROR_INVALIDDATA;
193  }
194  }
195 
196  if (pkt->size > 0x1fff)
197  goto too_large;
198 
199  init_put_bits(&bs, ctx->buffer, pkt->size+1024+MAX_EXTRADATA_SIZE);
200 
202 
203  /* PayloadLengthInfo() */
204  for (i = 0; i <= pkt->size-255; i+=255)
205  put_bits(&bs, 8, 255);
206 
207  put_bits(&bs, 8, pkt->size-i);
208 
209  /* The LATM payload is written unaligned */
210 
211  /* PayloadMux() */
212  if (pkt->size && (pkt->data[0] & 0xe1) == 0x81) {
213  // Convert byte-aligned DSE to non-aligned.
214  // Due to the input format encoding we know that
215  // it is naturally byte-aligned in the input stream,
216  // so there are no padding bits to account for.
217  // To avoid having to add padding bits and rearrange
218  // the whole stream we just remove the byte-align flag.
219  // This allows us to remux our FATE AAC samples into latm
220  // files that are still playable with minimal effort.
221  put_bits(&bs, 8, pkt->data[0] & 0xfe);
222  copy_bits(&bs, pkt->data + 1, 8*pkt->size - 8);
223  } else
224  copy_bits(&bs, pkt->data, 8*pkt->size);
225 
226  flush_put_bits(&bs);
227 
228  len = put_bits_count(&bs) >> 3;
229 
230  if (len > 0x1fff)
231  goto too_large;
232 
233  loas_header[1] |= (len >> 8) & 0x1f;
234  loas_header[2] |= len & 0xff;
235 
236  avio_write(pb, loas_header, 3);
237  avio_write(pb, ctx->buffer, len);
238 
239  return 0;
240 
241 too_large:
242  av_log(s, AV_LOG_ERROR, "LATM packet size larger than maximum size 0x1fff\n");
243  return AVERROR_INVALIDDATA;
244 }
245 
246 static int latm_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
247 {
248  int ret = 1;
249  AVStream *st = s->streams[pkt->stream_index];
250 
251  if (st->codecpar->codec_id == AV_CODEC_ID_AAC) {
252  if (pkt->size > 2 && (AV_RB16(pkt->data) & 0xfff0) == 0xfff0)
253  ret = ff_stream_add_bitstream_filter(st, "aac_adtstoasc", NULL);
254  }
255 
256  return ret;
257 }
258 
260  .name = "latm",
261  .long_name = NULL_IF_CONFIG_SMALL("LOAS/LATM"),
262  .mime_type = "audio/MP4A-LATM",
263  .extensions = "latm,loas",
264  .priv_data_size = sizeof(LATMContext),
265  .audio_codec = AV_CODEC_ID_AAC,
266  .video_codec = AV_CODEC_ID_NONE,
269  .priv_class = &latm_muxer_class,
272 };
uint8_t
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
Libavcodec external API header.
Main libavformat public API header.
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:462
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:225
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, buffer_size_t *size)
Definition: avpacket.c:368
#define AV_RB16
Definition: intreadwrite.h:53
#define flags(name, subs,...)
Definition: cbs_av1.c:561
#define s(width, name)
Definition: cbs_vp9.c:257
#define NULL
Definition: coverity.c:32
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:729
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:346
bitstream reader API header.
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:291
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
@ AV_OPT_TYPE_INT
Definition: opt.h:225
@ AV_CODEC_ID_MP4ALS
Definition: codec_id.h:469
@ AV_CODEC_ID_NONE
Definition: codec_id.h:47
@ AV_CODEC_ID_AAC
Definition: codec_id.h:426
@ AV_CODEC_ID_AAC_LATM
Definition: codec_id.h:473
@ AV_PKT_DATA_NEW_EXTRADATA
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition: packet.h:55
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
int i
Definition: input.c:407
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:218
#define MAX_EXTRADATA_SIZE
Definition: latmenc.c:31
static int latm_write_header(AVFormatContext *s)
Definition: latmenc.c:86
static void latm_write_frame_header(AVFormatContext *s, PutBitContext *bs)
Definition: latmenc.c:115
static const AVOption options[]
Definition: latmenc.c:43
static void copy_bits(PutBitContext *pb, const uint8_t *src, int length)
Definition: latmenc.c:104
static int latm_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
Definition: latmenc.c:246
static const AVClass latm_muxer_class
Definition: latmenc.c:49
static int latm_decode_extradata(AVFormatContext *s, uint8_t *buf, int size)
Definition: latmenc.c:56
AVOutputFormat ff_latm_muxer
Definition: latmenc.c:259
static int latm_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: latmenc.c:161
int ff_alloc_extradata(AVCodecParameters *par, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0.
Definition: utils.c:3312
int ff_stream_add_bitstream_filter(AVStream *st, const char *name, const char *args)
Add a bitstream filter to a stream.
Definition: utils.c:5572
int ff_raw_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawenc.c:29
common internal API header
int buffer_size_t
Definition: internal.h:306
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
int avpriv_mpeg4audio_get_config2(MPEG4AudioConfig *c, const uint8_t *buf, int size, int sync_extension, void *logctx)
Parse MPEG-4 systems extradata from a raw buffer to retrieve audio configuration.
Definition: mpeg4audio.c:190
static int ff_copy_pce_data(PutBitContext *pb, GetBitContext *gb)
Definition: mpeg4audio.h:146
@ AOT_ALS
Y Audio LosslesS.
Definition: mpeg4audio.h:122
@ AOT_SBR
Y Spectral Band Replication.
Definition: mpeg4audio.h:94
static int check_bitstream(AVFormatContext *s, AVStream *st, AVPacket *pkt)
Definition: mux.c:1095
AVOptions.
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:278
bitstream writer API
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:57
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:76
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:110
Describe the class of an AVClass context structure.
Definition: log.h:67
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
This struct describes the properties of an encoded stream.
Definition: codec_par.h:52
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:78
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:74
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
Format I/O context.
Definition: avformat.h:1232
Bytestream IO Context.
Definition: avio.h:161
AVOption.
Definition: opt.h:248
const char * name
Definition: avformat.h:491
This structure stores compressed data.
Definition: packet.h:346
int stream_index
Definition: packet.h:371
int size
Definition: packet.h:370
uint8_t * data
Definition: packet.h:369
Stream structure.
Definition: avformat.h:873
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1038
int object_type
Definition: latmenc.c:37
AVClass * av_class
Definition: latmenc.c:34
int counter
Definition: latmenc.c:38
int channel_conf
Definition: latmenc.c:36
int off
Definition: latmenc.c:35
uint8_t buffer[0x1fff+MAX_EXTRADATA_SIZE+1024]
Definition: latmenc.c:40
int mod
Definition: latmenc.c:39
#define av_log(a,...)
#define src
Definition: vp8dsp.c:255
AVPacket * pkt
Definition: movenc.c:59
AVFormatContext * ctx
Definition: movenc.c:48
int size
static int mod(int a, int b)
Modulo operation with only positive remainders.
Definition: vf_v360.c:747
int len
uint8_t bits
Definition: vp3data.h:141