32f3c541 |
/* |
1ddc176e |
* RTP Depacketization of MP4A-LATM, RFC 3016
* 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
*/
|
199fb402 |
#include "avio_internal.h" |
1ddc176e |
#include "rtpdec_formats.h"
#include "internal.h"
#include "libavutil/avstring.h"
#include "libavcodec/get_bits.h"
struct PayloadContext { |
471fe57e |
AVIOContext *dyn_buf; |
1ddc176e |
uint8_t *buf;
int pos, len;
uint32_t timestamp;
};
|
d594dbec |
static void latm_close_context(PayloadContext *data) |
1ddc176e |
{ |
199fb402 |
ffio_free_dyn_buf(&data->dyn_buf); |
bb858e67 |
av_freep(&data->buf); |
1ddc176e |
}
static int latm_parse_packet(AVFormatContext *ctx, PayloadContext *data,
AVStream *st, AVPacket *pkt, uint32_t *timestamp, |
90c784cc |
const uint8_t *buf, int len, uint16_t seq,
int flags) |
1ddc176e |
{
int ret, cur_len;
if (buf) {
if (!data->dyn_buf || data->timestamp != *timestamp) {
av_freep(&data->buf); |
199fb402 |
ffio_free_dyn_buf(&data->dyn_buf); |
1ddc176e |
data->timestamp = *timestamp; |
b92c5452 |
if ((ret = avio_open_dyn_buf(&data->dyn_buf)) < 0) |
1ddc176e |
return ret;
} |
e9eb8d0b |
avio_write(data->dyn_buf, buf, len); |
1ddc176e |
if (!(flags & RTP_FLAG_MARKER))
return AVERROR(EAGAIN); |
bb858e67 |
av_freep(&data->buf); |
6dc7d80d |
data->len = avio_close_dyn_buf(data->dyn_buf, &data->buf); |
1ddc176e |
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;
}
|
ac0e54fd |
static int parse_fmtp_config(AVStream *st, const char *value) |
1ddc176e |
{
int len = ff_hex_to_data(NULL, value), i, ret = 0;
GetBitContext gb;
uint8_t *config; |
e65ab9d9 |
int audio_mux_version, same_time_framing, num_programs, num_layers; |
1ddc176e |
/* Pad this buffer, too, to avoid out of bounds reads with get_bits below */ |
059a9348 |
config = av_mallocz(len + AV_INPUT_BUFFER_PADDING_SIZE); |
1ddc176e |
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); |
e65ab9d9 |
skip_bits(&gb, 6); /* num_sub_frames */ |
1ddc176e |
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;
}
av_freep(&st->codec->extradata); |
a807c682 |
if (ff_alloc_extradata(st->codec, (get_bits_left(&gb) + 7)/8)) { |
1ddc176e |
ret = AVERROR(ENOMEM);
goto end;
}
for (i = 0; i < st->codec->extradata_size; i++)
st->codec->extradata[i] = get_bits(&gb, 8);
end:
av_free(config);
return ret;
}
|
0307cc22 |
static int parse_fmtp(AVFormatContext *s,
AVStream *stream, PayloadContext *data, |
ec96a89c |
const char *attr, const char *value) |
1ddc176e |
{
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) |
0307cc22 |
avpriv_request_sample(s, |
1ecdf891 |
"RTP MP4A-LATM with in-band configuration"); |
1ddc176e |
}
return 0;
}
static int latm_parse_sdp_line(AVFormatContext *s, int st_index,
PayloadContext *data, const char *line)
{
const char *p;
|
45600148 |
if (st_index < 0)
return 0;
|
1ddc176e |
if (av_strstart(line, "fmtp:", &p)) |
0307cc22 |
return ff_parse_fmtp(s, s->streams[st_index], data, p, parse_fmtp); |
1ddc176e |
return 0;
}
RTPDynamicProtocolHandler ff_mp4a_latm_dynamic_handler = {
.enc_name = "MP4A-LATM",
.codec_type = AVMEDIA_TYPE_AUDIO, |
36ef5369 |
.codec_id = AV_CODEC_ID_AAC, |
5d8cae45 |
.priv_data_size = sizeof(PayloadContext), |
1ddc176e |
.parse_sdp_a_line = latm_parse_sdp_line, |
d594dbec |
.close = latm_close_context, |
3cd5828f |
.parse_packet = latm_parse_packet, |
1ddc176e |
}; |