FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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  MPEG4AudioConfig m4ac;
59 
60  if (size > MAX_EXTRADATA_SIZE) {
61  av_log(ctx, AV_LOG_ERROR, "Extradata is larger than currently supported.\n");
62  return AVERROR_INVALIDDATA;
63  }
64  ctx->off = avpriv_mpeg4audio_get_config(&m4ac, buf, size * 8, 1);
65  if (ctx->off < 0)
66  return ctx->off;
67 
68  if (ctx->object_type == AOT_ALS && (ctx->off & 7)) {
69  // as long as avpriv_mpeg4audio_get_config works correctly this is impossible
70  av_log(ctx, AV_LOG_ERROR, "BUG: ALS offset is not byte-aligned\n");
71  return AVERROR_INVALIDDATA;
72  }
73  /* FIXME: are any formats not allowed in LATM? */
74 
75  if (m4ac.object_type > AOT_SBR && m4ac.object_type != AOT_ALS) {
76  av_log(ctx, AV_LOG_ERROR, "Muxing MPEG-4 AOT %d in LATM is not supported\n", m4ac.object_type);
77  return AVERROR_INVALIDDATA;
78  }
79  ctx->channel_conf = m4ac.chan_config;
80  ctx->object_type = m4ac.object_type;
81 
82  return 0;
83 }
84 
86 {
88  AVCodecParameters *par = s->streams[0]->codecpar;
89 
90  if (par->codec_id == AV_CODEC_ID_AAC_LATM)
91  return 0;
92 
93  if (par->extradata_size > 0 &&
94  latm_decode_extradata(ctx, par->extradata, par->extradata_size) < 0)
95  return AVERROR_INVALIDDATA;
96 
97  return 0;
98 }
99 
101 {
102  LATMContext *ctx = s->priv_data;
103  AVCodecParameters *par = s->streams[0]->codecpar;
104  int header_size;
105 
106  /* AudioMuxElement */
107  put_bits(bs, 1, !!ctx->counter);
108 
109  if (!ctx->counter) {
110  /* StreamMuxConfig */
111  put_bits(bs, 1, 0); /* audioMuxVersion */
112  put_bits(bs, 1, 1); /* allStreamsSameTimeFraming */
113  put_bits(bs, 6, 0); /* numSubFrames */
114  put_bits(bs, 4, 0); /* numProgram */
115  put_bits(bs, 3, 0); /* numLayer */
116 
117  /* AudioSpecificConfig */
118  if (ctx->object_type == AOT_ALS) {
119  header_size = par->extradata_size-(ctx->off >> 3);
120  avpriv_copy_bits(bs, &par->extradata[ctx->off >> 3], header_size);
121  } else {
122  // + 3 assumes not scalable and dependsOnCoreCoder == 0,
123  // see decode_ga_specific_config in libavcodec/aacdec.c
124  avpriv_copy_bits(bs, par->extradata, ctx->off + 3);
125 
126  if (!ctx->channel_conf) {
127  GetBitContext gb;
128  int ret = init_get_bits8(&gb, par->extradata, par->extradata_size);
129  av_assert0(ret >= 0); // extradata size has been checked already, so this should not fail
130  skip_bits_long(&gb, ctx->off + 3);
131  avpriv_copy_pce_data(bs, &gb);
132  }
133  }
134 
135  put_bits(bs, 3, 0); /* frameLengthType */
136  put_bits(bs, 8, 0xff); /* latmBufferFullness */
137 
138  put_bits(bs, 1, 0); /* otherDataPresent */
139  put_bits(bs, 1, 0); /* crcCheckPresent */
140  }
141 
142  ctx->counter++;
143  ctx->counter %= ctx->mod;
144 }
145 
147 {
148  LATMContext *ctx = s->priv_data;
149  AVIOContext *pb = s->pb;
150  PutBitContext bs;
151  int i, len;
152  uint8_t loas_header[] = "\x56\xe0\x00";
153 
155  return ff_raw_write_packet(s, pkt);
156 
157  if (!s->streams[0]->codecpar->extradata) {
158  if(pkt->size > 2 && pkt->data[0] == 0x56 && (pkt->data[1] >> 4) == 0xe &&
159  (AV_RB16(pkt->data + 1) & 0x1FFF) + 3 == pkt->size)
160  return ff_raw_write_packet(s, pkt);
161  else
162  return AVERROR_INVALIDDATA;
163  }
164 
165  if (pkt->size > 0x1fff)
166  goto too_large;
167 
168  init_put_bits(&bs, ctx->buffer, pkt->size+1024+MAX_EXTRADATA_SIZE);
169 
170  latm_write_frame_header(s, &bs);
171 
172  /* PayloadLengthInfo() */
173  for (i = 0; i <= pkt->size-255; i+=255)
174  put_bits(&bs, 8, 255);
175 
176  put_bits(&bs, 8, pkt->size-i);
177 
178  /* The LATM payload is written unaligned */
179 
180  /* PayloadMux() */
181  if (pkt->size && (pkt->data[0] & 0xe1) == 0x81) {
182  // Convert byte-aligned DSE to non-aligned.
183  // Due to the input format encoding we know that
184  // it is naturally byte-aligned in the input stream,
185  // so there are no padding bits to account for.
186  // To avoid having to add padding bits and rearrange
187  // the whole stream we just remove the byte-align flag.
188  // This allows us to remux our FATE AAC samples into latm
189  // files that are still playable with minimal effort.
190  put_bits(&bs, 8, pkt->data[0] & 0xfe);
191  avpriv_copy_bits(&bs, pkt->data + 1, 8*pkt->size - 8);
192  } else
193  avpriv_copy_bits(&bs, pkt->data, 8*pkt->size);
194 
196  flush_put_bits(&bs);
197 
198  len = put_bits_count(&bs) >> 3;
199 
200  if (len > 0x1fff)
201  goto too_large;
202 
203  loas_header[1] |= (len >> 8) & 0x1f;
204  loas_header[2] |= len & 0xff;
205 
206  avio_write(pb, loas_header, 3);
207  avio_write(pb, ctx->buffer, len);
208 
209  return 0;
210 
211 too_large:
212  av_log(s, AV_LOG_ERROR, "LATM packet size larger than maximum size 0x1fff\n");
213  return AVERROR_INVALIDDATA;
214 }
215 
216 static int latm_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
217 {
218  int ret = 1;
219  AVStream *st = s->streams[pkt->stream_index];
220 
221  if (st->codecpar->codec_id == AV_CODEC_ID_AAC) {
222  if (pkt->size > 2 && (AV_RB16(pkt->data) & 0xfff0) == 0xfff0)
223  ret = ff_stream_add_bitstream_filter(st, "aac_adtstoasc", NULL);
224  }
225 
226  return ret;
227 }
228 
230  .name = "latm",
231  .long_name = NULL_IF_CONFIG_SMALL("LOAS/LATM"),
232  .mime_type = "audio/MP4A-LATM",
233  .extensions = "latm,loas",
234  .priv_data_size = sizeof(LATMContext),
235  .audio_codec = AV_CODEC_ID_AAC,
236  .video_codec = AV_CODEC_ID_NONE,
239  .priv_class = &latm_muxer_class,
240  .check_bitstream= latm_check_bitstream,
242 };
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:672
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
Bytestream IO Context.
Definition: avio.h:155
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static const AVOption options[]
Definition: latmenc.c:43
static int latm_decode_extradata(LATMContext *ctx, uint8_t *buf, int size)
Definition: latmenc.c:56
AVOption.
Definition: opt.h:246
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:206
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:204
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:4066
int size
Definition: avcodec.h:1658
void avpriv_copy_bits(PutBitContext *pb, const uint8_t *src, int length)
Copy the content of src to the bitstream.
Definition: bitstream.c:64
void avpriv_align_put_bits(PutBitContext *s)
Pad the bitstream with zeros up to the next byte boundary.
Definition: bitstream.c:48
static const AVClass latm_muxer_class
Definition: latmenc.c:49
static AVPacket pkt
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:87
This struct describes the properties of an encoded stream.
Definition: avcodec.h:4058
Format I/O context.
Definition: avformat.h:1349
int counter
Definition: latmenc.c:38
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
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
uint8_t
AVOptions.
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1417
AVClass * av_class
Definition: latmenc.c:34
uint8_t * data
Definition: avcodec.h:1657
static int flags
Definition: log.c:57
bitstream reader API header.
ptrdiff_t size
Definition: opengl_enc.c:101
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:205
#define av_log(a,...)
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:276
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
av_default_item_name
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:85
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:4084
int channel_conf
Definition: latmenc.c:36
const char * name
Definition: avformat.h:524
AVFormatContext * ctx
Definition: movenc.c:48
int ff_raw_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawenc.c:29
Stream structure.
Definition: avformat.h:889
static int latm_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: latmenc.c:146
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:486
Libavcodec external API header.
AVIOContext * pb
I/O context.
Definition: avformat.h:1391
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:456
int mod
Definition: latmenc.c:39
#define MAX_EXTRADATA_SIZE
Definition: latmenc.c:31
void * buf
Definition: avisynth_c.h:690
static int latm_write_header(AVFormatContext *s)
Definition: latmenc.c:85
Describe the class of an AVClass context structure.
Definition: log.h:67
int object_type
Definition: latmenc.c:37
Y Spectral Band Replication.
Definition: mpeg4audio.h:75
Main libavformat public API header.
int off
Definition: latmenc.c:35
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
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 from a raw buffer to retrieve audio configuration. ...
Definition: mpeg4audio.c:155
AVOutputFormat ff_latm_muxer
Definition: latmenc.c:229
Y Audio LosslesS.
Definition: mpeg4audio.h:103
int len
static void latm_write_frame_header(AVFormatContext *s, PutBitContext *bs)
Definition: latmenc.c:100
int ff_stream_add_bitstream_filter(AVStream *st, const char *name, const char *args)
Add a bitstream filter to a stream.
Definition: utils.c:5188
void * priv_data
Format private data.
Definition: avformat.h:1377
int avpriv_copy_pce_data(PutBitContext *pb, GetBitContext *gb)
Definition: mpeg4audio.c:180
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:337
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:4080
AVCodecParameters * codecpar
Definition: avformat.h:1252
uint8_t buffer[0x1fff+MAX_EXTRADATA_SIZE+1024]
Definition: latmenc.c:40
int stream_index
Definition: avcodec.h:1659
This structure stores compressed data.
Definition: avcodec.h:1634
static int latm_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
Definition: latmenc.c:216
GLuint buffer
Definition: opengl_enc.c:102
bitstream writer API