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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 "rawenc.h"
29 
30 #define MAX_EXTRADATA_SIZE 1024
31 
32 typedef struct {
34  int off;
37  int counter;
38  int mod;
39  uint8_t buffer[0x1fff + MAX_EXTRADATA_SIZE + 1024];
40 } LATMContext;
41 
42 static const AVOption options[] = {
43  {"smc-interval", "StreamMuxConfig interval.",
44  offsetof(LATMContext, mod), AV_OPT_TYPE_INT, {.i64 = 0x0014}, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM},
45  {NULL},
46 };
47 
48 static const AVClass latm_muxer_class = {
49  .class_name = "LATM/LOAS muxer",
50  .item_name = av_default_item_name,
51  .option = options,
52  .version = LIBAVUTIL_VERSION_INT,
53 };
54 
56 {
57  MPEG4AudioConfig m4ac;
58 
59  if (size > MAX_EXTRADATA_SIZE) {
60  av_log(ctx, AV_LOG_ERROR, "Extradata is larger than currently supported.\n");
61  return AVERROR_INVALIDDATA;
62  }
63  ctx->off = avpriv_mpeg4audio_get_config(&m4ac, buf, size * 8, 1);
64  if (ctx->off < 0)
65  return ctx->off;
66 
67  if (ctx->object_type == AOT_ALS && (ctx->off & 7)) {
68  // as long as avpriv_mpeg4audio_get_config works correctly this is impossible
69  av_log(ctx, AV_LOG_ERROR, "BUG: ALS offset is not byte-aligned\n");
70  return AVERROR_INVALIDDATA;
71  }
72  /* FIXME: are any formats not allowed in LATM? */
73 
74  if (m4ac.object_type > AOT_SBR && m4ac.object_type != AOT_ALS) {
75  av_log(ctx, AV_LOG_ERROR, "Muxing MPEG-4 AOT %d in LATM is not supported\n", m4ac.object_type);
76  return AVERROR_INVALIDDATA;
77  }
78  ctx->channel_conf = m4ac.chan_config;
79  ctx->object_type = m4ac.object_type;
80 
81  return 0;
82 }
83 
85 {
86  LATMContext *ctx = s->priv_data;
87  AVCodecContext *avctx = s->streams[0]->codec;
88 
89  if (avctx->codec_id == AV_CODEC_ID_AAC_LATM)
90  return 0;
91 
92  if (avctx->extradata_size > 0 &&
93  latm_decode_extradata(ctx, avctx->extradata, avctx->extradata_size) < 0)
94  return AVERROR_INVALIDDATA;
95 
96  return 0;
97 }
98 
100 {
101  LATMContext *ctx = s->priv_data;
102  AVCodecContext *avctx = s->streams[0]->codec;
103  int header_size;
104 
105  /* AudioMuxElement */
106  put_bits(bs, 1, !!ctx->counter);
107 
108  if (!ctx->counter) {
109  /* StreamMuxConfig */
110  put_bits(bs, 1, 0); /* audioMuxVersion */
111  put_bits(bs, 1, 1); /* allStreamsSameTimeFraming */
112  put_bits(bs, 6, 0); /* numSubFrames */
113  put_bits(bs, 4, 0); /* numProgram */
114  put_bits(bs, 3, 0); /* numLayer */
115 
116  /* AudioSpecificConfig */
117  if (ctx->object_type == AOT_ALS) {
118  header_size = avctx->extradata_size-(ctx->off >> 3);
119  avpriv_copy_bits(bs, &avctx->extradata[ctx->off >> 3], header_size);
120  } else {
121  // + 3 assumes not scalable and dependsOnCoreCoder == 0,
122  // see decode_ga_specific_config in libavcodec/aacdec.c
123  avpriv_copy_bits(bs, avctx->extradata, ctx->off + 3);
124 
125  if (!ctx->channel_conf) {
126  GetBitContext gb;
127  init_get_bits(&gb, avctx->extradata, avctx->extradata_size * 8);
128  skip_bits_long(&gb, ctx->off + 3);
129  avpriv_copy_pce_data(bs, &gb);
130  }
131  }
132 
133  put_bits(bs, 3, 0); /* frameLengthType */
134  put_bits(bs, 8, 0xff); /* latmBufferFullness */
135 
136  put_bits(bs, 1, 0); /* otherDataPresent */
137  put_bits(bs, 1, 0); /* crcCheckPresent */
138  }
139 
140  ctx->counter++;
141  ctx->counter %= ctx->mod;
142 }
143 
145 {
146  LATMContext *ctx = s->priv_data;
147  AVIOContext *pb = s->pb;
148  PutBitContext bs;
149  int i, len;
150  uint8_t loas_header[] = "\x56\xe0\x00";
151 
153  return ff_raw_write_packet(s, pkt);
154 
155  if (pkt->size > 2 && pkt->data[0] == 0xff && (pkt->data[1] >> 4) == 0xf) {
156  av_log(s, AV_LOG_ERROR, "ADTS header detected - ADTS will not be incorrectly muxed into LATM\n");
157  return AVERROR_INVALIDDATA;
158  }
159 
160  if (!s->streams[0]->codec->extradata) {
161  if(pkt->size > 2 && pkt->data[0] == 0x56 && (pkt->data[1] >> 4) == 0xe &&
162  (AV_RB16(pkt->data + 1) & 0x1FFF) + 3 == pkt->size)
163  return ff_raw_write_packet(s, pkt);
164  else
165  return AVERROR_INVALIDDATA;
166  }
167 
168  if (pkt->size > 0x1fff)
169  goto too_large;
170 
171  init_put_bits(&bs, ctx->buffer, pkt->size+1024+MAX_EXTRADATA_SIZE);
172 
173  latm_write_frame_header(s, &bs);
174 
175  /* PayloadLengthInfo() */
176  for (i = 0; i <= pkt->size-255; i+=255)
177  put_bits(&bs, 8, 255);
178 
179  put_bits(&bs, 8, pkt->size-i);
180 
181  /* The LATM payload is written unaligned */
182 
183  /* PayloadMux() */
184  if (pkt->size && (pkt->data[0] & 0xe1) == 0x81) {
185  // Convert byte-aligned DSE to non-aligned.
186  // Due to the input format encoding we know that
187  // it is naturally byte-aligned in the input stream,
188  // so there are no padding bits to account for.
189  // To avoid having to add padding bits and rearrange
190  // the whole stream we just remove the byte-align flag.
191  // This allows us to remux our FATE AAC samples into latm
192  // files that are still playable with minimal effort.
193  put_bits(&bs, 8, pkt->data[0] & 0xfe);
194  avpriv_copy_bits(&bs, pkt->data + 1, 8*pkt->size - 8);
195  } else
196  avpriv_copy_bits(&bs, pkt->data, 8*pkt->size);
197 
199  flush_put_bits(&bs);
200 
201  len = put_bits_count(&bs) >> 3;
202 
203  if (len > 0x1fff)
204  goto too_large;
205 
206  loas_header[1] |= (len >> 8) & 0x1f;
207  loas_header[2] |= len & 0xff;
208 
209  avio_write(pb, loas_header, 3);
210  avio_write(pb, ctx->buffer, len);
211 
212  return 0;
213 
214 too_large:
215  av_log(s, AV_LOG_ERROR, "LATM packet size larger than maximum size 0x1fff\n");
216  return AVERROR_INVALIDDATA;
217 }
218 
220  .name = "latm",
221  .long_name = NULL_IF_CONFIG_SMALL("LOAS/LATM"),
222  .mime_type = "audio/MP4A-LATM",
223  .extensions = "latm,loas",
224  .priv_data_size = sizeof(LATMContext),
225  .audio_codec = AV_CODEC_ID_AAC,
226  .video_codec = AV_CODEC_ID_NONE,
229  .priv_class = &latm_muxer_class,
230 };
const char * s
Definition: avisynth_c.h:668
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static const AVOption options[]
Definition: latmenc.c:42
static int latm_decode_extradata(LATMContext *ctx, uint8_t *buf, int size)
Definition: latmenc.c:55
AVOption.
Definition: opt.h:251
av_default_item_name
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:198
static int write_packet(AVFormatContext *s, AVPacket *pkt)
void avpriv_copy_bits(PutBitContext *pb, const uint8_t *src, int length)
Copy the content of src to the bitstream.
Definition: bitstream.c:61
void avpriv_align_put_bits(PutBitContext *s)
Pad the bitstream with zeros up to the next byte boundary.
Definition: bitstream.c:46
static const AVClass latm_muxer_class
Definition: latmenc.c:48
Format I/O context.
Definition: avformat.h:944
int counter
Definition: latmenc.c:37
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:55
uint8_t
AVOptions.
static AVPacket pkt
Definition: demuxing.c:56
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
AVStream ** streams
Definition: avformat.h:992
AVClass * av_class
Definition: latmenc.c:33
uint8_t * data
bitstream reader API header.
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:173
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:281
#define AV_RB16
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
static void put_bits(J2kEncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:160
external API header
int size
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:73
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:662
int channel_conf
Definition: latmenc.c:35
const char * name
Definition: avformat.h:378
int ff_raw_write_packet(AVFormatContext *s, AVPacket *pkt)
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
static int latm_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: latmenc.c:144
NULL
Definition: eval.c:55
enum AVCodecID codec_id
AVIOContext * pb
I/O context.
Definition: avformat.h:977
main external API structure.
int mod
Definition: latmenc.c:38
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
#define MAX_EXTRADATA_SIZE
Definition: latmenc.c:30
void * buf
Definition: avisynth_c.h:594
static int latm_write_header(AVFormatContext *s)
Definition: latmenc.c:84
Describe the class of an AVClass context structure.
Definition: log.h:50
int object_type
Definition: latmenc.c:36
synthesis window for stochastic i
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:379
Y Spectral Band Replication.
Definition: mpeg4audio.h:64
Main libavformat public API header.
int off
Definition: latmenc.c:34
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:81
the buffer and buffer reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFilterBuffer structures They must not be accessed but through references stored in AVFilterBufferRef structures Several references can point to the same buffer
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:54
int avpriv_mpeg4audio_get_config(MPEG4AudioConfig *c, const uint8_t *buf, int bit_size, int sync_extension)
Parse MPEG-4 systems extradata to retrieve audio configuration.
Definition: mpeg4audio.c:81
AVOutputFormat ff_latm_muxer
Definition: latmenc.c:219
Y Audio LosslesS.
Definition: mpeg4audio.h:92
int len
static void latm_write_frame_header(AVFormatContext *s, PutBitContext *bs)
Definition: latmenc.c:99
void * priv_data
Format private data.
Definition: avformat.h:964
int avpriv_copy_pce_data(PutBitContext *pb, GetBitContext *gb)
Definition: mpeg4audio.c:161
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:470
uint8_t buffer[0x1fff+MAX_EXTRADATA_SIZE+1024]
Definition: latmenc.c:39
This structure stores compressed data.
bitstream writer API