00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "avformat.h"
00023 #include "rtpdec_formats.h"
00024 #include "libavutil/avstring.h"
00025
00026 static const uint8_t frame_sizes_nb[16] = {
00027 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0
00028 };
00029 static const uint8_t frame_sizes_wb[16] = {
00030 17, 23, 32, 36, 40, 46, 50, 58, 60, 5, 5, 0, 0, 0, 0, 0
00031 };
00032
00033 struct PayloadContext {
00034 int octet_align;
00035 int crc;
00036 int interleaving;
00037 int channels;
00038 };
00039
00040 static PayloadContext *amr_new_context(void)
00041 {
00042 PayloadContext *data = av_mallocz(sizeof(PayloadContext));
00043 if(!data) return data;
00044 data->channels = 1;
00045 return data;
00046 }
00047
00048 static void amr_free_context(PayloadContext *data)
00049 {
00050 av_free(data);
00051 }
00052
00053 static int amr_handle_packet(AVFormatContext *ctx,
00054 PayloadContext *data,
00055 AVStream *st,
00056 AVPacket * pkt,
00057 uint32_t * timestamp,
00058 const uint8_t * buf,
00059 int len, int flags)
00060 {
00061 const uint8_t *frame_sizes = NULL;
00062 int frames;
00063 int i;
00064 const uint8_t *speech_data;
00065 uint8_t *ptr;
00066
00067 if (st->codec->codec_id == CODEC_ID_AMR_NB) {
00068 frame_sizes = frame_sizes_nb;
00069 } else if (st->codec->codec_id == CODEC_ID_AMR_WB) {
00070 frame_sizes = frame_sizes_wb;
00071 } else {
00072 av_log(ctx, AV_LOG_ERROR, "Bad codec ID\n");
00073 return AVERROR_INVALIDDATA;
00074 }
00075
00076 if (st->codec->channels != 1) {
00077 av_log(ctx, AV_LOG_ERROR, "Only mono AMR is supported\n");
00078 return AVERROR_INVALIDDATA;
00079 }
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093 for (frames = 1; frames < len && (buf[frames] & 0x80); frames++) ;
00094
00095 if (1 + frames >= len) {
00096
00097 av_log(ctx, AV_LOG_ERROR, "No speech data found\n");
00098 return AVERROR_INVALIDDATA;
00099 }
00100
00101 speech_data = buf + 1 + frames;
00102
00103
00104 if (av_new_packet(pkt, len - 1)) {
00105 av_log(ctx, AV_LOG_ERROR, "Out of memory\n");
00106 return AVERROR(ENOMEM);
00107 }
00108 pkt->stream_index = st->index;
00109 ptr = pkt->data;
00110
00111 for (i = 0; i < frames; i++) {
00112 uint8_t toc = buf[1 + i];
00113 int frame_size = frame_sizes[(toc >> 3) & 0x0f];
00114
00115 if (speech_data + frame_size > buf + len) {
00116
00117 av_log(ctx, AV_LOG_WARNING, "Too little speech data in the RTP packet\n");
00118
00119 memset(ptr, 0, pkt->data + pkt->size - ptr);
00120 pkt->size = ptr - pkt->data;
00121 return 0;
00122 }
00123
00124
00125 *ptr++ = toc & 0x7C;
00126
00127
00128 memcpy(ptr, speech_data, frame_size);
00129 speech_data += frame_size;
00130 ptr += frame_size;
00131 }
00132
00133 if (speech_data < buf + len) {
00134 av_log(ctx, AV_LOG_WARNING, "Too much speech data in the RTP packet?\n");
00135
00136 memset(ptr, 0, pkt->data + pkt->size - ptr);
00137 pkt->size = ptr - pkt->data;
00138 }
00139
00140 return 0;
00141 }
00142
00143 static int amr_parse_fmtp(AVStream *stream, PayloadContext *data,
00144 char *attr, char *value)
00145 {
00146
00147
00148
00149
00150 if (!strcmp(value, "")) {
00151 av_log(NULL, AV_LOG_WARNING, "AMR fmtp attribute %s had "
00152 "nonstandard empty value\n", attr);
00153 strcpy(value, "1");
00154 }
00155 if (!strcmp(attr, "octet-align"))
00156 data->octet_align = atoi(value);
00157 else if (!strcmp(attr, "crc"))
00158 data->crc = atoi(value);
00159 else if (!strcmp(attr, "interleaving"))
00160 data->interleaving = atoi(value);
00161 else if (!strcmp(attr, "channels"))
00162 data->channels = atoi(value);
00163 return 0;
00164 }
00165
00166 static int amr_parse_sdp_line(AVFormatContext *s, int st_index,
00167 PayloadContext *data, const char *line)
00168 {
00169 const char *p;
00170 int ret;
00171
00172
00173
00174
00175
00176
00177 if (av_strstart(line, "fmtp:", &p)) {
00178 ret = ff_parse_fmtp(s->streams[st_index], data, p, amr_parse_fmtp);
00179 if (!data->octet_align || data->crc ||
00180 data->interleaving || data->channels != 1) {
00181 av_log(s, AV_LOG_ERROR, "Unsupported RTP/AMR configuration!\n");
00182 return -1;
00183 }
00184 return ret;
00185 }
00186 return 0;
00187 }
00188
00189 RTPDynamicProtocolHandler ff_amr_nb_dynamic_handler = {
00190 .enc_name = "AMR",
00191 .codec_type = AVMEDIA_TYPE_AUDIO,
00192 .codec_id = CODEC_ID_AMR_NB,
00193 .parse_sdp_a_line = amr_parse_sdp_line,
00194 .alloc = amr_new_context,
00195 .free = amr_free_context,
00196 .parse_packet = amr_handle_packet,
00197 };
00198
00199 RTPDynamicProtocolHandler ff_amr_wb_dynamic_handler = {
00200 .enc_name = "AMR-WB",
00201 .codec_type = AVMEDIA_TYPE_AUDIO,
00202 .codec_id = CODEC_ID_AMR_WB,
00203 .parse_sdp_a_line = amr_parse_sdp_line,
00204 .alloc = amr_new_context,
00205 .free = amr_free_context,
00206 .parse_packet = amr_handle_packet,
00207 };
00208