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_amr.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 static int amr_handle_packet(AVFormatContext *ctx,
00034 PayloadContext *data,
00035 AVStream *st,
00036 AVPacket * pkt,
00037 uint32_t * timestamp,
00038 const uint8_t * buf,
00039 int len, int flags)
00040 {
00041 const uint8_t *frame_sizes = NULL;
00042 int frames;
00043 int i;
00044 const uint8_t *speech_data;
00045 uint8_t *ptr;
00046
00047 if (st->codec->codec_id == CODEC_ID_AMR_NB) {
00048 frame_sizes = frame_sizes_nb;
00049 } else if (st->codec->codec_id == CODEC_ID_AMR_WB) {
00050 frame_sizes = frame_sizes_wb;
00051 } else {
00052 av_log(ctx, AV_LOG_ERROR, "Bad codec ID\n");
00053 return AVERROR_INVALIDDATA;
00054 }
00055
00056 if (st->codec->channels != 1) {
00057 av_log(ctx, AV_LOG_ERROR, "Only mono AMR is supported\n");
00058 return AVERROR_INVALIDDATA;
00059 }
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 for (frames = 1; frames < len && (buf[frames] & 0x80); frames++) ;
00074
00075 if (1 + frames >= len) {
00076
00077 av_log(ctx, AV_LOG_ERROR, "No speech data found\n");
00078 return AVERROR_INVALIDDATA;
00079 }
00080
00081 speech_data = buf + 1 + frames;
00082
00083
00084 if (av_new_packet(pkt, len - 1)) {
00085 av_log(ctx, AV_LOG_ERROR, "Out of memory\n");
00086 return AVERROR(ENOMEM);
00087 }
00088 pkt->stream_index = st->index;
00089 ptr = pkt->data;
00090
00091 for (i = 0; i < frames; i++) {
00092 uint8_t toc = buf[1 + i];
00093 int frame_size = frame_sizes[(toc >> 3) & 0x0f];
00094
00095 if (speech_data + frame_size > buf + len) {
00096
00097 av_log(ctx, AV_LOG_WARNING, "Too little speech data in the RTP packet\n");
00098
00099 memset(ptr, 0, pkt->data + pkt->size - ptr);
00100 pkt->size = ptr - pkt->data;
00101 return 0;
00102 }
00103
00104
00105 *ptr++ = toc & 0x7C;
00106
00107
00108 memcpy(ptr, speech_data, frame_size);
00109 speech_data += frame_size;
00110 ptr += frame_size;
00111 }
00112
00113 if (speech_data < buf + len) {
00114 av_log(ctx, AV_LOG_WARNING, "Too much speech data in the RTP packet?\n");
00115
00116 memset(ptr, 0, pkt->data + pkt->size - ptr);
00117 pkt->size = ptr - pkt->data;
00118 }
00119
00120 return 0;
00121 }
00122
00123 static int amr_parse_sdp_line(AVFormatContext *s, int st_index,
00124 PayloadContext *data, const char *line)
00125 {
00126 const char *p;
00127 char attr[25], value[25];
00128
00129
00130
00131
00132
00133
00134 if (av_strstart(line, "fmtp:", &p)) {
00135 int octet_align = 0;
00136 int crc = 0;
00137 int interleaving = 0;
00138 int channels = 1;
00139
00140 while (*p && *p == ' ') p++;
00141 while (*p && *p != ' ') p++;
00142 while (*p && *p == ' ') p++;
00143
00144 while (ff_rtsp_next_attr_and_value(&p, attr, sizeof(attr), value, sizeof(value))) {
00145
00146
00147
00148
00149 if (!strcmp(value, "")) {
00150 av_log(s, AV_LOG_WARNING, "AMR fmtp attribute %s had "
00151 "nonstandard empty value\n", attr);
00152 strcpy(value, "1");
00153 }
00154 if (!strcmp(attr, "octet-align"))
00155 octet_align = atoi(value);
00156 else if (!strcmp(attr, "crc"))
00157 crc = atoi(value);
00158 else if (!strcmp(attr, "interleaving"))
00159 interleaving = atoi(value);
00160 else if (!strcmp(attr, "channels"))
00161 channels = atoi(value);
00162 }
00163 if (!octet_align || crc || interleaving || channels != 1) {
00164 av_log(s, AV_LOG_ERROR, "Unsupported RTP/AMR configuration!\n");
00165 return -1;
00166 }
00167 }
00168 return 0;
00169 }
00170
00171 RTPDynamicProtocolHandler ff_amr_nb_dynamic_handler = {
00172 .enc_name = "AMR",
00173 .codec_type = AVMEDIA_TYPE_AUDIO,
00174 .codec_id = CODEC_ID_AMR_NB,
00175 .parse_sdp_a_line = amr_parse_sdp_line,
00176 .parse_packet = amr_handle_packet,
00177 };
00178
00179 RTPDynamicProtocolHandler ff_amr_wb_dynamic_handler = {
00180 .enc_name = "AMR-WB",
00181 .codec_type = AVMEDIA_TYPE_AUDIO,
00182 .codec_id = CODEC_ID_AMR_WB,
00183 .parse_sdp_a_line = amr_parse_sdp_line,
00184 .parse_packet = amr_handle_packet,
00185 };
00186