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