From 08e696c0b28e58797b4b77310e07a93ab3267369 Mon Sep 17 00:00:00 2001 From: Martin Storsjö Date: Wed, 8 Apr 2009 07:16:14 +0000 Subject: Add support for AMR audio in the RTP muxer patch by Martin Storsjö (martin AT martin DOT st) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Originally committed as revision 18375 to svn://svn.ffmpeg.org/ffmpeg/trunk --- Changelog | 1 + libavformat/Makefile | 1 + libavformat/rtp_amr.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++ libavformat/rtpenc.c | 23 ++++++++++++++++++ libavformat/rtpenc.h | 1 + libavformat/sdp.c | 12 ++++++++++ 6 files changed, 104 insertions(+) create mode 100644 libavformat/rtp_amr.c diff --git a/Changelog b/Changelog index 6781cb1b03..d5c80904ce 100644 --- a/Changelog +++ b/Changelog @@ -7,6 +7,7 @@ version : - Alpha channel scaler - PCX encoder - RTP packetization of H.263 +- RTP packetization of AMR diff --git a/libavformat/Makefile b/libavformat/Makefile index 2b35769d9e..e04b86f42e 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -188,6 +188,7 @@ OBJS-$(CONFIG_ROQ_MUXER) += raw.o OBJS-$(CONFIG_RPL_DEMUXER) += rpl.o OBJS-$(CONFIG_RTP_MUXER) += rtp.o \ rtp_aac.o \ + rtp_amr.o \ rtp_asf.o \ rtp_h263.o \ rtp_mpv.o \ diff --git a/libavformat/rtp_amr.c b/libavformat/rtp_amr.c new file mode 100644 index 0000000000..367789fccd --- /dev/null +++ b/libavformat/rtp_amr.c @@ -0,0 +1,66 @@ +/* + * RTP packetization for AMR audio + * Copyright (c) 2007 Luca Abeni + * Copyright (c) 2009 Martin Storsjo + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "avformat.h" +#include "rtpenc.h" + +/** + * Packetize AMR frames into RTP packets according to RFC 3267, + * in octet-aligned mode. + */ +void ff_rtp_send_amr(AVFormatContext *s1, const uint8_t *buff, int size) +{ + RTPMuxContext *s = s1->priv_data; + int max_header_toc_size = 1 + s->max_frames_per_packet; + uint8_t *p; + int len; + + /* Test if the packet must be sent. */ + len = s->buf_ptr - s->buf; + if (s->num_frames == s->max_frames_per_packet || (len && len + size - 1 > s->max_payload_size)) { + int header_size = s->num_frames + 1; + p = s->buf + max_header_toc_size - header_size; + if (p != s->buf) + memmove(p, s->buf, header_size); + + ff_rtp_send_data(s1, p, s->buf_ptr - p, 1); + + s->num_frames = 0; + } + + if (!s->num_frames) { + s->buf[0] = 0xf0; + s->buf_ptr = s->buf + max_header_toc_size; + s->timestamp = s->cur_timestamp; + } else { + /* Mark the previous TOC entry as having more entries following. */ + s->buf[1 + s->num_frames - 1] |= 0x80; + } + + /* Copy the frame type and quality bits. */ + s->buf[1 + s->num_frames++] = buff[0] & 0x7C; + buff++; + size--; + memcpy(s->buf_ptr, buff, size); + s->buf_ptr += size; +} + diff --git a/libavformat/rtpenc.c b/libavformat/rtpenc.c index 511c0bd5bc..c5df85961b 100644 --- a/libavformat/rtpenc.c +++ b/libavformat/rtpenc.c @@ -60,6 +60,8 @@ static int is_supported(enum CodecID id) case CODEC_ID_PCM_U16LE: case CODEC_ID_PCM_U8: case CODEC_ID_MPEG2TS: + case CODEC_ID_AMR_NB: + case CODEC_ID_AMR_WB: return 1; default: return 0; @@ -134,6 +136,23 @@ static int rtp_write_header(AVFormatContext *s1) s->max_payload_size = n * TS_PACKET_SIZE; s->buf_ptr = s->buf; break; + case CODEC_ID_AMR_NB: + case CODEC_ID_AMR_WB: + if (!s->max_frames_per_packet) + s->max_frames_per_packet = 12; + if (st->codec->codec_id == CODEC_ID_AMR_NB) + n = 31; + else + n = 61; + /* max_header_toc_size + the largest AMR payload must fit */ + if (1 + s->max_frames_per_packet + n > s->max_payload_size) { + av_log(s1, AV_LOG_ERROR, "RTP max payload size too small for AMR\n"); + return -1; + } + if (st->codec->channels != 1) { + av_log(s1, AV_LOG_ERROR, "Only mono is supported\n"); + return -1; + } case CODEC_ID_AAC: s->num_frames = 0; default: @@ -366,6 +385,10 @@ static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt) case CODEC_ID_AAC: ff_rtp_send_aac(s1, buf1, size); break; + case CODEC_ID_AMR_NB: + case CODEC_ID_AMR_WB: + ff_rtp_send_amr(s1, buf1, size); + break; case CODEC_ID_MPEG2TS: rtp_send_mpegts_raw(s1, buf1, size); break; diff --git a/libavformat/rtpenc.h b/libavformat/rtpenc.h index 35c548ffa8..57101601cd 100644 --- a/libavformat/rtpenc.h +++ b/libavformat/rtpenc.h @@ -59,6 +59,7 @@ void ff_rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m); void ff_rtp_send_h264(AVFormatContext *s1, const uint8_t *buf1, int size); void ff_rtp_send_h263(AVFormatContext *s1, const uint8_t *buf1, int size); void ff_rtp_send_aac(AVFormatContext *s1, const uint8_t *buff, int size); +void ff_rtp_send_amr(AVFormatContext *s1, const uint8_t *buff, int size); void ff_rtp_send_mpegvideo(AVFormatContext *s1, const uint8_t *buf1, int size); #endif /* AVFORMAT_RTPENC_H */ diff --git a/libavformat/sdp.c b/libavformat/sdp.c index fd51bc76ef..67b10a21fe 100644 --- a/libavformat/sdp.c +++ b/libavformat/sdp.c @@ -228,6 +228,18 @@ static char *sdp_write_media_attributes(char *buff, int size, AVCodecContext *c, payload_type, c->sample_rate, c->channels); break; + case CODEC_ID_AMR_NB: + av_strlcatf(buff, size, "a=rtpmap:%d AMR/%d/%d\r\n" + "a=fmtp:%d octet-align=1\r\n", + payload_type, c->sample_rate, c->channels, + payload_type); + break; + case CODEC_ID_AMR_WB: + av_strlcatf(buff, size, "a=rtpmap:%d AMR-WB/%d/%d\r\n" + "a=fmtp:%d octet-align=1\r\n", + payload_type, c->sample_rate, c->channels, + payload_type); + break; default: /* Nothing special to do here... */ break; -- cgit v1.2.3