Libav
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 
29 typedef struct {
31  int off;
34  int counter;
35  int mod;
36 } LATMContext;
37 
38 static const AVOption options[] = {
39  {"smc-interval", "StreamMuxConfig interval.",
40  offsetof(LATMContext, mod), AV_OPT_TYPE_INT, {.i64 = 0x0014}, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM},
41  {NULL},
42 };
43 
44 static const AVClass latm_muxer_class = {
45  .class_name = "LATM/LOAS muxer",
46  .item_name = av_default_item_name,
47  .option = options,
48  .version = LIBAVUTIL_VERSION_INT,
49 };
50 
51 static int latm_decode_extradata(LATMContext *ctx, uint8_t *buf, int size)
52 {
53  GetBitContext gb;
54  MPEG4AudioConfig m4ac;
55 
56  init_get_bits(&gb, buf, size * 8);
57  ctx->off = avpriv_mpeg4audio_get_config(&m4ac, buf, size * 8, 1);
58  if (ctx->off < 0)
59  return ctx->off;
60  skip_bits_long(&gb, ctx->off);
61 
62  /* FIXME: are any formats not allowed in LATM? */
63 
64  if (m4ac.object_type > AOT_SBR && m4ac.object_type != AOT_ALS) {
65  av_log(ctx, AV_LOG_ERROR, "Muxing MPEG-4 AOT %d in LATM is not supported\n", m4ac.object_type);
66  return AVERROR_INVALIDDATA;
67  }
68  ctx->channel_conf = m4ac.chan_config;
69  ctx->object_type = m4ac.object_type;
70 
71  return 0;
72 }
73 
75 {
76  LATMContext *ctx = s->priv_data;
77  AVCodecContext *avctx = s->streams[0]->codec;
78 
79  if (avctx->extradata_size > 0 &&
80  latm_decode_extradata(ctx, avctx->extradata, avctx->extradata_size) < 0)
81  return AVERROR_INVALIDDATA;
82 
83  return 0;
84 }
85 
87 {
88  LATMContext *ctx = s->priv_data;
89  AVCodecContext *avctx = s->streams[0]->codec;
90  GetBitContext gb;
91  int header_size;
92 
93  /* AudioMuxElement */
94  put_bits(bs, 1, !!ctx->counter);
95 
96  if (!ctx->counter) {
97  init_get_bits(&gb, avctx->extradata, avctx->extradata_size * 8);
98 
99  /* StreamMuxConfig */
100  put_bits(bs, 1, 0); /* audioMuxVersion */
101  put_bits(bs, 1, 1); /* allStreamsSameTimeFraming */
102  put_bits(bs, 6, 0); /* numSubFrames */
103  put_bits(bs, 4, 0); /* numProgram */
104  put_bits(bs, 3, 0); /* numLayer */
105 
106  /* AudioSpecificConfig */
107  if (ctx->object_type == AOT_ALS) {
108  header_size = avctx->extradata_size-(ctx->off + 7) >> 3;
109  avpriv_copy_bits(bs, &avctx->extradata[ctx->off], header_size);
110  } else {
111  avpriv_copy_bits(bs, avctx->extradata, ctx->off + 3);
112 
113  if (!ctx->channel_conf) {
114  avpriv_copy_pce_data(bs, &gb);
115  }
116  }
117 
118  put_bits(bs, 3, 0); /* frameLengthType */
119  put_bits(bs, 8, 0xff); /* latmBufferFullness */
120 
121  put_bits(bs, 1, 0); /* otherDataPresent */
122  put_bits(bs, 1, 0); /* crcCheckPresent */
123  }
124 
125  ctx->counter++;
126  ctx->counter %= ctx->mod;
127 
128  return 0;
129 }
130 
132 {
133  AVIOContext *pb = s->pb;
134  PutBitContext bs;
135  int i, len;
136  uint8_t loas_header[] = "\x56\xe0\x00";
137  uint8_t *buf;
138 
139  if (pkt->size > 2 && pkt->data[0] == 0xff && (pkt->data[1] >> 4) == 0xf) {
140  av_log(s, AV_LOG_ERROR, "ADTS header detected - ADTS will not be incorrectly muxed into LATM\n");
141  return AVERROR_INVALIDDATA;
142  }
143 
144  buf = av_malloc(pkt->size+1024);
145  if (!buf)
146  return AVERROR(ENOMEM);
147 
148  init_put_bits(&bs, buf, pkt->size+1024);
149 
150  latm_write_frame_header(s, &bs);
151 
152  /* PayloadLengthInfo() */
153  for (i = 0; i <= pkt->size-255; i+=255)
154  put_bits(&bs, 8, 255);
155 
156  put_bits(&bs, 8, pkt->size-i);
157 
158  /* The LATM payload is written unaligned */
159 
160  /* PayloadMux() */
161  for (i = 0; i < pkt->size; i++)
162  put_bits(&bs, 8, pkt->data[i]);
163 
165  flush_put_bits(&bs);
166 
167  len = put_bits_count(&bs) >> 3;
168 
169  loas_header[1] |= (len >> 8) & 0x1f;
170  loas_header[2] |= len & 0xff;
171 
172  avio_write(pb, loas_header, 3);
173  avio_write(pb, buf, len);
174 
175  av_free(buf);
176 
177  return 0;
178 }
179 
181  .name = "latm",
182  .long_name = NULL_IF_CONFIG_SMALL("LOAS/LATM"),
183  .mime_type = "audio/MP4A-LATM",
184  .extensions = "latm",
185  .priv_data_size = sizeof(LATMContext),
186  .audio_codec = AV_CODEC_ID_AAC,
187  .video_codec = AV_CODEC_ID_NONE,
190  .priv_class = &latm_muxer_class,
192 };
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
static const AVOption options[]
Definition: latmenc.c:38
static int latm_decode_extradata(LATMContext *ctx, uint8_t *buf, int size)
Definition: latmenc.c:51
int size
AVOption.
Definition: opt.h:234
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:199
static int write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: assenc.c:58
int size
Definition: avcodec.h:968
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:45
static const AVClass latm_muxer_class
Definition: latmenc.c:44
Format I/O context.
Definition: avformat.h:922
int counter
Definition: latmenc.c:34
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:38
uint8_t
AVOptions.
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1158
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:990
AVClass * av_class
Definition: latmenc.c:30
uint8_t * data
Definition: avcodec.h:967
static int flags
Definition: log.c:44
bitstream reader API header.
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:165
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:264
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:168
static void put_bits(PutBitContext *s, int n, unsigned int value)
Write up to 31 bits into a bitstream.
Definition: put_bits.h:134
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:67
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
int channel_conf
Definition: latmenc.c:32
const char * name
Definition: avformat.h:446
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
static int latm_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: latmenc.c:131
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:416
NULL
Definition: eval.c:55
Libavcodec external API header.
static int latm_write_frame_header(AVFormatContext *s, PutBitContext *bs)
Definition: latmenc.c:86
AVIOContext * pb
I/O context.
Definition: avformat.h:964
av_default_item_name
Definition: dnxhdenc.c:52
main external API structure.
Definition: avcodec.h:1044
int mod
Definition: latmenc.c:35
static int latm_write_header(AVFormatContext *s)
Definition: latmenc.c:74
int extradata_size
Definition: avcodec.h:1159
Describe the class of an AVClass context structure.
Definition: log.h:33
int object_type
Definition: latmenc.c:33
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:375
Y Spectral Band Replication.
Definition: mpeg4audio.h:64
Main libavformat public API header.
int off
Definition: latmenc.c:31
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:83
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
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:79
AVOutputFormat ff_latm_muxer
Definition: latmenc.c:180
Y Audio LosslesS.
Definition: mpeg4audio.h:92
int len
void * priv_data
Format private data.
Definition: avformat.h:950
int avpriv_copy_pce_data(PutBitContext *pb, GetBitContext *gb)
Definition: mpeg4audio.c:155
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:380
This structure stores compressed data.
Definition: avcodec.h:944
bitstream writer API