Add support for AMR audio in the RTP muxer
authorMartin Storsjö <martin@martin.st>
Wed, 8 Apr 2009 07:16:14 +0000 (07:16 +0000)
committerLuca Abeni <lucabe72@email.it>
Wed, 8 Apr 2009 07:16:14 +0000 (07:16 +0000)
patch by Martin Storsjö (martin AT martin DOT st)

Originally committed as revision 18375 to svn://svn.ffmpeg.org/ffmpeg/trunk

Changelog
libavformat/Makefile
libavformat/rtp_amr.c [new file with mode: 0644]
libavformat/rtpenc.c
libavformat/rtpenc.h
libavformat/sdp.c

index 6781cb1..d5c8090 100644 (file)
--- a/Changelog
+++ b/Changelog
@@ -7,6 +7,7 @@ version <next>:
 - Alpha channel scaler
 - PCX encoder
 - RTP packetization of H.263
+- RTP packetization of AMR
 
 
 
index 2b35769..e04b86f 100644 (file)
@@ -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 (file)
index 0000000..367789f
--- /dev/null
@@ -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;
+}
+
index 511c0bd..c5df859 100644 (file)
@@ -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;
index 35c548f..5710160 100644 (file)
@@ -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 */
index fd51bc7..67b10a2 100644 (file)
@@ -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;