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