2012-02-05 10:31:33 +00:00
|
|
|
/*
|
2010-08-12 21:07:17 +00:00
|
|
|
* RTP Depacketization of MP4A-LATM, RFC 3016
|
|
|
|
* Copyright (c) 2010 Martin Storsjo
|
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* This file is part of Libav.
|
2010-08-12 21:07:17 +00:00
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2010-08-12 21:07:17 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* Libav is distributed in the hope that it will be useful,
|
2010-08-12 21:07:17 +00:00
|
|
|
* 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
|
2011-03-18 17:35:10 +00:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2010-08-12 21:07:17 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2015-02-24 11:37:03 +00:00
|
|
|
#include "avio_internal.h"
|
2010-08-12 21:07:17 +00:00
|
|
|
#include "rtpdec_formats.h"
|
|
|
|
#include "internal.h"
|
|
|
|
#include "libavutil/avstring.h"
|
|
|
|
#include "libavcodec/get_bits.h"
|
|
|
|
|
|
|
|
struct PayloadContext {
|
2011-02-20 10:04:12 +00:00
|
|
|
AVIOContext *dyn_buf;
|
2010-08-12 21:07:17 +00:00
|
|
|
uint8_t *buf;
|
|
|
|
int pos, len;
|
|
|
|
uint32_t timestamp;
|
|
|
|
};
|
|
|
|
|
2015-02-24 15:01:48 +00:00
|
|
|
static void latm_close_context(PayloadContext *data)
|
2010-08-12 21:07:17 +00:00
|
|
|
{
|
2015-02-24 11:37:03 +00:00
|
|
|
ffio_free_dyn_buf(&data->dyn_buf);
|
2010-08-12 21:07:17 +00:00
|
|
|
av_free(data->buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int latm_parse_packet(AVFormatContext *ctx, PayloadContext *data,
|
|
|
|
AVStream *st, AVPacket *pkt, uint32_t *timestamp,
|
2012-12-10 12:38:32 +00:00
|
|
|
const uint8_t *buf, int len, uint16_t seq,
|
|
|
|
int flags)
|
2010-08-12 21:07:17 +00:00
|
|
|
{
|
|
|
|
int ret, cur_len;
|
|
|
|
|
|
|
|
if (buf) {
|
|
|
|
if (!data->dyn_buf || data->timestamp != *timestamp) {
|
|
|
|
av_freep(&data->buf);
|
2015-02-24 11:37:03 +00:00
|
|
|
ffio_free_dyn_buf(&data->dyn_buf);
|
2010-08-12 21:07:17 +00:00
|
|
|
|
|
|
|
data->timestamp = *timestamp;
|
2011-03-17 07:13:34 +00:00
|
|
|
if ((ret = avio_open_dyn_buf(&data->dyn_buf)) < 0)
|
2010-08-12 21:07:17 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2011-02-21 18:28:17 +00:00
|
|
|
avio_write(data->dyn_buf, buf, len);
|
2010-08-12 21:07:17 +00:00
|
|
|
|
|
|
|
if (!(flags & RTP_FLAG_MARKER))
|
|
|
|
return AVERROR(EAGAIN);
|
|
|
|
av_free(data->buf);
|
2011-03-17 07:16:07 +00:00
|
|
|
data->len = avio_close_dyn_buf(data->dyn_buf, &data->buf);
|
2010-08-12 21:07:17 +00:00
|
|
|
data->dyn_buf = NULL;
|
|
|
|
data->pos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!data->buf) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "No data available yet\n");
|
|
|
|
return AVERROR(EIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
cur_len = 0;
|
|
|
|
while (data->pos < data->len) {
|
|
|
|
uint8_t val = data->buf[data->pos++];
|
|
|
|
cur_len += val;
|
|
|
|
if (val != 0xff)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (data->pos + cur_len > data->len) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Malformed LATM packet\n");
|
|
|
|
return AVERROR(EIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ret = av_new_packet(pkt, cur_len)) < 0)
|
|
|
|
return ret;
|
|
|
|
memcpy(pkt->data, data->buf + data->pos, cur_len);
|
|
|
|
data->pos += cur_len;
|
|
|
|
pkt->stream_index = st->index;
|
|
|
|
return data->pos < data->len;
|
|
|
|
}
|
|
|
|
|
2015-02-24 10:31:23 +00:00
|
|
|
static int parse_fmtp_config(AVStream *st, const char *value)
|
2010-08-12 21:07:17 +00:00
|
|
|
{
|
|
|
|
int len = ff_hex_to_data(NULL, value), i, ret = 0;
|
|
|
|
GetBitContext gb;
|
|
|
|
uint8_t *config;
|
2011-06-01 16:26:27 +00:00
|
|
|
int audio_mux_version, same_time_framing, num_programs, num_layers;
|
2010-08-12 21:07:17 +00:00
|
|
|
|
|
|
|
/* Pad this buffer, too, to avoid out of bounds reads with get_bits below */
|
2015-06-29 21:48:34 +00:00
|
|
|
config = av_mallocz(len + AV_INPUT_BUFFER_PADDING_SIZE);
|
2010-08-12 21:07:17 +00:00
|
|
|
if (!config)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
ff_hex_to_data(config, value);
|
|
|
|
init_get_bits(&gb, config, len*8);
|
|
|
|
audio_mux_version = get_bits(&gb, 1);
|
|
|
|
same_time_framing = get_bits(&gb, 1);
|
2011-06-01 16:26:27 +00:00
|
|
|
skip_bits(&gb, 6); /* num_sub_frames */
|
2010-08-12 21:07:17 +00:00
|
|
|
num_programs = get_bits(&gb, 4);
|
|
|
|
num_layers = get_bits(&gb, 3);
|
|
|
|
if (audio_mux_version != 0 || same_time_framing != 1 || num_programs != 0 ||
|
|
|
|
num_layers != 0) {
|
|
|
|
av_log(NULL, AV_LOG_WARNING, "Unsupported LATM config (%d,%d,%d,%d)\n",
|
|
|
|
audio_mux_version, same_time_framing,
|
|
|
|
num_programs, num_layers);
|
|
|
|
ret = AVERROR_PATCHWELCOME;
|
|
|
|
goto end;
|
|
|
|
}
|
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
|
|
|
av_freep(&st->codecpar->extradata);
|
|
|
|
st->codecpar->extradata_size = (get_bits_left(&gb) + 7)/8;
|
|
|
|
st->codecpar->extradata = av_mallocz(st->codecpar->extradata_size +
|
|
|
|
AV_INPUT_BUFFER_PADDING_SIZE);
|
|
|
|
if (!st->codecpar->extradata) {
|
2010-08-12 21:07:17 +00:00
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto end;
|
|
|
|
}
|
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
|
|
|
for (i = 0; i < st->codecpar->extradata_size; i++)
|
|
|
|
st->codecpar->extradata[i] = get_bits(&gb, 8);
|
2010-08-12 21:07:17 +00:00
|
|
|
|
|
|
|
end:
|
|
|
|
av_free(config);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-07-05 07:01:07 +00:00
|
|
|
static int parse_fmtp(AVFormatContext *s,
|
|
|
|
AVStream *stream, PayloadContext *data,
|
2015-02-24 10:32:17 +00:00
|
|
|
const char *attr, const char *value)
|
2010-08-12 21:07:17 +00:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
|
|
|
if (!strcmp(attr, "config")) {
|
|
|
|
res = parse_fmtp_config(stream, value);
|
|
|
|
if (res < 0)
|
|
|
|
return res;
|
|
|
|
} else if (!strcmp(attr, "cpresent")) {
|
|
|
|
int cpresent = atoi(value);
|
|
|
|
if (cpresent != 0)
|
2014-07-05 07:01:07 +00:00
|
|
|
avpriv_request_sample(s,
|
2013-02-25 22:54:28 +00:00
|
|
|
"RTP MP4A-LATM with in-band configuration");
|
2010-08-12 21:07:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int latm_parse_sdp_line(AVFormatContext *s, int st_index,
|
|
|
|
PayloadContext *data, const char *line)
|
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
|
2012-04-06 20:07:12 +00:00
|
|
|
if (st_index < 0)
|
|
|
|
return 0;
|
|
|
|
|
2010-08-12 21:07:17 +00:00
|
|
|
if (av_strstart(line, "fmtp:", &p))
|
2014-07-05 07:01:07 +00:00
|
|
|
return ff_parse_fmtp(s, s->streams[st_index], data, p, parse_fmtp);
|
2010-08-12 21:07:17 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
RTPDynamicProtocolHandler ff_mp4a_latm_dynamic_handler = {
|
|
|
|
.enc_name = "MP4A-LATM",
|
|
|
|
.codec_type = AVMEDIA_TYPE_AUDIO,
|
2012-08-05 09:11:04 +00:00
|
|
|
.codec_id = AV_CODEC_ID_AAC,
|
2015-02-23 20:18:32 +00:00
|
|
|
.priv_data_size = sizeof(PayloadContext),
|
2010-08-12 21:07:17 +00:00
|
|
|
.parse_sdp_a_line = latm_parse_sdp_line,
|
2015-02-24 15:01:48 +00:00
|
|
|
.close = latm_close_context,
|
2015-02-23 20:06:41 +00:00
|
|
|
.parse_packet = latm_parse_packet,
|
2010-08-12 21:07:17 +00:00
|
|
|
};
|