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