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/avcodec.h"
26 #include "libavcodec/mpeg4audio.h"
27 #include "libavutil/opt.h"
28 #include "avformat.h"
29 #include "apetag.h"
30 #include "id3v2.h"
31 
32 #define ADTS_HEADER_SIZE 7
33 
34 typedef struct ADTSContext {
35  AVClass *class;
40  int pce_size;
41  int apetag;
42  int id3v2tag;
44 } ADTSContext;
45 
46 #define ADTS_MAX_FRAME_BYTES ((1 << 13) - 1)
47 
49 {
50  GetBitContext gb;
51  PutBitContext pb;
52  MPEG4AudioConfig m4ac;
53  int off, ret;
54 
55  ret = init_get_bits8(&gb, buf, size);
56  if (ret < 0)
57  return ret;
58  off = avpriv_mpeg4audio_get_config(&m4ac, buf, size * 8, 1);
59  if (off < 0)
60  return off;
61  skip_bits_long(&gb, off);
62  adts->objecttype = m4ac.object_type - 1;
63  adts->sample_rate_index = m4ac.sampling_index;
64  adts->channel_conf = m4ac.chan_config;
65 
66  if (adts->objecttype > 3U) {
67  av_log(s, AV_LOG_ERROR, "MPEG-4 AOT %d is not allowed in ADTS\n", adts->objecttype+1);
68  return AVERROR_INVALIDDATA;
69  }
70  if (adts->sample_rate_index == 15) {
71  av_log(s, AV_LOG_ERROR, "Escape sample rate index illegal in ADTS\n");
72  return AVERROR_INVALIDDATA;
73  }
74  if (get_bits(&gb, 1)) {
75  av_log(s, AV_LOG_ERROR, "960/120 MDCT window is not allowed in ADTS\n");
76  return AVERROR_INVALIDDATA;
77  }
78  if (get_bits(&gb, 1)) {
79  av_log(s, AV_LOG_ERROR, "Scalable configurations are not allowed in ADTS\n");
80  return AVERROR_INVALIDDATA;
81  }
82  if (get_bits(&gb, 1)) {
83  av_log(s, AV_LOG_ERROR, "Extension flag is not allowed in ADTS\n");
84  return AVERROR_INVALIDDATA;
85  }
86  if (!adts->channel_conf) {
87  init_put_bits(&pb, adts->pce_data, MAX_PCE_SIZE);
88 
89  put_bits(&pb, 3, 5); //ID_PCE
90  adts->pce_size = (ff_copy_pce_data(&pb, &gb) + 3) / 8;
91  flush_put_bits(&pb);
92  }
93 
94  adts->write_adts = 1;
95 
96  return 0;
97 }
98 
100 {
101  ADTSContext *adts = s->priv_data;
102  AVCodecParameters *par = s->streams[0]->codecpar;
103 
104  if (par->codec_id != AV_CODEC_ID_AAC) {
105  av_log(s, AV_LOG_ERROR, "Only AAC streams can be muxed by the ADTS muxer\n");
106  return AVERROR(EINVAL);
107  }
108  if (par->extradata_size > 0)
109  return adts_decode_extradata(s, adts, par->extradata,
110  par->extradata_size);
111 
112  return 0;
113 }
114 
116 {
117  ADTSContext *adts = s->priv_data;
118 
119  if (adts->id3v2tag)
121 
122  return 0;
123 }
124 
126  uint8_t *buf, int size, int pce_size)
127 {
128  PutBitContext pb;
129 
130  unsigned full_frame_size = (unsigned)ADTS_HEADER_SIZE + size + pce_size;
131  if (full_frame_size > ADTS_MAX_FRAME_BYTES) {
132  av_log(NULL, AV_LOG_ERROR, "ADTS frame size too large: %u (max %d)\n",
133  full_frame_size, ADTS_MAX_FRAME_BYTES);
134  return AVERROR_INVALIDDATA;
135  }
136 
138 
139  /* adts_fixed_header */
140  put_bits(&pb, 12, 0xfff); /* syncword */
141  put_bits(&pb, 1, 0); /* ID */
142  put_bits(&pb, 2, 0); /* layer */
143  put_bits(&pb, 1, 1); /* protection_absent */
144  put_bits(&pb, 2, ctx->objecttype); /* profile_objecttype */
145  put_bits(&pb, 4, ctx->sample_rate_index);
146  put_bits(&pb, 1, 0); /* private_bit */
147  put_bits(&pb, 3, ctx->channel_conf); /* channel_configuration */
148  put_bits(&pb, 1, 0); /* original_copy */
149  put_bits(&pb, 1, 0); /* home */
150 
151  /* adts_variable_header */
152  put_bits(&pb, 1, 0); /* copyright_identification_bit */
153  put_bits(&pb, 1, 0); /* copyright_identification_start */
154  put_bits(&pb, 13, full_frame_size); /* aac_frame_length */
155  put_bits(&pb, 11, 0x7ff); /* adts_buffer_fullness */
156  put_bits(&pb, 2, 0); /* number_of_raw_data_blocks_in_frame */
157 
158  flush_put_bits(&pb);
159 
160  return 0;
161 }
162 
164 {
165  ADTSContext *adts = s->priv_data;
166  AVCodecParameters *par = s->streams[0]->codecpar;
167  AVIOContext *pb = s->pb;
169 
170  if (!pkt->size)
171  return 0;
172  if (!par->extradata_size) {
173  uint8_t *side_data;
174  int side_data_size = 0, ret;
175 
177  &side_data_size);
178  if (side_data_size) {
179  ret = adts_decode_extradata(s, adts, side_data, side_data_size);
180  if (ret < 0)
181  return ret;
182  ret = ff_alloc_extradata(par, side_data_size);
183  if (ret < 0)
184  return ret;
185  memcpy(par->extradata, side_data, side_data_size);
186  }
187  }
188  if (adts->write_adts) {
189  int err = adts_write_frame_header(adts, buf, pkt->size,
190  adts->pce_size);
191  if (err < 0)
192  return err;
194  if (adts->pce_size) {
195  avio_write(pb, adts->pce_data, adts->pce_size);
196  adts->pce_size = 0;
197  }
198  }
199  avio_write(pb, pkt->data, pkt->size);
200 
201  return 0;
202 }
203 
205 {
206  ADTSContext *adts = s->priv_data;
207 
208  if (adts->apetag)
210 
211  return 0;
212 }
213 
214 #define ENC AV_OPT_FLAG_ENCODING_PARAM
215 #define OFFSET(obj) offsetof(ADTSContext, obj)
216 static const AVOption options[] = {
217  { "write_id3v2", "Enable ID3v2 tag writing", OFFSET(id3v2tag), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, ENC},
218  { "write_apetag", "Enable APE tag writing", OFFSET(apetag), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, ENC},
219  { NULL },
220 };
221 
222 static const AVClass adts_muxer_class = {
223  .class_name = "ADTS muxer",
224  .item_name = av_default_item_name,
225  .option = options,
226  .version = LIBAVUTIL_VERSION_INT,
227 };
228 
230  .name = "adts",
231  .long_name = NULL_IF_CONFIG_SMALL("ADTS AAC (Advanced Audio Coding)"),
232  .mime_type = "audio/aac",
233  .extensions = "aac,adts",
234  .priv_data_size = sizeof(ADTSContext),
235  .audio_codec = AV_CODEC_ID_AAC,
236  .video_codec = AV_CODEC_ID_NONE,
237  .init = adts_init,
241  .priv_class = &adts_muxer_class,
243 };
skip_bits_long
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:291
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3971
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
ff_adts_muxer
AVOutputFormat ff_adts_muxer
Definition: adtsenc.c:229
adts_write_trailer
static int adts_write_trailer(AVFormatContext *s)
Definition: adtsenc.c:204
AVOutputFormat::name
const char * name
Definition: avformat.h:496
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:99
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3949
ADTSContext::apetag
int apetag
Definition: adtsenc.c:41
AVFMT_NOTIMESTAMPS
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:467
adts_decode_extradata
static int adts_decode_extradata(AVFormatContext *s, ADTSContext *adts, const uint8_t *buf, int size)
Definition: adtsenc.c:48
init_put_bits
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
id3v2.h
apetag.h
put_bits
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:208
AVPacket::data
uint8_t * data
Definition: avcodec.h:1477
AVOption
AVOption.
Definition: opt.h:246
MAX_PCE_SIZE
#define MAX_PCE_SIZE
Maximum size of a PCE including the 3-bit ID_PCE.
Definition: mpeg4audio.h:119
options
static const AVOption options[]
Definition: adtsenc.c:216
ADTS_MAX_FRAME_BYTES
#define ADTS_MAX_FRAME_BYTES
Definition: adtsenc.c:46
MPEG4AudioConfig
Definition: mpeg4audio.h:33
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
mpeg4audio.h
U
#define U(x)
Definition: vp56_arith.h:37
ADTS_HEADER_SIZE
#define ADTS_HEADER_SIZE
Definition: adtsenc.c:32
adts_write_packet
static int adts_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: adtsenc.c:163
GetBitContext
Definition: get_bits.h:61
adts_write_header
static int adts_write_header(AVFormatContext *s)
Definition: adtsenc.c:115
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
buf
void * buf
Definition: avisynth_c.h:766
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
s
#define s(width, name)
Definition: cbs_vp9.c:257
ADTSContext::id3v2tag
int id3v2tag
Definition: adtsenc.c:42
ctx
AVFormatContext * ctx
Definition: movenc.c:48
get_bits.h
PutBitContext
Definition: put_bits.h:35
ADTSContext::pce_size
int pce_size
Definition: adtsenc.c:40
av_packet_get_side_data
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
Definition: avpacket.c:350
ff_ape_write_tag
int ff_ape_write_tag(AVFormatContext *s)
Write an APE tag into a file.
Definition: apetag.c:185
ADTSContext::objecttype
int objecttype
Definition: adtsenc.c:37
AVFormatContext
Format I/O context.
Definition: avformat.h:1342
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
NULL
#define NULL
Definition: coverity.c:32
write_trailer
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:94
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
MPEG4AudioConfig::sampling_index
int sampling_index
Definition: mpeg4audio.h:35
ff_copy_pce_data
static int ff_copy_pce_data(PutBitContext *pb, GetBitContext *gb)
Definition: mpeg4audio.h:131
ENC
#define ENC
Definition: adtsenc.c:214
AVCodecParameters::extradata_size
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3975
AV_CODEC_ID_AAC
@ AV_CODEC_ID_AAC
Definition: avcodec.h:566
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
AVPacket::size
int size
Definition: avcodec.h:1478
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:188
ADTSContext::write_adts
int write_adts
Definition: adtsenc.c:36
size
int size
Definition: twinvq_data.h:11134
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:218
write_packet
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:690
ADTSContext::channel_conf
int channel_conf
Definition: adtsenc.c:39
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: avcodec.h:216
AVOutputFormat
Definition: avformat.h:495
ADTSContext::sample_rate_index
int sample_rate_index
Definition: adtsenc.c:38
uint8_t
uint8_t
Definition: audio_convert.c:194
MPEG4AudioConfig::chan_config
int chan_config
Definition: mpeg4audio.h:37
adts_muxer_class
static const AVClass adts_muxer_class
Definition: adtsenc.c:222
avcodec.h
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:72
avformat.h
MPEG4AudioConfig::object_type
int object_type
Definition: mpeg4audio.h:34
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
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: avcodec.h:1199
ADTSContext::pce_data
uint8_t pce_data[MAX_PCE_SIZE]
Definition: adtsenc.c:43
adts_write_frame_header
static int adts_write_frame_header(ADTSContext *ctx, uint8_t *buf, int size, int pce_size)
Definition: adtsenc.c:125
flush_put_bits
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3957
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1454
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:240
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:565
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
write_header
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:337
avpriv_mpeg4audio_get_config
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:159
put_bits.h
OFFSET
#define OFFSET(obj)
Definition: adtsenc.c:215
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:449
ADTSContext
Definition: adtsenc.c:34
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:3309