FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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;
54 
55  init_get_bits(&gb, buf, size * 8);
56  off = avpriv_mpeg4audio_get_config(&m4ac, buf, size * 8, 1);
57  if (off < 0)
58  return off;
59  skip_bits_long(&gb, off);
60  adts->objecttype = m4ac.object_type - 1;
61  adts->sample_rate_index = m4ac.sampling_index;
62  adts->channel_conf = m4ac.chan_config;
63 
64  if (adts->objecttype > 3U) {
65  av_log(s, AV_LOG_ERROR, "MPEG-4 AOT %d is not allowed in ADTS\n", adts->objecttype+1);
66  return AVERROR_INVALIDDATA;
67  }
68  if (adts->sample_rate_index == 15) {
69  av_log(s, AV_LOG_ERROR, "Escape sample rate index illegal in ADTS\n");
70  return AVERROR_INVALIDDATA;
71  }
72  if (get_bits(&gb, 1)) {
73  av_log(s, AV_LOG_ERROR, "960/120 MDCT window is not allowed in ADTS\n");
74  return AVERROR_INVALIDDATA;
75  }
76  if (get_bits(&gb, 1)) {
77  av_log(s, AV_LOG_ERROR, "Scalable configurations are not allowed in ADTS\n");
78  return AVERROR_INVALIDDATA;
79  }
80  if (get_bits(&gb, 1)) {
81  av_log(s, AV_LOG_ERROR, "Extension flag is not allowed in ADTS\n");
82  return AVERROR_INVALIDDATA;
83  }
84  if (!adts->channel_conf) {
85  init_put_bits(&pb, adts->pce_data, MAX_PCE_SIZE);
86 
87  put_bits(&pb, 3, 5); //ID_PCE
88  adts->pce_size = (avpriv_copy_pce_data(&pb, &gb) + 3) / 8;
89  flush_put_bits(&pb);
90  }
91 
92  adts->write_adts = 1;
93 
94  return 0;
95 }
96 
98 {
99  ADTSContext *adts = s->priv_data;
100  AVCodecParameters *par = s->streams[0]->codecpar;
101 
102  if (adts->id3v2tag)
104  if (par->extradata_size > 0)
105  return adts_decode_extradata(s, adts, par->extradata,
106  par->extradata_size);
107 
108  return 0;
109 }
110 
112  uint8_t *buf, int size, int pce_size)
113 {
114  PutBitContext pb;
115 
116  unsigned full_frame_size = (unsigned)ADTS_HEADER_SIZE + size + pce_size;
117  if (full_frame_size > ADTS_MAX_FRAME_BYTES) {
118  av_log(NULL, AV_LOG_ERROR, "ADTS frame size too large: %u (max %d)\n",
119  full_frame_size, ADTS_MAX_FRAME_BYTES);
120  return AVERROR_INVALIDDATA;
121  }
122 
123  init_put_bits(&pb, buf, ADTS_HEADER_SIZE);
124 
125  /* adts_fixed_header */
126  put_bits(&pb, 12, 0xfff); /* syncword */
127  put_bits(&pb, 1, 0); /* ID */
128  put_bits(&pb, 2, 0); /* layer */
129  put_bits(&pb, 1, 1); /* protection_absent */
130  put_bits(&pb, 2, ctx->objecttype); /* profile_objecttype */
131  put_bits(&pb, 4, ctx->sample_rate_index);
132  put_bits(&pb, 1, 0); /* private_bit */
133  put_bits(&pb, 3, ctx->channel_conf); /* channel_configuration */
134  put_bits(&pb, 1, 0); /* original_copy */
135  put_bits(&pb, 1, 0); /* home */
136 
137  /* adts_variable_header */
138  put_bits(&pb, 1, 0); /* copyright_identification_bit */
139  put_bits(&pb, 1, 0); /* copyright_identification_start */
140  put_bits(&pb, 13, full_frame_size); /* aac_frame_length */
141  put_bits(&pb, 11, 0x7ff); /* adts_buffer_fullness */
142  put_bits(&pb, 2, 0); /* number_of_raw_data_blocks_in_frame */
143 
144  flush_put_bits(&pb);
145 
146  return 0;
147 }
148 
150 {
151  ADTSContext *adts = s->priv_data;
152  AVCodecParameters *par = s->streams[0]->codecpar;
153  AVIOContext *pb = s->pb;
155 
156  if (!pkt->size)
157  return 0;
158  if (!par->extradata_size) {
159  uint8_t *side_data;
160  int side_data_size = 0, ret;
161 
163  &side_data_size);
164  if (side_data_size) {
165  ret = adts_decode_extradata(s, adts, side_data, side_data_size);
166  if (ret < 0)
167  return ret;
168  ret = ff_alloc_extradata(par, side_data_size);
169  if (ret < 0)
170  return ret;
171  memcpy(par->extradata, side_data, side_data_size);
172  }
173  }
174  if (adts->write_adts) {
175  int err = adts_write_frame_header(adts, buf, pkt->size,
176  adts->pce_size);
177  if (err < 0)
178  return err;
179  avio_write(pb, buf, ADTS_HEADER_SIZE);
180  if (adts->pce_size) {
181  avio_write(pb, adts->pce_data, adts->pce_size);
182  adts->pce_size = 0;
183  }
184  }
185  avio_write(pb, pkt->data, pkt->size);
186 
187  return 0;
188 }
189 
191 {
192  ADTSContext *adts = s->priv_data;
193 
194  if (adts->apetag)
195  ff_ape_write_tag(s);
196 
197  return 0;
198 }
199 
200 #define ENC AV_OPT_FLAG_ENCODING_PARAM
201 #define OFFSET(obj) offsetof(ADTSContext, obj)
202 static const AVOption options[] = {
203  { "write_id3v2", "Enable ID3v2 tag writing", OFFSET(id3v2tag), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, ENC},
204  { "write_apetag", "Enable APE tag writing", OFFSET(apetag), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, ENC},
205  { NULL },
206 };
207 
208 static const AVClass adts_muxer_class = {
209  .class_name = "ADTS muxer",
210  .item_name = av_default_item_name,
211  .option = options,
212  .version = LIBAVUTIL_VERSION_INT,
213 };
214 
216  .name = "adts",
217  .long_name = NULL_IF_CONFIG_SMALL("ADTS AAC (Advanced Audio Coding)"),
218  .mime_type = "audio/aac",
219  .extensions = "aac,adts",
220  .priv_data_size = sizeof(ADTSContext),
221  .audio_codec = AV_CODEC_ID_AAC,
222  .video_codec = AV_CODEC_ID_NONE,
226  .priv_class = &adts_muxer_class,
228 };
uint8_t pce_data[MAX_PCE_SIZE]
Definition: adtsenc.c:43
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:671
#define NULL
Definition: coverity.c:32
int id3v2tag
Definition: adtsenc.c:42
const char * s
Definition: avisynth_c.h:768
Bytestream IO Context.
Definition: avio.h:161
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
AVOption.
Definition: opt.h:246
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:350
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:206
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:261
#define LIBAVUTIL_VERSION_INT
Definition: version.h:86
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:204
int size
Definition: avcodec.h:1680
#define ID3v2_DEFAULT_MAGIC
Default magic bytes for ID3v2 header: "ID3".
Definition: id3v2.h:35
static int adts_decode_extradata(AVFormatContext *s, ADTSContext *adts, const uint8_t *buf, int size)
Definition: adtsenc.c:48
int objecttype
Definition: adtsenc.c:37
#define ADTS_MAX_FRAME_BYTES
Definition: adtsenc.c:46
static AVPacket pkt
This struct describes the properties of an encoded stream.
Definition: avcodec.h:4144
static int adts_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: adtsenc.c:149
Format I/O context.
Definition: avformat.h:1349
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
uint8_t
static const AVOption options[]
Definition: adtsenc.c:202
AVOptions.
static int adts_write_header(AVFormatContext *s)
Definition: adtsenc.c:97
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1417
uint8_t * data
Definition: avcodec.h:1679
#define ADTS_HEADER_SIZE
Definition: adtsenc.c:32
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:216
#define av_log(a,...)
#define U(x)
Definition: vp56_arith.h:37
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
av_default_item_name
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
Definition: avpacket.c:350
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
int channel_conf
Definition: adtsenc.c:39
int write_adts
Definition: adtsenc.c:36
int sample_rate_index
Definition: adtsenc.c:38
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:4170
#define ENC
Definition: adtsenc.c:200
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:3216
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:94
const char * name
Definition: avformat.h:524
AVFormatContext * ctx
Definition: movenc.c:48
int pce_size
Definition: adtsenc.c:40
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:486
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition: avcodec.h:1420
Libavcodec external API header.
AVIOContext * pb
I/O context.
Definition: avformat.h:1391
void * buf
Definition: avisynth_c.h:690
int ff_ape_write_tag(AVFormatContext *s)
Write an APE tag into a file.
Definition: apetag.c:185
Describe the class of an AVClass context structure.
Definition: log.h:67
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:425
int apetag
Definition: adtsenc.c:41
static const AVClass adts_muxer_class
Definition: adtsenc.c:208
Main libavformat public API header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
static int adts_write_frame_header(ADTSContext *ctx, uint8_t *buf, int size, int pce_size)
Definition: adtsenc.c:111
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
#define MAX_PCE_SIZE
Maximum size of a PCE including the 3-bit ID_PCE.
Definition: mpeg4audio.h:115
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
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:4166
#define OFFSET(obj)
Definition: adtsenc.c:201
AVCodecParameters * codecpar
Definition: avformat.h:1252
This structure stores compressed data.
Definition: avcodec.h:1656
AVOutputFormat ff_adts_muxer
Definition: adtsenc.c:215
static int adts_write_trailer(AVFormatContext *s)
Definition: adtsenc.c:190
bitstream writer API