00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "avformat.h"
00029 #include "internal.h"
00030
00031 static const char AMR_header [] = "#!AMR\n";
00032 static const char AMRWB_header [] = "#!AMR-WB\n";
00033
00034 #if CONFIG_AMR_MUXER
00035 static int amr_write_header(AVFormatContext *s)
00036 {
00037 AVIOContext *pb = s->pb;
00038 AVCodecContext *enc = s->streams[0]->codec;
00039
00040 s->priv_data = NULL;
00041
00042 if (enc->codec_id == CODEC_ID_AMR_NB)
00043 {
00044 avio_write(pb, AMR_header, sizeof(AMR_header) - 1);
00045 }
00046 else if(enc->codec_id == CODEC_ID_AMR_WB)
00047 {
00048 avio_write(pb, AMRWB_header, sizeof(AMRWB_header) - 1);
00049 }
00050 else
00051 {
00052 return -1;
00053 }
00054 avio_flush(pb);
00055 return 0;
00056 }
00057
00058 static int amr_write_packet(AVFormatContext *s, AVPacket *pkt)
00059 {
00060 avio_write(s->pb, pkt->data, pkt->size);
00061 avio_flush(s->pb);
00062 return 0;
00063 }
00064 #endif
00065
00066 static int amr_probe(AVProbeData *p)
00067 {
00068
00069
00070
00071
00072 if(memcmp(p->buf,AMR_header,5)==0)
00073 return AVPROBE_SCORE_MAX;
00074 else
00075 return 0;
00076 }
00077
00078
00079 static int amr_read_header(AVFormatContext *s)
00080 {
00081 AVIOContext *pb = s->pb;
00082 AVStream *st;
00083 uint8_t header[9];
00084
00085 avio_read(pb, header, 6);
00086
00087 st = avformat_new_stream(s, NULL);
00088 if (!st)
00089 {
00090 return AVERROR(ENOMEM);
00091 }
00092 if(memcmp(header,AMR_header,6)!=0)
00093 {
00094 avio_read(pb, header+6, 3);
00095 if(memcmp(header,AMRWB_header,9)!=0)
00096 {
00097 return -1;
00098 }
00099
00100 st->codec->codec_tag = MKTAG('s', 'a', 'w', 'b');
00101 st->codec->codec_id = CODEC_ID_AMR_WB;
00102 st->codec->sample_rate = 16000;
00103 }
00104 else
00105 {
00106 st->codec->codec_tag = MKTAG('s', 'a', 'm', 'r');
00107 st->codec->codec_id = CODEC_ID_AMR_NB;
00108 st->codec->sample_rate = 8000;
00109 }
00110 st->codec->channels = 1;
00111 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00112 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
00113
00114 return 0;
00115 }
00116
00117 static int amr_read_packet(AVFormatContext *s,
00118 AVPacket *pkt)
00119 {
00120 AVCodecContext *enc = s->streams[0]->codec;
00121 int read, size = 0, toc, mode;
00122 int64_t pos = avio_tell(s->pb);
00123
00124 if (url_feof(s->pb))
00125 {
00126 return AVERROR(EIO);
00127 }
00128
00129
00130 toc=avio_r8(s->pb);
00131 mode = (toc >> 3) & 0x0F;
00132
00133 if (enc->codec_id == CODEC_ID_AMR_NB)
00134 {
00135 static const uint8_t packed_size[16] = {12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0};
00136
00137 size=packed_size[mode]+1;
00138 }
00139 else if(enc->codec_id == CODEC_ID_AMR_WB)
00140 {
00141 static uint8_t packed_size[16] = {18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 6, 0, 0, 0, 1, 1};
00142
00143 size=packed_size[mode];
00144 }
00145 else
00146 {
00147 assert(0);
00148 }
00149
00150 if ( (size==0) || av_new_packet(pkt, size))
00151 {
00152 return AVERROR(EIO);
00153 }
00154
00155
00156 s->streams[0]->codec->bit_rate = size*8*50;
00157
00158 pkt->stream_index = 0;
00159 pkt->pos = pos;
00160 pkt->data[0]=toc;
00161 pkt->duration= enc->codec_id == CODEC_ID_AMR_NB ? 160 : 320;
00162 read = avio_read(s->pb, pkt->data+1, size-1);
00163
00164 if (read != size-1)
00165 {
00166 av_free_packet(pkt);
00167 return AVERROR(EIO);
00168 }
00169
00170 return 0;
00171 }
00172
00173 #if CONFIG_AMR_DEMUXER
00174 AVInputFormat ff_amr_demuxer = {
00175 .name = "amr",
00176 .long_name = NULL_IF_CONFIG_SMALL("3GPP AMR file format"),
00177 .read_probe = amr_probe,
00178 .read_header = amr_read_header,
00179 .read_packet = amr_read_packet,
00180 .flags = AVFMT_GENERIC_INDEX,
00181 };
00182 #endif
00183
00184 #if CONFIG_AMR_MUXER
00185 AVOutputFormat ff_amr_muxer = {
00186 .name = "amr",
00187 .long_name = NULL_IF_CONFIG_SMALL("3GPP AMR file format"),
00188 .mime_type = "audio/amr",
00189 .extensions = "amr",
00190 .audio_codec = CODEC_ID_AMR_NB,
00191 .video_codec = CODEC_ID_NONE,
00192 .write_header = amr_write_header,
00193 .write_packet = amr_write_packet,
00194 };
00195 #endif