FFmpeg
adtsenc.c
Go to the documentation of this file.
1 /*
2  * ADTS muxer.
3  * Copyright (c) 2006 Baptiste Coudurier <baptiste.coudurier@smartjog.com>
4  * Mans Rullgard <mans@mansr.com>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include "libavcodec/get_bits.h"
24 #include "libavcodec/put_bits.h"
25 #include "libavcodec/codec_id.h"
26 #include "libavcodec/codec_par.h"
27 #include "libavcodec/packet.h"
28 #include "libavcodec/mpeg4audio.h"
30 #include "libavutil/opt.h"
31 #include "avformat.h"
32 #include "apetag.h"
33 #include "id3v2.h"
34 #include "mux.h"
35 
36 #define ADTS_HEADER_SIZE 7
37 
38 typedef struct ADTSContext {
39  AVClass *class;
44  int pce_size;
45  int apetag;
46  int id3v2tag;
47  int mpeg_id;
49 } ADTSContext;
50 
51 #define ADTS_MAX_FRAME_BYTES ((1 << 14) - 1)
52 
53 static int adts_decode_extradata(AVFormatContext *s, ADTSContext *adts, const uint8_t *buf, int size)
54 {
55  GetBitContext gb;
56  PutBitContext pb;
57  MPEG4AudioConfig m4ac;
58  int off, ret;
59 
60  ret = init_get_bits8(&gb, buf, size);
61  if (ret < 0)
62  return ret;
63  off = avpriv_mpeg4audio_get_config2(&m4ac, buf, size, 1, s);
64  if (off < 0)
65  return off;
66  skip_bits_long(&gb, off);
67  adts->objecttype = m4ac.object_type - 1;
68  adts->sample_rate_index = m4ac.sampling_index;
69  adts->channel_conf = m4ac.chan_config;
70 
71  if (adts->objecttype > 3U) {
72  av_log(s, AV_LOG_ERROR, "MPEG-4 AOT %d is not allowed in ADTS\n", adts->objecttype+1);
73  return AVERROR_INVALIDDATA;
74  }
75  if (adts->sample_rate_index == 15) {
76  av_log(s, AV_LOG_ERROR, "Escape sample rate index illegal in ADTS\n");
77  return AVERROR_INVALIDDATA;
78  }
79  if (get_bits(&gb, 1)) {
80  av_log(s, AV_LOG_ERROR, "960/120 MDCT window is not allowed in ADTS\n");
81  return AVERROR_INVALIDDATA;
82  }
83  if (get_bits(&gb, 1)) {
84  av_log(s, AV_LOG_ERROR, "Scalable configurations are not allowed in ADTS\n");
85  return AVERROR_INVALIDDATA;
86  }
87  if (get_bits(&gb, 1)) {
88  av_log(s, AV_LOG_ERROR, "Extension flag is not allowed in ADTS\n");
89  return AVERROR_INVALIDDATA;
90  }
91  if (!adts->channel_conf) {
92  init_put_bits(&pb, adts->pce_data, MAX_PCE_SIZE);
93 
94  put_bits(&pb, 3, 5); //ID_PCE
95  adts->pce_size = (ff_copy_pce_data(&pb, &gb) + 3) / 8;
96  flush_put_bits(&pb);
97  }
98 
99  adts->write_adts = 1;
100 
101  return 0;
102 }
103 
105 {
106  ADTSContext *adts = s->priv_data;
107  AVCodecParameters *par = s->streams[0]->codecpar;
108 
109  if (par->codec_id != AV_CODEC_ID_AAC) {
110  av_log(s, AV_LOG_ERROR, "Only AAC streams can be muxed by the ADTS muxer\n");
111  return AVERROR(EINVAL);
112  }
113  if (par->extradata_size > 0)
114  return adts_decode_extradata(s, adts, par->extradata,
115  par->extradata_size);
116 
117  return 0;
118 }
119 
121 {
122  ADTSContext *adts = s->priv_data;
123 
124  if (adts->id3v2tag)
126 
127  return 0;
128 }
129 
131  uint8_t *buf, int size, int pce_size)
132 {
133  PutBitContext pb;
134 
135  unsigned full_frame_size = (unsigned)ADTS_HEADER_SIZE + size + pce_size;
136  if (full_frame_size > ADTS_MAX_FRAME_BYTES) {
137  av_log(NULL, AV_LOG_ERROR, "ADTS frame size too large: %u (max %d)\n",
138  full_frame_size, ADTS_MAX_FRAME_BYTES);
139  return AVERROR_INVALIDDATA;
140  }
141 
142  init_put_bits(&pb, buf, ADTS_HEADER_SIZE);
143 
144  /* adts_fixed_header */
145  put_bits(&pb, 12, 0xfff); /* syncword */
146  put_bits(&pb, 1, ctx->mpeg_id); /* ID */
147  put_bits(&pb, 2, 0); /* layer */
148  put_bits(&pb, 1, 1); /* protection_absent */
149  put_bits(&pb, 2, ctx->objecttype); /* profile_objecttype */
150  put_bits(&pb, 4, ctx->sample_rate_index);
151  put_bits(&pb, 1, 0); /* private_bit */
152  put_bits(&pb, 3, ctx->channel_conf); /* channel_configuration */
153  put_bits(&pb, 1, 0); /* original_copy */
154  put_bits(&pb, 1, 0); /* home */
155 
156  /* adts_variable_header */
157  put_bits(&pb, 1, 0); /* copyright_identification_bit */
158  put_bits(&pb, 1, 0); /* copyright_identification_start */
159  put_bits(&pb, 13, full_frame_size); /* aac_frame_length */
160  put_bits(&pb, 11, 0x7ff); /* adts_buffer_fullness */
161  put_bits(&pb, 2, 0); /* number_of_raw_data_blocks_in_frame */
162 
163  flush_put_bits(&pb);
164 
165  return 0;
166 }
167 
169 {
170  ADTSContext *adts = s->priv_data;
171  AVCodecParameters *par = s->streams[0]->codecpar;
172  AVIOContext *pb = s->pb;
173  uint8_t buf[ADTS_HEADER_SIZE];
174 
175  if (!pkt->size)
176  return 0;
177  if (!par->extradata_size) {
178  uint8_t *side_data;
179  size_t side_data_size;
180  int ret;
181 
183  &side_data_size);
184  if (side_data_size) {
185  ret = adts_decode_extradata(s, adts, side_data, side_data_size);
186  if (ret < 0)
187  return ret;
188  ret = ff_alloc_extradata(par, side_data_size);
189  if (ret < 0)
190  return ret;
191  memcpy(par->extradata, side_data, side_data_size);
192  }
193  }
194  if (adts->write_adts) {
195  int err = adts_write_frame_header(adts, buf, pkt->size,
196  adts->pce_size);
197  if (err < 0)
198  return err;
199  avio_write(pb, buf, ADTS_HEADER_SIZE);
200  if (adts->pce_size) {
201  avio_write(pb, adts->pce_data, adts->pce_size);
202  adts->pce_size = 0;
203  }
204  }
205  avio_write(pb, pkt->data, pkt->size);
206 
207  return 0;
208 }
209 
211 {
212  ADTSContext *adts = s->priv_data;
213 
214  if (adts->apetag)
216 
217  return 0;
218 }
219 
220 #define ENC AV_OPT_FLAG_ENCODING_PARAM
221 #define OFFSET(obj) offsetof(ADTSContext, obj)
222 static const AVOption options[] = {
223  { "write_id3v2", "Enable ID3v2 tag writing", OFFSET(id3v2tag), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, ENC},
224  { "write_apetag", "Enable APE tag writing", OFFSET(apetag), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, ENC},
225  { "write_mpeg2", "Set MPEG version to MPEG-2", OFFSET(mpeg_id), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, ENC},
226  { NULL },
227 };
228 
229 static const AVClass adts_muxer_class = {
230  .class_name = "ADTS muxer",
231  .item_name = av_default_item_name,
232  .option = options,
233  .version = LIBAVUTIL_VERSION_INT,
234 };
235 
237  .p.name = "adts",
238  .p.long_name = NULL_IF_CONFIG_SMALL("ADTS AAC (Advanced Audio Coding)"),
239  .p.mime_type = "audio/aac",
240  .p.extensions = "aac,adts",
241  .priv_data_size = sizeof(ADTSContext),
242  .p.audio_codec = AV_CODEC_ID_AAC,
243  .p.video_codec = AV_CODEC_ID_NONE,
244  .init = adts_init,
245  .write_header = adts_write_header,
246  .write_packet = adts_write_packet,
247  .write_trailer = adts_write_trailer,
248  .p.priv_class = &adts_muxer_class,
249  .p.flags = AVFMT_NOTIMESTAMPS,
250 };
skip_bits_long
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:268
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:76
adts_write_trailer
static int adts_write_trailer(AVFormatContext *s)
Definition: adtsenc.c:210
AVOutputFormat::name
const char * name
Definition: avformat.h:508
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
adts_init
static int adts_init(AVFormatContext *s)
Definition: adtsenc.c:104
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:54
ADTSContext::apetag
int apetag
Definition: adtsenc.c:45
AVFMT_NOTIMESTAMPS
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:479
adts_decode_extradata
static int adts_decode_extradata(AVFormatContext *s, ADTSContext *adts, const uint8_t *buf, int size)
Definition: adtsenc.c:53
init_put_bits
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:62
id3v2.h
apetag.h
put_bits
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:221
AVPacket::data
uint8_t * data
Definition: packet.h:374
AVOption
AVOption.
Definition: opt.h:251
MAX_PCE_SIZE
#define MAX_PCE_SIZE
Maximum size of a PCE including the 3-bit ID_PCE.
Definition: mpeg4audio.h:117
options
static const AVOption options[]
Definition: adtsenc.c:222
FFOutputFormat::p
AVOutputFormat p
The public AVOutputFormat.
Definition: mux.h:34
ADTS_MAX_FRAME_BYTES
#define ADTS_MAX_FRAME_BYTES
Definition: adtsenc.c:51
MPEG4AudioConfig
Definition: mpeg4audio.h:29
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:325
mpeg4audio.h
ADTS_HEADER_SIZE
#define ADTS_HEADER_SIZE
Definition: adtsenc.c:36
adts_write_packet
static int adts_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: adtsenc.c:168
GetBitContext
Definition: get_bits.h:107
adts_write_header
static int adts_write_header(AVFormatContext *s)
Definition: adtsenc.c:120
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:524
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:256
ADTSContext::id3v2tag
int id3v2tag
Definition: adtsenc.c:46
ctx
AVFormatContext * ctx
Definition: movenc.c:48
ff_copy_pce_data
static int ff_copy_pce_data(PutBitContext *pb, GetBitContext *gb)
Definition: mpeg4audio_copy_pce.h:38
get_bits.h
ff_adts_muxer
const FFOutputFormat ff_adts_muxer
Definition: adtsenc.c:236
PutBitContext
Definition: put_bits.h:50
ADTSContext::pce_size
int pce_size
Definition: adtsenc.c:44
ff_ape_write_tag
int ff_ape_write_tag(AVFormatContext *s)
Write an APE tag into a file.
Definition: apetag.c:176
ADTSContext::objecttype
int objecttype
Definition: adtsenc.c:41
AVFormatContext
Format I/O context.
Definition: avformat.h:1104
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:237
MPEG4AudioConfig::sampling_index
int sampling_index
Definition: mpeg4audio.h:31
FFOutputFormat
Definition: mux.h:30
ENC
#define ENC
Definition: adtsenc.c:220
AVCodecParameters::extradata_size
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:80
AV_CODEC_ID_AAC
@ AV_CODEC_ID_AAC
Definition: codec_id.h:440
AVIOContext
Bytestream IO Context.
Definition: avio.h:166
AVPacket::size
int size
Definition: packet.h:375
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:115
ADTSContext::write_adts
int write_adts
Definition: adtsenc.c:40
size
int size
Definition: twinvq_data.h:10344
ID3v2_DEFAULT_MAGIC
#define ID3v2_DEFAULT_MAGIC
Default magic bytes for ID3v2 header: "ID3".
Definition: id3v2.h:35
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:222
ADTSContext::channel_conf
int channel_conf
Definition: adtsenc.c:43
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:50
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:251
ADTSContext::sample_rate_index
int sample_rate_index
Definition: adtsenc.c:42
MPEG4AudioConfig::chan_config
int chan_config
Definition: mpeg4audio.h:33
adts_muxer_class
static const AVClass adts_muxer_class
Definition: adtsenc.c:229
ret
ret
Definition: filter_design.txt:187
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:30
U
#define U(x)
Definition: vpx_arith.h:37
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:56
ADTSContext::pce_data
uint8_t pce_data[MAX_PCE_SIZE]
Definition: adtsenc.c:48
adts_write_frame_header
static int adts_write_frame_header(ADTSContext *ctx, uint8_t *buf, int size, int pce_size)
Definition: adtsenc.c:130
flush_put_bits
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:143
codec_par.h
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:62
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:244
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
put_bits.h
OFFSET
#define OFFSET(obj)
Definition: adtsenc.c:221
ff_id3v2_write_simple
int ff_id3v2_write_simple(struct AVFormatContext *s, int id3v2_version, const char *magic)
Write an ID3v2 tag containing all global metadata from s.
Definition: id3v2enc.c:445
mpeg4audio_copy_pce.h
ADTSContext::mpeg_id
int mpeg_id
Definition: adtsenc.c:47
ADTSContext
Definition: adtsenc.c:38
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:238
mux.h