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