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