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 (!adts->channel_conf) {
00063 init_put_bits(&pb, adts->pce_data, MAX_PCE_SIZE);
00064
00065 put_bits(&pb, 3, 5);
00066 adts->pce_size = (ff_copy_pce_data(&pb, &gb) + 3) / 8;
00067 flush_put_bits(&pb);
00068 }
00069
00070 adts->write_adts = 1;
00071
00072 return 0;
00073 }
00074
00075 static int adts_write_header(AVFormatContext *s)
00076 {
00077 ADTSContext *adts = s->priv_data;
00078 AVCodecContext *avc = s->streams[0]->codec;
00079
00080 if (avc->extradata_size > 0 &&
00081 ff_adts_decode_extradata(s, adts, avc->extradata, avc->extradata_size) < 0)
00082 return -1;
00083
00084 return 0;
00085 }
00086
00087 int ff_adts_write_frame_header(ADTSContext *ctx,
00088 uint8_t *buf, int size, int pce_size)
00089 {
00090 PutBitContext pb;
00091
00092 init_put_bits(&pb, buf, ADTS_HEADER_SIZE);
00093
00094
00095 put_bits(&pb, 12, 0xfff);
00096 put_bits(&pb, 1, 0);
00097 put_bits(&pb, 2, 0);
00098 put_bits(&pb, 1, 1);
00099 put_bits(&pb, 2, ctx->objecttype);
00100 put_bits(&pb, 4, ctx->sample_rate_index);
00101 put_bits(&pb, 1, 0);
00102 put_bits(&pb, 3, ctx->channel_conf);
00103 put_bits(&pb, 1, 0);
00104 put_bits(&pb, 1, 0);
00105
00106
00107 put_bits(&pb, 1, 0);
00108 put_bits(&pb, 1, 0);
00109 put_bits(&pb, 13, ADTS_HEADER_SIZE + size + pce_size);
00110 put_bits(&pb, 11, 0x7ff);
00111 put_bits(&pb, 2, 0);
00112
00113 flush_put_bits(&pb);
00114
00115 return 0;
00116 }
00117
00118 static int adts_write_packet(AVFormatContext *s, AVPacket *pkt)
00119 {
00120 ADTSContext *adts = s->priv_data;
00121 AVIOContext *pb = s->pb;
00122 uint8_t buf[ADTS_HEADER_SIZE];
00123
00124 if (!pkt->size)
00125 return 0;
00126 if (adts->write_adts) {
00127 ff_adts_write_frame_header(adts, buf, pkt->size, adts->pce_size);
00128 avio_write(pb, buf, ADTS_HEADER_SIZE);
00129 if (adts->pce_size) {
00130 avio_write(pb, adts->pce_data, adts->pce_size);
00131 adts->pce_size = 0;
00132 }
00133 }
00134 avio_write(pb, pkt->data, pkt->size);
00135 put_flush_packet(pb);
00136
00137 return 0;
00138 }
00139
00140 AVOutputFormat ff_adts_muxer = {
00141 "adts",
00142 NULL_IF_CONFIG_SMALL("ADTS AAC"),
00143 "audio/aac",
00144 "aac,adts",
00145 sizeof(ADTSContext),
00146 CODEC_ID_AAC,
00147 CODEC_ID_NONE,
00148 adts_write_header,
00149 adts_write_packet,
00150 };