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