Commit 08e696c0 authored by Martin Storsjö's avatar Martin Storsjö Committed by Luca Abeni

Add support for AMR audio in the RTP muxer

patch by Martin Storsjö (martin AT martin DOT st)

Originally committed as revision 18375 to svn://svn.ffmpeg.org/ffmpeg/trunk
parent 17ad7b24
...@@ -7,6 +7,7 @@ version <next>: ...@@ -7,6 +7,7 @@ version <next>:
- Alpha channel scaler - Alpha channel scaler
- PCX encoder - PCX encoder
- RTP packetization of H.263 - RTP packetization of H.263
- RTP packetization of AMR
......
...@@ -188,6 +188,7 @@ OBJS-$(CONFIG_ROQ_MUXER) += raw.o ...@@ -188,6 +188,7 @@ OBJS-$(CONFIG_ROQ_MUXER) += raw.o
OBJS-$(CONFIG_RPL_DEMUXER) += rpl.o OBJS-$(CONFIG_RPL_DEMUXER) += rpl.o
OBJS-$(CONFIG_RTP_MUXER) += rtp.o \ OBJS-$(CONFIG_RTP_MUXER) += rtp.o \
rtp_aac.o \ rtp_aac.o \
rtp_amr.o \
rtp_asf.o \ rtp_asf.o \
rtp_h263.o \ rtp_h263.o \
rtp_mpv.o \ rtp_mpv.o \
......
/*
* 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;
}
...@@ -60,6 +60,8 @@ static int is_supported(enum CodecID id) ...@@ -60,6 +60,8 @@ static int is_supported(enum CodecID id)
case CODEC_ID_PCM_U16LE: case CODEC_ID_PCM_U16LE:
case CODEC_ID_PCM_U8: case CODEC_ID_PCM_U8:
case CODEC_ID_MPEG2TS: case CODEC_ID_MPEG2TS:
case CODEC_ID_AMR_NB:
case CODEC_ID_AMR_WB:
return 1; return 1;
default: default:
return 0; return 0;
...@@ -134,6 +136,23 @@ static int rtp_write_header(AVFormatContext *s1) ...@@ -134,6 +136,23 @@ static int rtp_write_header(AVFormatContext *s1)
s->max_payload_size = n * TS_PACKET_SIZE; s->max_payload_size = n * TS_PACKET_SIZE;
s->buf_ptr = s->buf; s->buf_ptr = s->buf;
break; 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: case CODEC_ID_AAC:
s->num_frames = 0; s->num_frames = 0;
default: default:
...@@ -366,6 +385,10 @@ static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt) ...@@ -366,6 +385,10 @@ static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt)
case CODEC_ID_AAC: case CODEC_ID_AAC:
ff_rtp_send_aac(s1, buf1, size); ff_rtp_send_aac(s1, buf1, size);
break; break;
case CODEC_ID_AMR_NB:
case CODEC_ID_AMR_WB:
ff_rtp_send_amr(s1, buf1, size);
break;
case CODEC_ID_MPEG2TS: case CODEC_ID_MPEG2TS:
rtp_send_mpegts_raw(s1, buf1, size); rtp_send_mpegts_raw(s1, buf1, size);
break; break;
......
...@@ -59,6 +59,7 @@ void ff_rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m); ...@@ -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_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_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_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); void ff_rtp_send_mpegvideo(AVFormatContext *s1, const uint8_t *buf1, int size);
#endif /* AVFORMAT_RTPENC_H */ #endif /* AVFORMAT_RTPENC_H */
...@@ -228,6 +228,18 @@ static char *sdp_write_media_attributes(char *buff, int size, AVCodecContext *c, ...@@ -228,6 +228,18 @@ static char *sdp_write_media_attributes(char *buff, int size, AVCodecContext *c,
payload_type, payload_type,
c->sample_rate, c->channels); c->sample_rate, c->channels);
break; 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: default:
/* Nothing special to do here... */ /* Nothing special to do here... */
break; break;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment