FFmpeg
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/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 "rawenc.h"
32 
33 #define MAX_EXTRADATA_SIZE 1024
34 
35 typedef struct LATMContext {
37  int off;
40  int counter;
41  int mod;
42  uint8_t buffer[0x1fff + MAX_EXTRADATA_SIZE + 1024];
43 } LATMContext;
44 
45 static const AVOption options[] = {
46  {"smc-interval", "StreamMuxConfig interval.",
47  offsetof(LATMContext, mod), AV_OPT_TYPE_INT, {.i64 = 0x0014}, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM},
48  {NULL},
49 };
50 
51 static const AVClass latm_muxer_class = {
52  .class_name = "LATM/LOAS muxer",
53  .item_name = av_default_item_name,
54  .option = options,
55  .version = LIBAVUTIL_VERSION_INT,
56 };
57 
58 static int latm_decode_extradata(AVFormatContext *s, uint8_t *buf, int size)
59 {
60  LATMContext *ctx = s->priv_data;
61  MPEG4AudioConfig m4ac;
62 
63  if (size > MAX_EXTRADATA_SIZE) {
64  av_log(s, AV_LOG_ERROR, "Extradata is larger than currently supported.\n");
65  return AVERROR_INVALIDDATA;
66  }
67  ctx->off = avpriv_mpeg4audio_get_config2(&m4ac, buf, size, 1, s);
68  if (ctx->off < 0)
69  return ctx->off;
70 
71  if (ctx->object_type == AOT_ALS && (ctx->off & 7)) {
72  // as long as avpriv_mpeg4audio_get_config works correctly this is impossible
73  av_log(s, AV_LOG_ERROR, "BUG: ALS offset is not byte-aligned\n");
74  return AVERROR_INVALIDDATA;
75  }
76  /* FIXME: are any formats not allowed in LATM? */
77 
78  if (m4ac.object_type > AOT_SBR && m4ac.object_type != AOT_ALS) {
79  av_log(s, AV_LOG_ERROR, "Muxing MPEG-4 AOT %d in LATM is not supported\n", m4ac.object_type);
80  return AVERROR_INVALIDDATA;
81  }
82  ctx->channel_conf = m4ac.chan_config;
83  ctx->object_type = m4ac.object_type;
84 
85  return 0;
86 }
87 
89 {
90  AVCodecParameters *par = s->streams[0]->codecpar;
91 
92  if (par->codec_id == AV_CODEC_ID_AAC_LATM)
93  return 0;
94  if (par->codec_id != AV_CODEC_ID_AAC && par->codec_id != AV_CODEC_ID_MP4ALS) {
95  av_log(s, AV_LOG_ERROR, "Only AAC, LATM and ALS are supported\n");
96  return AVERROR(EINVAL);
97  }
98 
99  if (par->extradata_size > 0 &&
101  return AVERROR_INVALIDDATA;
102 
103  return 0;
104 }
105 
106 static void copy_bits(PutBitContext *pb, const uint8_t *src, int length)
107 {
108  int words = length >> 4;
109  int bits = length & 15;
110  int i;
111  for (i = 0; i < words; i++)
112  put_bits(pb, 16, AV_RB16(src + 2 * i));
113  if (bits)
114  put_bits(pb, bits, AV_RB16(src + 2 * words) >> (16 - bits));
115 }
116 
118 {
119  LATMContext *ctx = s->priv_data;
120  AVCodecParameters *par = s->streams[0]->codecpar;
121  int header_size;
122 
123  /* AudioMuxElement */
124  put_bits(bs, 1, !!ctx->counter);
125 
126  if (!ctx->counter) {
127  /* StreamMuxConfig */
128  put_bits(bs, 1, 0); /* audioMuxVersion */
129  put_bits(bs, 1, 1); /* allStreamsSameTimeFraming */
130  put_bits(bs, 6, 0); /* numSubFrames */
131  put_bits(bs, 4, 0); /* numProgram */
132  put_bits(bs, 3, 0); /* numLayer */
133 
134  /* AudioSpecificConfig */
135  if (ctx->object_type == AOT_ALS) {
136  header_size = (par->extradata_size - (ctx->off >> 3)) * 8;
137  copy_bits(bs, &par->extradata[ctx->off >> 3], header_size);
138  } else {
139  // + 3 assumes not scalable and dependsOnCoreCoder == 0,
140  // see decode_ga_specific_config in libavcodec/aacdec.c
141  copy_bits(bs, par->extradata, ctx->off + 3);
142 
143  if (!ctx->channel_conf) {
144  GetBitContext gb;
145  int ret = init_get_bits8(&gb, par->extradata, par->extradata_size);
146  av_assert0(ret >= 0); // extradata size has been checked already, so this should not fail
147  skip_bits_long(&gb, ctx->off + 3);
148  ff_copy_pce_data(bs, &gb);
149  }
150  }
151 
152  put_bits(bs, 3, 0); /* frameLengthType */
153  put_bits(bs, 8, 0xff); /* latmBufferFullness */
154 
155  put_bits(bs, 1, 0); /* otherDataPresent */
156  put_bits(bs, 1, 0); /* crcCheckPresent */
157  }
158 
159  ctx->counter++;
160  ctx->counter %= ctx->mod;
161 }
162 
164 {
165  LATMContext *ctx = s->priv_data;
166  AVCodecParameters *par = s->streams[0]->codecpar;
167  AVIOContext *pb = s->pb;
168  PutBitContext bs;
169  int i, len;
170  uint8_t loas_header[] = "\x56\xe0\x00";
171 
172  if (par->codec_id == AV_CODEC_ID_AAC_LATM)
173  return ff_raw_write_packet(s, pkt);
174 
175  if (!par->extradata) {
176  if(pkt->size > 2 && pkt->data[0] == 0x56 && (pkt->data[1] >> 4) == 0xe &&
177  (AV_RB16(pkt->data + 1) & 0x1FFF) + 3 == pkt->size)
178  return ff_raw_write_packet(s, pkt);
179  else {
180  uint8_t *side_data;
181  size_t side_data_size;
182  int ret;
183 
185  &side_data_size);
186  if (side_data_size) {
187  if (latm_decode_extradata(s, side_data, side_data_size) < 0)
188  return AVERROR_INVALIDDATA;
189  ret = ff_alloc_extradata(par, side_data_size);
190  if (ret < 0)
191  return ret;
192  memcpy(par->extradata, side_data, side_data_size);
193  } else
194  return AVERROR_INVALIDDATA;
195  }
196  }
197 
198  if (pkt->size > 0x1fff)
199  goto too_large;
200 
201  init_put_bits(&bs, ctx->buffer, pkt->size+1024+MAX_EXTRADATA_SIZE);
202 
204 
205  /* PayloadLengthInfo() */
206  for (i = 0; i <= pkt->size-255; i+=255)
207  put_bits(&bs, 8, 255);
208 
209  put_bits(&bs, 8, pkt->size-i);
210 
211  /* The LATM payload is written unaligned */
212 
213  /* PayloadMux() */
214  if (pkt->size && (pkt->data[0] & 0xe1) == 0x81) {
215  // Convert byte-aligned DSE to non-aligned.
216  // Due to the input format encoding we know that
217  // it is naturally byte-aligned in the input stream,
218  // so there are no padding bits to account for.
219  // To avoid having to add padding bits and rearrange
220  // the whole stream we just remove the byte-align flag.
221  // This allows us to remux our FATE AAC samples into latm
222  // files that are still playable with minimal effort.
223  put_bits(&bs, 8, pkt->data[0] & 0xfe);
224  copy_bits(&bs, pkt->data + 1, 8*pkt->size - 8);
225  } else
226  copy_bits(&bs, pkt->data, 8*pkt->size);
227 
228  flush_put_bits(&bs);
229 
230  len = put_bytes_output(&bs);
231 
232  if (len > 0x1fff)
233  goto too_large;
234 
235  loas_header[1] |= (len >> 8) & 0x1f;
236  loas_header[2] |= len & 0xff;
237 
238  avio_write(pb, loas_header, 3);
239  avio_write(pb, ctx->buffer, len);
240 
241  return 0;
242 
243 too_large:
244  av_log(s, AV_LOG_ERROR, "LATM packet size larger than maximum size 0x1fff\n");
245  return AVERROR_INVALIDDATA;
246 }
247 
249  const AVPacket *pkt)
250 {
251  int ret = 1;
252 
253  if (st->codecpar->codec_id == AV_CODEC_ID_AAC) {
254  if (pkt->size > 2 && (AV_RB16(pkt->data) & 0xfff0) == 0xfff0)
255  ret = ff_stream_add_bitstream_filter(st, "aac_adtstoasc", NULL);
256  }
257 
258  return ret;
259 }
260 
262  .name = "latm",
263  .long_name = NULL_IF_CONFIG_SMALL("LOAS/LATM"),
264  .mime_type = "audio/MP4A-LATM",
265  .extensions = "latm,loas",
266  .priv_data_size = sizeof(LATMContext),
267  .audio_codec = AV_CODEC_ID_AAC,
268  .video_codec = AV_CODEC_ID_NONE,
271  .priv_class = &latm_muxer_class,
274 };
skip_bits_long
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:292
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:74
AVOutputFormat::name
const char * name
Definition: avformat.h:504
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
put_bytes_output
static int put_bytes_output(const PutBitContext *s)
Definition: put_bits.h:88
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:52
latm_muxer_class
static const AVClass latm_muxer_class
Definition: latmenc.c:51
AVFMT_NOTIMESTAMPS
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:475
init_put_bits
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:61
check_bitstream
static int check_bitstream(AVFormatContext *s, FFStream *sti, AVPacket *pkt)
Definition: mux.c:1062
put_bits
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:220
AVPacket::data
uint8_t * data
Definition: packet.h:373
AVOption
AVOption.
Definition: opt.h:247
MPEG4AudioConfig
Definition: mpeg4audio.h:32
LATMContext::av_class
AVClass * av_class
Definition: latmenc.c:36
mpeg4audio.h
latm_check_bitstream
static int latm_check_bitstream(AVFormatContext *s, AVStream *st, const AVPacket *pkt)
Definition: latmenc.c:248
GetBitContext
Definition: get_bits.h:62
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:678
codec_id.h
avpriv_mpeg4audio_get_config2
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:165
s
#define s(width, name)
Definition: cbs_vp9.c:257
LATMContext::off
int off
Definition: latmenc.c:37
AV_OPT_FLAG_ENCODING_PARAM
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:277
bits
uint8_t bits
Definition: vp3data.h:141
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
ctx
AVFormatContext * ctx
Definition: movenc.c:48
ff_raw_write_packet
int ff_raw_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawenc.c:29
get_bits.h
LATMContext::object_type
int object_type
Definition: latmenc.c:39
PutBitContext
Definition: put_bits.h:49
AVFormatContext
Format I/O context.
Definition: avformat.h:1200
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1095
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
ff_stream_add_bitstream_filter
int ff_stream_add_bitstream_filter(AVStream *st, const char *name, const char *args)
Add a bitstream filter to a stream.
Definition: utils.c:1790
src
#define src
Definition: vp8dsp.c:255
ff_copy_pce_data
static int ff_copy_pce_data(PutBitContext *pb, GetBitContext *gb)
Definition: mpeg4audio.h:132
AV_CODEC_ID_MP4ALS
@ AV_CODEC_ID_MP4ALS
Definition: codec_id.h:468
AVCodecParameters::extradata_size
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:78
AV_CODEC_ID_AAC
@ AV_CODEC_ID_AAC
Definition: codec_id.h:425
latm_decode_extradata
static int latm_decode_extradata(AVFormatContext *s, uint8_t *buf, int size)
Definition: latmenc.c:58
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
AVPacket::size
int size
Definition: packet.h:374
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
latm_write_packet
static int latm_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: latmenc.c:163
size
int size
Definition: twinvq_data.h:10344
LATMContext::buffer
uint8_t buffer[0x1fff+MAX_EXTRADATA_SIZE+1024]
Definition: latmenc.c:42
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:232
LATMContext::counter
int counter
Definition: latmenc.c:40
latm_write_header
static int latm_write_header(AVFormatContext *s)
Definition: latmenc.c:88
ff_latm_muxer
const AVOutputFormat ff_latm_muxer
Definition: latmenc.c:261
rawenc.h
write_packet
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:727
LATMContext
Definition: aacdec.c:266
LATMContext::mod
int mod
Definition: latmenc.c:41
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:48
AVOutputFormat
Definition: avformat.h:503
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
packet.h
av_packet_get_side_data
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, size_t *size)
Get side information from packet.
Definition: avpacket.c:253
MAX_EXTRADATA_SIZE
#define MAX_EXTRADATA_SIZE
Definition: latmenc.c:33
LATMContext::channel_conf
int channel_conf
Definition: latmenc.c:38
MPEG4AudioConfig::chan_config
int chan_config
Definition: mpeg4audio.h:36
len
int len
Definition: vorbis_enc_data.h:426
mod
static int mod(int a, int b)
Modulo operation with only positive remainders.
Definition: vf_v360.c:749
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:935
copy_bits
static void copy_bits(PutBitContext *pb, const uint8_t *src, int length)
Definition: latmenc.c:106
AVClass::class_name
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:71
avformat.h
MPEG4AudioConfig::object_type
int object_type
Definition: mpeg4audio.h:33
AV_PKT_DATA_NEW_EXTRADATA
@ 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
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:224
AOT_SBR
@ AOT_SBR
Y Spectral Band Replication.
Definition: mpeg4audio.h:80
latm_write_frame_header
static void latm_write_frame_header(AVFormatContext *s, PutBitContext *bs)
Definition: latmenc.c:117
flush_put_bits
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:142
AOT_ALS
@ AOT_ALS
Y Audio LosslesS.
Definition: mpeg4audio.h:108
codec_par.h
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
AVPacket
This structure stores compressed data.
Definition: packet.h:350
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AV_CODEC_ID_AAC_LATM
@ AV_CODEC_ID_AAC_LATM
Definition: codec_id.h:472
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
write_header
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:347
put_bits.h
options
static const AVOption options[]
Definition: latmenc.c:45
AV_RB16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:98
ff_alloc_extradata
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:451