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 "avformat.h"
28 
29 #define ADTS_HEADER_SIZE 7
30 
31 typedef struct {
36  int pce_size;
37  uint8_t pce_data[MAX_PCE_SIZE];
38 } ADTSContext;
39 
40 #define ADTS_MAX_FRAME_BYTES ((1 << 13) - 1)
41 
43 {
44  GetBitContext gb;
45  PutBitContext pb;
46  MPEG4AudioConfig m4ac;
47  int off;
48 
49  init_get_bits(&gb, buf, size * 8);
50  off = avpriv_mpeg4audio_get_config(&m4ac, buf, size * 8, 1);
51  if (off < 0)
52  return off;
53  skip_bits_long(&gb, off);
54  adts->objecttype = m4ac.object_type - 1;
55  adts->sample_rate_index = m4ac.sampling_index;
56  adts->channel_conf = m4ac.chan_config;
57 
58  if (adts->objecttype > 3U) {
59  av_log(s, AV_LOG_ERROR, "MPEG-4 AOT %d is not allowed in ADTS\n", adts->objecttype+1);
60  return -1;
61  }
62  if (adts->sample_rate_index == 15) {
63  av_log(s, AV_LOG_ERROR, "Escape sample rate index illegal in ADTS\n");
64  return -1;
65  }
66  if (get_bits(&gb, 1)) {
67  av_log(s, AV_LOG_ERROR, "960/120 MDCT window is not allowed in ADTS\n");
68  return -1;
69  }
70  if (get_bits(&gb, 1)) {
71  av_log(s, AV_LOG_ERROR, "Scalable configurations are not allowed in ADTS\n");
72  return -1;
73  }
74  if (get_bits(&gb, 1)) {
75  av_log(s, AV_LOG_ERROR, "Extension flag is not allowed in ADTS\n");
76  return -1;
77  }
78  if (!adts->channel_conf) {
79  init_put_bits(&pb, adts->pce_data, MAX_PCE_SIZE);
80 
81  put_bits(&pb, 3, 5); //ID_PCE
82  adts->pce_size = (avpriv_copy_pce_data(&pb, &gb) + 3) / 8;
83  flush_put_bits(&pb);
84  }
85 
86  adts->write_adts = 1;
87 
88  return 0;
89 }
90 
92 {
93  ADTSContext *adts = s->priv_data;
94  AVCodecContext *avc = s->streams[0]->codec;
95 
96  if (avc->extradata_size > 0 &&
97  adts_decode_extradata(s, adts, avc->extradata, avc->extradata_size) < 0)
98  return -1;
99 
100  return 0;
101 }
102 
104  uint8_t *buf, int size, int pce_size)
105 {
106  PutBitContext pb;
107 
108  unsigned full_frame_size = (unsigned)ADTS_HEADER_SIZE + size + pce_size;
109  if (full_frame_size > ADTS_MAX_FRAME_BYTES) {
110  av_log(NULL, AV_LOG_ERROR, "ADTS frame size too large: %u (max %d)\n",
111  full_frame_size, ADTS_MAX_FRAME_BYTES);
112  return AVERROR_INVALIDDATA;
113  }
114 
115  init_put_bits(&pb, buf, ADTS_HEADER_SIZE);
116 
117  /* adts_fixed_header */
118  put_bits(&pb, 12, 0xfff); /* syncword */
119  put_bits(&pb, 1, 0); /* ID */
120  put_bits(&pb, 2, 0); /* layer */
121  put_bits(&pb, 1, 1); /* protection_absent */
122  put_bits(&pb, 2, ctx->objecttype); /* profile_objecttype */
123  put_bits(&pb, 4, ctx->sample_rate_index);
124  put_bits(&pb, 1, 0); /* private_bit */
125  put_bits(&pb, 3, ctx->channel_conf); /* channel_configuration */
126  put_bits(&pb, 1, 0); /* original_copy */
127  put_bits(&pb, 1, 0); /* home */
128 
129  /* adts_variable_header */
130  put_bits(&pb, 1, 0); /* copyright_identification_bit */
131  put_bits(&pb, 1, 0); /* copyright_identification_start */
132  put_bits(&pb, 13, full_frame_size); /* aac_frame_length */
133  put_bits(&pb, 11, 0x7ff); /* adts_buffer_fullness */
134  put_bits(&pb, 2, 0); /* number_of_raw_data_blocks_in_frame */
135 
136  flush_put_bits(&pb);
137 
138  return 0;
139 }
140 
142 {
143  ADTSContext *adts = s->priv_data;
144  AVIOContext *pb = s->pb;
146 
147  if (!pkt->size)
148  return 0;
149  if (adts->write_adts) {
150  int err = adts_write_frame_header(adts, buf, pkt->size,
151  adts->pce_size);
152  if (err < 0)
153  return err;
154  avio_write(pb, buf, ADTS_HEADER_SIZE);
155  if (adts->pce_size) {
156  avio_write(pb, adts->pce_data, adts->pce_size);
157  adts->pce_size = 0;
158  }
159  }
160  avio_write(pb, pkt->data, pkt->size);
161 
162  return 0;
163 }
164 
166  .name = "adts",
167  .long_name = NULL_IF_CONFIG_SMALL("ADTS AAC (Advanced Audio Coding)"),
168  .mime_type = "audio/aac",
169  .extensions = "aac,adts",
170  .priv_data_size = sizeof(ADTSContext),
171  .audio_codec = AV_CODEC_ID_AAC,
172  .video_codec = AV_CODEC_ID_NONE,
175 };
uint8_t pce_data[MAX_PCE_SIZE]
Definition: adtsenc.c:37
const char * s
Definition: avisynth_c.h:668
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static int adts_decode_extradata(AVFormatContext *s, ADTSContext *adts, uint8_t *buf, int size)
Definition: adtsenc.c:42
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:240
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:198
static int write_packet(AVFormatContext *s, AVPacket *pkt)
int objecttype
Definition: adtsenc.c:33
#define ADTS_MAX_FRAME_BYTES
Definition: adtsenc.c:40
static int adts_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: adtsenc.c:141
Format I/O context.
Definition: avformat.h:944
uint8_t
static AVPacket pkt
Definition: demuxing.c:56
static int adts_write_header(AVFormatContext *s)
Definition: adtsenc.c:91
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
AVStream ** streams
Definition: avformat.h:992
uint8_t * data
#define ADTS_HEADER_SIZE
Definition: adtsenc.c:29
bitstream reader API header.
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:173
#define U(x)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
int channel_conf
Definition: adtsenc.c:35
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
int write_adts
Definition: adtsenc.c:32
static void put_bits(J2kEncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:160
external API header
int sample_rate_index
Definition: adtsenc.c:34
int size
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:662
const char * name
Definition: avformat.h:378
int pce_size
Definition: adtsenc.c:36
NULL
Definition: eval.c:55
AVIOContext * pb
I/O context.
Definition: avformat.h:977
main external API structure.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
void * buf
Definition: avisynth_c.h:594
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:379
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:81
static int adts_write_frame_header(ADTSContext *ctx, uint8_t *buf, int size, int pce_size)
Definition: adtsenc.c:103
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:54
#define MAX_PCE_SIZE
Maximum size of a PCE including the 3-bit ID_PCE.
Definition: mpeg4audio.h:104
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:81
void * priv_data
Format private data.
Definition: avformat.h:964
int avpriv_copy_pce_data(PutBitContext *pb, GetBitContext *gb)
Definition: mpeg4audio.c:161
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:470
This structure stores compressed data.
AVOutputFormat ff_adts_muxer
Definition: adtsenc.c:165
bitstream writer API