yading@11: /* yading@11: * RTP AMR Depacketizer, RFC 3267 yading@11: * Copyright (c) 2010 Martin Storsjo yading@11: * yading@11: * This file is part of FFmpeg. yading@11: * yading@11: * FFmpeg is free software; you can redistribute it and/or yading@11: * modify it under the terms of the GNU Lesser General Public yading@11: * License as published by the Free Software Foundation; either yading@11: * version 2.1 of the License, or (at your option) any later version. yading@11: * yading@11: * FFmpeg is distributed in the hope that it will be useful, yading@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@11: * Lesser General Public License for more details. yading@11: * yading@11: * You should have received a copy of the GNU Lesser General Public yading@11: * License along with FFmpeg; if not, write to the Free Software yading@11: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@11: */ yading@11: yading@11: #include "libavutil/channel_layout.h" yading@11: #include "avformat.h" yading@11: #include "rtpdec_formats.h" yading@11: #include "libavutil/avstring.h" yading@11: yading@11: static const uint8_t frame_sizes_nb[16] = { yading@11: 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 yading@11: }; yading@11: static const uint8_t frame_sizes_wb[16] = { yading@11: 17, 23, 32, 36, 40, 46, 50, 58, 60, 5, 5, 0, 0, 0, 0, 0 yading@11: }; yading@11: yading@11: struct PayloadContext { yading@11: int octet_align; yading@11: int crc; yading@11: int interleaving; yading@11: int channels; yading@11: }; yading@11: yading@11: static PayloadContext *amr_new_context(void) yading@11: { yading@11: PayloadContext *data = av_mallocz(sizeof(PayloadContext)); yading@11: if(!data) return data; yading@11: data->channels = 1; yading@11: return data; yading@11: } yading@11: yading@11: static void amr_free_context(PayloadContext *data) yading@11: { yading@11: av_free(data); yading@11: } yading@11: yading@11: static int amr_handle_packet(AVFormatContext *ctx, PayloadContext *data, yading@11: AVStream *st, AVPacket *pkt, uint32_t *timestamp, yading@11: const uint8_t *buf, int len, uint16_t seq, yading@11: int flags) yading@11: { yading@11: const uint8_t *frame_sizes = NULL; yading@11: int frames; yading@11: int i; yading@11: const uint8_t *speech_data; yading@11: uint8_t *ptr; yading@11: yading@11: if (st->codec->codec_id == AV_CODEC_ID_AMR_NB) { yading@11: frame_sizes = frame_sizes_nb; yading@11: } else if (st->codec->codec_id == AV_CODEC_ID_AMR_WB) { yading@11: frame_sizes = frame_sizes_wb; yading@11: } else { yading@11: av_log(ctx, AV_LOG_ERROR, "Bad codec ID\n"); yading@11: return AVERROR_INVALIDDATA; yading@11: } yading@11: yading@11: if (st->codec->channels != 1) { yading@11: av_log(ctx, AV_LOG_ERROR, "Only mono AMR is supported\n"); yading@11: return AVERROR_INVALIDDATA; yading@11: } yading@11: st->codec->channel_layout = AV_CH_LAYOUT_MONO; yading@11: yading@11: /* The AMR RTP packet consists of one header byte, followed yading@11: * by one TOC byte for each AMR frame in the packet, followed yading@11: * by the speech data for all the AMR frames. yading@11: * yading@11: * The header byte contains only a codec mode request, for yading@11: * requesting what kind of AMR data the sender wants to yading@11: * receive. Not used at the moment. yading@11: */ yading@11: yading@11: /* Count the number of frames in the packet. The highest bit yading@11: * is set in a TOC byte if there are more frames following. yading@11: */ yading@11: for (frames = 1; frames < len && (buf[frames] & 0x80); frames++) ; yading@11: yading@11: if (1 + frames >= len) { yading@11: /* We hit the end of the packet while counting frames. */ yading@11: av_log(ctx, AV_LOG_ERROR, "No speech data found\n"); yading@11: return AVERROR_INVALIDDATA; yading@11: } yading@11: yading@11: speech_data = buf + 1 + frames; yading@11: yading@11: /* Everything except the codec mode request byte should be output. */ yading@11: if (av_new_packet(pkt, len - 1)) { yading@11: av_log(ctx, AV_LOG_ERROR, "Out of memory\n"); yading@11: return AVERROR(ENOMEM); yading@11: } yading@11: pkt->stream_index = st->index; yading@11: ptr = pkt->data; yading@11: yading@11: for (i = 0; i < frames; i++) { yading@11: uint8_t toc = buf[1 + i]; yading@11: int frame_size = frame_sizes[(toc >> 3) & 0x0f]; yading@11: yading@11: if (speech_data + frame_size > buf + len) { yading@11: /* Too little speech data */ yading@11: av_log(ctx, AV_LOG_WARNING, "Too little speech data in the RTP packet\n"); yading@11: /* Set the unwritten part of the packet to zero. */ yading@11: memset(ptr, 0, pkt->data + pkt->size - ptr); yading@11: pkt->size = ptr - pkt->data; yading@11: return 0; yading@11: } yading@11: yading@11: /* Extract the AMR frame mode from the TOC byte */ yading@11: *ptr++ = toc & 0x7C; yading@11: yading@11: /* Copy the speech data */ yading@11: memcpy(ptr, speech_data, frame_size); yading@11: speech_data += frame_size; yading@11: ptr += frame_size; yading@11: } yading@11: yading@11: if (speech_data < buf + len) { yading@11: av_log(ctx, AV_LOG_WARNING, "Too much speech data in the RTP packet?\n"); yading@11: /* Set the unwritten part of the packet to zero. */ yading@11: memset(ptr, 0, pkt->data + pkt->size - ptr); yading@11: pkt->size = ptr - pkt->data; yading@11: } yading@11: yading@11: return 0; yading@11: } yading@11: yading@11: static int amr_parse_fmtp(AVStream *stream, PayloadContext *data, yading@11: char *attr, char *value) yading@11: { yading@11: /* Some AMR SDP configurations contain "octet-align", without yading@11: * the trailing =1. Therefore, if the value is empty, yading@11: * interpret it as "1". yading@11: */ yading@11: if (!strcmp(value, "")) { yading@11: av_log(NULL, AV_LOG_WARNING, "AMR fmtp attribute %s had " yading@11: "nonstandard empty value\n", attr); yading@11: strcpy(value, "1"); yading@11: } yading@11: if (!strcmp(attr, "octet-align")) yading@11: data->octet_align = atoi(value); yading@11: else if (!strcmp(attr, "crc")) yading@11: data->crc = atoi(value); yading@11: else if (!strcmp(attr, "interleaving")) yading@11: data->interleaving = atoi(value); yading@11: else if (!strcmp(attr, "channels")) yading@11: data->channels = atoi(value); yading@11: return 0; yading@11: } yading@11: yading@11: static int amr_parse_sdp_line(AVFormatContext *s, int st_index, yading@11: PayloadContext *data, const char *line) yading@11: { yading@11: const char *p; yading@11: int ret; yading@11: yading@11: if (st_index < 0) yading@11: return 0; yading@11: yading@11: /* Parse an fmtp line this one: yading@11: * a=fmtp:97 octet-align=1; interleaving=0 yading@11: * That is, a normal fmtp: line followed by semicolon & space yading@11: * separated key/value pairs. yading@11: */ yading@11: if (av_strstart(line, "fmtp:", &p)) { yading@11: ret = ff_parse_fmtp(s->streams[st_index], data, p, amr_parse_fmtp); yading@11: if (!data->octet_align || data->crc || yading@11: data->interleaving || data->channels != 1) { yading@11: av_log(s, AV_LOG_ERROR, "Unsupported RTP/AMR configuration!\n"); yading@11: return -1; yading@11: } yading@11: return ret; yading@11: } yading@11: return 0; yading@11: } yading@11: yading@11: RTPDynamicProtocolHandler ff_amr_nb_dynamic_handler = { yading@11: .enc_name = "AMR", yading@11: .codec_type = AVMEDIA_TYPE_AUDIO, yading@11: .codec_id = AV_CODEC_ID_AMR_NB, yading@11: .parse_sdp_a_line = amr_parse_sdp_line, yading@11: .alloc = amr_new_context, yading@11: .free = amr_free_context, yading@11: .parse_packet = amr_handle_packet, yading@11: }; yading@11: yading@11: RTPDynamicProtocolHandler ff_amr_wb_dynamic_handler = { yading@11: .enc_name = "AMR-WB", yading@11: .codec_type = AVMEDIA_TYPE_AUDIO, yading@11: .codec_id = AV_CODEC_ID_AMR_WB, yading@11: .parse_sdp_a_line = amr_parse_sdp_line, yading@11: .alloc = amr_new_context, yading@11: .free = amr_free_context, yading@11: .parse_packet = amr_handle_packet, yading@11: };