00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "libavcodec/get_bits.h"
00024 #include "libavcodec/put_bits.h"
00025 #include "libavcodec/avcodec.h"
00026 #include "libavcodec/mpeg4audio.h"
00027 #include "avformat.h"
00028
00029 #define ADTS_HEADER_SIZE 7
00030
00031 typedef struct {
00032 int write_adts;
00033 int objecttype;
00034 int sample_rate_index;
00035 int channel_conf;
00036 int pce_size;
00037 uint8_t pce_data[MAX_PCE_SIZE];
00038 } ADTSContext;
00039
00040 #define ADTS_MAX_FRAME_BYTES ((1 << 13) - 1)
00041
00042 static int adts_decode_extradata(AVFormatContext *s, ADTSContext *adts, uint8_t *buf, int size)
00043 {
00044 GetBitContext gb;
00045 PutBitContext pb;
00046 MPEG4AudioConfig m4ac;
00047 int off;
00048
00049 init_get_bits(&gb, buf, size * 8);
00050 off = avpriv_mpeg4audio_get_config(&m4ac, buf, size * 8, 1);
00051 if (off < 0)
00052 return off;
00053 skip_bits_long(&gb, off);
00054 adts->objecttype = m4ac.object_type - 1;
00055 adts->sample_rate_index = m4ac.sampling_index;
00056 adts->channel_conf = m4ac.chan_config;
00057
00058 if (adts->objecttype > 3U) {
00059 av_log(s, AV_LOG_ERROR, "MPEG-4 AOT %d is not allowed in ADTS\n", adts->objecttype+1);
00060 return -1;
00061 }
00062 if (adts->sample_rate_index == 15) {
00063 av_log(s, AV_LOG_ERROR, "Escape sample rate index illegal in ADTS\n");
00064 return -1;
00065 }
00066 if (get_bits(&gb, 1)) {
00067 av_log(s, AV_LOG_ERROR, "960/120 MDCT window is not allowed in ADTS\n");
00068 return -1;
00069 }
00070 if (get_bits(&gb, 1)) {
00071 av_log(s, AV_LOG_ERROR, "Scalable configurations are not allowed in ADTS\n");
00072 return -1;
00073 }
00074 if (get_bits(&gb, 1)) {
00075 av_log(s, AV_LOG_ERROR, "Extension flag is not allowed in ADTS\n");
00076 return -1;
00077 }
00078 if (!adts->channel_conf) {
00079 init_put_bits(&pb, adts->pce_data, MAX_PCE_SIZE);
00080
00081 put_bits(&pb, 3, 5);
00082 adts->pce_size = (avpriv_copy_pce_data(&pb, &gb) + 3) / 8;
00083 flush_put_bits(&pb);
00084 }
00085
00086 adts->write_adts = 1;
00087
00088 return 0;
00089 }
00090
00091 static int adts_write_header(AVFormatContext *s)
00092 {
00093 ADTSContext *adts = s->priv_data;
00094 AVCodecContext *avc = s->streams[0]->codec;
00095
00096 if (avc->extradata_size > 0 &&
00097 adts_decode_extradata(s, adts, avc->extradata, avc->extradata_size) < 0)
00098 return -1;
00099
00100 return 0;
00101 }
00102
00103 static int adts_write_frame_header(ADTSContext *ctx,
00104 uint8_t *buf, int size, int pce_size)
00105 {
00106 PutBitContext pb;
00107
00108 unsigned full_frame_size = (unsigned)ADTS_HEADER_SIZE + size + pce_size;
00109 if (full_frame_size > ADTS_MAX_FRAME_BYTES) {
00110 av_log(NULL, AV_LOG_ERROR, "ADTS frame size too large: %u (max %d)\n",
00111 full_frame_size, ADTS_MAX_FRAME_BYTES);
00112 return AVERROR_INVALIDDATA;
00113 }
00114
00115 init_put_bits(&pb, buf, ADTS_HEADER_SIZE);
00116
00117
00118 put_bits(&pb, 12, 0xfff);
00119 put_bits(&pb, 1, 0);
00120 put_bits(&pb, 2, 0);
00121 put_bits(&pb, 1, 1);
00122 put_bits(&pb, 2, ctx->objecttype);
00123 put_bits(&pb, 4, ctx->sample_rate_index);
00124 put_bits(&pb, 1, 0);
00125 put_bits(&pb, 3, ctx->channel_conf);
00126 put_bits(&pb, 1, 0);
00127 put_bits(&pb, 1, 0);
00128
00129
00130 put_bits(&pb, 1, 0);
00131 put_bits(&pb, 1, 0);
00132 put_bits(&pb, 13, full_frame_size);
00133 put_bits(&pb, 11, 0x7ff);
00134 put_bits(&pb, 2, 0);
00135
00136 flush_put_bits(&pb);
00137
00138 return 0;
00139 }
00140
00141 static int adts_write_packet(AVFormatContext *s, AVPacket *pkt)
00142 {
00143 ADTSContext *adts = s->priv_data;
00144 AVIOContext *pb = s->pb;
00145 uint8_t buf[ADTS_HEADER_SIZE];
00146
00147 if (!pkt->size)
00148 return 0;
00149 if (adts->write_adts) {
00150 int err = adts_write_frame_header(adts, buf, pkt->size,
00151 adts->pce_size);
00152 if (err < 0)
00153 return err;
00154 avio_write(pb, buf, ADTS_HEADER_SIZE);
00155 if (adts->pce_size) {
00156 avio_write(pb, adts->pce_data, adts->pce_size);
00157 adts->pce_size = 0;
00158 }
00159 }
00160 avio_write(pb, pkt->data, pkt->size);
00161 avio_flush(pb);
00162
00163 return 0;
00164 }
00165
00166 AVOutputFormat ff_adts_muxer = {
00167 .name = "adts",
00168 .long_name = NULL_IF_CONFIG_SMALL("ADTS AAC (Advanced Audio Coding)"),
00169 .mime_type = "audio/aac",
00170 .extensions = "aac,adts",
00171 .priv_data_size = sizeof(ADTSContext),
00172 .audio_codec = AV_CODEC_ID_AAC,
00173 .video_codec = AV_CODEC_ID_NONE,
00174 .write_header = adts_write_header,
00175 .write_packet = adts_write_packet,
00176 };