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