changeset 4836:bf87d9ffb3ae libavformat

Add support for AMR audio in the RTP muxer patch by Martin Storsj (martin AT martin DOT st)
author lucabe
date Wed, 08 Apr 2009 07:16:14 +0000
parents ac71e186e2fe
children 4b74f09970d0
files Makefile rtp_amr.c rtpenc.c rtpenc.h sdp.c
diffstat 5 files changed, 103 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Wed Apr 08 03:38:59 2009 +0000
+++ b/Makefile	Wed Apr 08 07:16:14 2009 +0000
@@ -188,6 +188,7 @@
 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     \
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rtp_amr.c	Wed Apr 08 07:16:14 2009 +0000
@@ -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;
+}
+
--- a/rtpenc.c	Wed Apr 08 03:38:59 2009 +0000
+++ b/rtpenc.c	Wed Apr 08 07:16:14 2009 +0000
@@ -60,6 +60,8 @@
     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 @@
         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 @@
     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;
--- a/rtpenc.h	Wed Apr 08 03:38:59 2009 +0000
+++ b/rtpenc.h	Wed Apr 08 07:16:14 2009 +0000
@@ -59,6 +59,7 @@
 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 */
--- a/sdp.c	Wed Apr 08 03:38:59 2009 +0000
+++ b/sdp.c	Wed Apr 08 07:16:14 2009 +0000
@@ -228,6 +228,18 @@
                                          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;