2010-02-10 17:20:50 +00:00
|
|
|
/*
|
|
|
|
* RTP AMR Depacketizer, RFC 3267
|
|
|
|
* Copyright (c) 2010 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
|
|
|
|
*/
|
|
|
|
|
2012-04-07 22:24:45 +00:00
|
|
|
#include "libavutil/channel_layout.h"
|
2010-02-10 17:20:50 +00:00
|
|
|
#include "avformat.h"
|
2010-07-30 12:04:27 +00:00
|
|
|
#include "rtpdec_formats.h"
|
2010-02-10 17:20:50 +00:00
|
|
|
#include "libavutil/avstring.h"
|
|
|
|
|
|
|
|
static const uint8_t frame_sizes_nb[16] = {
|
|
|
|
12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0
|
|
|
|
};
|
|
|
|
static const uint8_t frame_sizes_wb[16] = {
|
|
|
|
17, 23, 32, 36, 40, 46, 50, 58, 60, 5, 5, 0, 0, 0, 0, 0
|
|
|
|
};
|
|
|
|
|
2010-06-28 20:35:21 +00:00
|
|
|
struct PayloadContext {
|
|
|
|
int octet_align;
|
|
|
|
int crc;
|
|
|
|
int interleaving;
|
|
|
|
int channels;
|
|
|
|
};
|
|
|
|
|
2015-02-24 10:43:07 +00:00
|
|
|
static av_cold int amr_init(AVFormatContext *s, int st_index, PayloadContext *data)
|
2010-06-28 20:35:21 +00:00
|
|
|
{
|
|
|
|
data->channels = 1;
|
2015-02-24 10:43:07 +00:00
|
|
|
return 0;
|
2010-06-28 20:35:21 +00:00
|
|
|
}
|
|
|
|
|
2012-12-10 12:38:32 +00:00
|
|
|
static int amr_handle_packet(AVFormatContext *ctx, PayloadContext *data,
|
|
|
|
AVStream *st, AVPacket *pkt, uint32_t *timestamp,
|
|
|
|
const uint8_t *buf, int len, uint16_t seq,
|
|
|
|
int flags)
|
2010-02-10 17:20:50 +00:00
|
|
|
{
|
|
|
|
const uint8_t *frame_sizes = NULL;
|
|
|
|
int frames;
|
2019-12-10 21:59:53 +00:00
|
|
|
int i, ret;
|
2010-02-10 17:20:50 +00:00
|
|
|
const uint8_t *speech_data;
|
|
|
|
uint8_t *ptr;
|
|
|
|
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 18:42:52 +00:00
|
|
|
if (st->codecpar->codec_id == AV_CODEC_ID_AMR_NB) {
|
2010-02-10 17:20:50 +00:00
|
|
|
frame_sizes = frame_sizes_nb;
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 18:42:52 +00:00
|
|
|
} else if (st->codecpar->codec_id == AV_CODEC_ID_AMR_WB) {
|
2010-02-10 17:20:50 +00:00
|
|
|
frame_sizes = frame_sizes_wb;
|
|
|
|
} else {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Bad codec ID\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2024-04-25 09:18:18 +00:00
|
|
|
if (st->codecpar->ch_layout.nb_channels != 1) {
|
2010-02-10 17:20:50 +00:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Only mono AMR is supported\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2024-04-25 09:18:18 +00:00
|
|
|
av_channel_layout_default(&st->codecpar->ch_layout, 1);
|
2010-02-10 17:20:50 +00:00
|
|
|
|
|
|
|
/* The AMR RTP packet consists of one header byte, followed
|
|
|
|
* by one TOC byte for each AMR frame in the packet, followed
|
|
|
|
* by the speech data for all the AMR frames.
|
|
|
|
*
|
|
|
|
* The header byte contains only a codec mode request, for
|
|
|
|
* requesting what kind of AMR data the sender wants to
|
|
|
|
* receive. Not used at the moment.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Count the number of frames in the packet. The highest bit
|
|
|
|
* is set in a TOC byte if there are more frames following.
|
|
|
|
*/
|
|
|
|
for (frames = 1; frames < len && (buf[frames] & 0x80); frames++) ;
|
|
|
|
|
|
|
|
if (1 + frames >= len) {
|
|
|
|
/* We hit the end of the packet while counting frames. */
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "No speech data found\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
speech_data = buf + 1 + frames;
|
|
|
|
|
|
|
|
/* Everything except the codec mode request byte should be output. */
|
2019-12-10 21:59:53 +00:00
|
|
|
if ((ret = av_new_packet(pkt, len - 1)) < 0) {
|
2010-02-10 17:20:50 +00:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Out of memory\n");
|
2019-12-10 21:59:53 +00:00
|
|
|
return ret;
|
2010-02-10 17:20:50 +00:00
|
|
|
}
|
|
|
|
pkt->stream_index = st->index;
|
|
|
|
ptr = pkt->data;
|
|
|
|
|
|
|
|
for (i = 0; i < frames; i++) {
|
|
|
|
uint8_t toc = buf[1 + i];
|
|
|
|
int frame_size = frame_sizes[(toc >> 3) & 0x0f];
|
|
|
|
|
|
|
|
if (speech_data + frame_size > buf + len) {
|
|
|
|
/* Too little speech data */
|
|
|
|
av_log(ctx, AV_LOG_WARNING, "Too little speech data in the RTP packet\n");
|
|
|
|
/* Set the unwritten part of the packet to zero. */
|
|
|
|
memset(ptr, 0, pkt->data + pkt->size - ptr);
|
|
|
|
pkt->size = ptr - pkt->data;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Extract the AMR frame mode from the TOC byte */
|
|
|
|
*ptr++ = toc & 0x7C;
|
|
|
|
|
|
|
|
/* Copy the speech data */
|
|
|
|
memcpy(ptr, speech_data, frame_size);
|
|
|
|
speech_data += frame_size;
|
|
|
|
ptr += frame_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (speech_data < buf + len) {
|
|
|
|
av_log(ctx, AV_LOG_WARNING, "Too much speech data in the RTP packet?\n");
|
|
|
|
/* Set the unwritten part of the packet to zero. */
|
|
|
|
memset(ptr, 0, pkt->data + pkt->size - ptr);
|
|
|
|
pkt->size = ptr - pkt->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-05 07:01:07 +00:00
|
|
|
static int amr_parse_fmtp(AVFormatContext *s,
|
|
|
|
AVStream *stream, PayloadContext *data,
|
2015-02-24 10:32:17 +00:00
|
|
|
const char *attr, const char *value)
|
2010-02-10 17:20:50 +00:00
|
|
|
{
|
2010-06-28 20:37:16 +00:00
|
|
|
/* Some AMR SDP configurations contain "octet-align", without
|
|
|
|
* the trailing =1. Therefore, if the value is empty,
|
|
|
|
* interpret it as "1".
|
|
|
|
*/
|
|
|
|
if (!strcmp(value, "")) {
|
2014-07-05 07:01:07 +00:00
|
|
|
av_log(s, AV_LOG_WARNING, "AMR fmtp attribute %s had "
|
|
|
|
"nonstandard empty value\n", attr);
|
2015-02-24 10:32:17 +00:00
|
|
|
value = "1";
|
2010-06-28 20:37:16 +00:00
|
|
|
}
|
|
|
|
if (!strcmp(attr, "octet-align"))
|
|
|
|
data->octet_align = atoi(value);
|
|
|
|
else if (!strcmp(attr, "crc"))
|
|
|
|
data->crc = atoi(value);
|
|
|
|
else if (!strcmp(attr, "interleaving"))
|
|
|
|
data->interleaving = atoi(value);
|
|
|
|
else if (!strcmp(attr, "channels"))
|
|
|
|
data->channels = atoi(value);
|
2010-06-28 20:35:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int amr_parse_sdp_line(AVFormatContext *s, int st_index,
|
|
|
|
PayloadContext *data, const char *line)
|
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
int ret;
|
|
|
|
|
2012-04-06 20:07:12 +00:00
|
|
|
if (st_index < 0)
|
|
|
|
return 0;
|
|
|
|
|
2010-06-28 20:35:21 +00:00
|
|
|
/* Parse an fmtp line this one:
|
|
|
|
* a=fmtp:97 octet-align=1; interleaving=0
|
|
|
|
* That is, a normal fmtp: line followed by semicolon & space
|
|
|
|
* separated key/value pairs.
|
|
|
|
*/
|
|
|
|
if (av_strstart(line, "fmtp:", &p)) {
|
2014-07-05 07:01:07 +00:00
|
|
|
ret = ff_parse_fmtp(s, s->streams[st_index], data, p, amr_parse_fmtp);
|
2010-06-28 20:35:21 +00:00
|
|
|
if (!data->octet_align || data->crc ||
|
|
|
|
data->interleaving || data->channels != 1) {
|
2010-02-10 17:20:50 +00:00
|
|
|
av_log(s, AV_LOG_ERROR, "Unsupported RTP/AMR configuration!\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2010-06-28 20:35:21 +00:00
|
|
|
return ret;
|
2010-02-10 17:20:50 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-12 21:21:06 +00:00
|
|
|
const RTPDynamicProtocolHandler ff_amr_nb_dynamic_handler = {
|
2010-02-10 17:20:50 +00:00
|
|
|
.enc_name = "AMR",
|
2010-03-30 23:30:55 +00:00
|
|
|
.codec_type = AVMEDIA_TYPE_AUDIO,
|
2012-08-05 09:11:04 +00:00
|
|
|
.codec_id = AV_CODEC_ID_AMR_NB,
|
2015-02-24 10:43:07 +00:00
|
|
|
.priv_data_size = sizeof(PayloadContext),
|
|
|
|
.init = amr_init,
|
2010-02-10 17:20:50 +00:00
|
|
|
.parse_sdp_a_line = amr_parse_sdp_line,
|
|
|
|
.parse_packet = amr_handle_packet,
|
|
|
|
};
|
|
|
|
|
2018-02-12 21:21:06 +00:00
|
|
|
const RTPDynamicProtocolHandler ff_amr_wb_dynamic_handler = {
|
2010-02-10 17:20:50 +00:00
|
|
|
.enc_name = "AMR-WB",
|
2010-03-30 23:30:55 +00:00
|
|
|
.codec_type = AVMEDIA_TYPE_AUDIO,
|
2012-08-05 09:11:04 +00:00
|
|
|
.codec_id = AV_CODEC_ID_AMR_WB,
|
2015-02-24 10:43:07 +00:00
|
|
|
.priv_data_size = sizeof(PayloadContext),
|
|
|
|
.init = amr_init,
|
2010-02-10 17:20:50 +00:00
|
|
|
.parse_sdp_a_line = amr_parse_sdp_line,
|
|
|
|
.parse_packet = amr_handle_packet,
|
|
|
|
};
|