2015-02-13 23:51:33 +02:00
|
|
|
/*
|
|
|
|
* RTP parser for loss tolerant payload format for MP3 audio (RFC 5219)
|
|
|
|
* Copyright (c) 2015 Gilles Chanteperdrix <gch@xenomai.org>
|
|
|
|
*
|
|
|
|
* This file is part of Libav.
|
|
|
|
*
|
|
|
|
* Libav 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.
|
|
|
|
*
|
|
|
|
* Libav 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 Libav; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libavutil/attributes.h"
|
|
|
|
#include "libavutil/intreadwrite.h"
|
|
|
|
|
2015-02-24 13:37:03 +02:00
|
|
|
#include "avio_internal.h"
|
2015-02-13 23:51:33 +02:00
|
|
|
#include "rtpdec_formats.h"
|
|
|
|
|
|
|
|
struct PayloadContext {
|
|
|
|
unsigned adu_size;
|
|
|
|
unsigned cur_size;
|
|
|
|
uint32_t timestamp;
|
|
|
|
uint8_t *split_buf;
|
|
|
|
int split_pos, split_buf_size, split_pkts;
|
|
|
|
AVIOContext *fragment;
|
|
|
|
};
|
|
|
|
|
2015-02-24 17:01:48 +02:00
|
|
|
static void mpa_robust_close_context(PayloadContext *data)
|
2015-02-13 23:51:33 +02:00
|
|
|
{
|
2015-02-24 13:37:03 +02:00
|
|
|
ffio_free_dyn_buf(&data->fragment);
|
2015-02-13 23:51:33 +02:00
|
|
|
av_free(data->split_buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mpa_robust_parse_rtp_header(AVFormatContext *ctx,
|
|
|
|
const uint8_t *buf, int len,
|
|
|
|
unsigned *adu_size, unsigned *cont)
|
|
|
|
{
|
|
|
|
unsigned header_size;
|
|
|
|
|
|
|
|
if (len < 2) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Invalid %d bytes packet\n", len);
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
*cont = !!(buf[0] & 0x80);
|
|
|
|
if (!(buf[0] & 0x40)) {
|
|
|
|
header_size = 1;
|
|
|
|
*adu_size = buf[0] & ~0xc0;
|
|
|
|
} else {
|
|
|
|
header_size = 2;
|
|
|
|
*adu_size = AV_RB16(buf) & ~0xc000;
|
|
|
|
}
|
|
|
|
|
|
|
|
return header_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mpa_robust_parse_packet(AVFormatContext *ctx, PayloadContext *data,
|
|
|
|
AVStream *st, AVPacket *pkt,
|
|
|
|
uint32_t *timestamp, const uint8_t *buf,
|
|
|
|
int len, uint16_t seq, int flags)
|
|
|
|
{
|
|
|
|
unsigned adu_size, continuation;
|
|
|
|
int err, header_size;
|
|
|
|
|
|
|
|
if (!buf) {
|
|
|
|
buf = &data->split_buf[data->split_pos];
|
|
|
|
len = data->split_buf_size - data->split_pos;
|
|
|
|
|
|
|
|
header_size = mpa_robust_parse_rtp_header(ctx, buf, len, &adu_size,
|
|
|
|
&continuation);
|
|
|
|
if (header_size < 0) {
|
|
|
|
av_freep(&data->split_buf);
|
|
|
|
return header_size;
|
|
|
|
}
|
|
|
|
buf += header_size;
|
|
|
|
len -= header_size;
|
|
|
|
|
|
|
|
if (continuation || adu_size > len) {
|
|
|
|
av_freep(&data->split_buf);
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Invalid frame\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (av_new_packet(pkt, adu_size)) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
pkt->stream_index = st->index;
|
|
|
|
memcpy(pkt->data, buf, adu_size);
|
|
|
|
|
2015-03-02 14:46:30 +02:00
|
|
|
data->split_pos += header_size + adu_size;
|
2015-02-13 23:51:33 +02:00
|
|
|
|
|
|
|
if (data->split_pos == data->split_buf_size) {
|
|
|
|
av_freep(&data->split_buf);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
header_size = mpa_robust_parse_rtp_header(ctx, buf, len, &adu_size,
|
|
|
|
&continuation);
|
|
|
|
if (header_size < 0)
|
|
|
|
return header_size;
|
|
|
|
|
|
|
|
buf += header_size;
|
|
|
|
len -= header_size;
|
|
|
|
|
|
|
|
if (!continuation && adu_size <= len) {
|
|
|
|
/* One or more complete frames */
|
|
|
|
|
|
|
|
if (av_new_packet(pkt, adu_size)) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
pkt->stream_index = st->index;
|
|
|
|
memcpy(pkt->data, buf, adu_size);
|
|
|
|
|
|
|
|
buf += adu_size;
|
|
|
|
len -= adu_size;
|
|
|
|
if (len) {
|
|
|
|
data->split_buf_size = len;
|
|
|
|
data->split_buf = av_malloc(data->split_buf_size);
|
|
|
|
data->split_pos = 0;
|
|
|
|
if (!data->split_buf) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
|
|
|
|
av_free_packet(pkt);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
memcpy(data->split_buf, buf, data->split_buf_size);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
} else if (!continuation) { /* && adu_size > len */
|
|
|
|
/* First fragment */
|
2015-02-24 13:37:03 +02:00
|
|
|
ffio_free_dyn_buf(&data->fragment);
|
2015-02-13 23:51:33 +02:00
|
|
|
|
|
|
|
data->adu_size = adu_size;
|
|
|
|
data->cur_size = len;
|
|
|
|
data->timestamp = *timestamp;
|
|
|
|
|
|
|
|
err = avio_open_dyn_buf(&data->fragment);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
avio_write(data->fragment, buf, len);
|
|
|
|
return AVERROR(EAGAIN);
|
|
|
|
}
|
|
|
|
/* else continuation == 1 */
|
|
|
|
|
|
|
|
/* Fragment other than first */
|
|
|
|
if (!data->fragment) {
|
|
|
|
av_log(ctx, AV_LOG_WARNING,
|
|
|
|
"Received packet without a start fragment; dropping.\n");
|
|
|
|
return AVERROR(EAGAIN);
|
|
|
|
}
|
|
|
|
if (adu_size = data->adu_size ||
|
|
|
|
data->timestamp != *timestamp) {
|
2015-02-24 13:37:03 +02:00
|
|
|
ffio_free_dyn_buf(&data->fragment);
|
2015-02-13 23:51:33 +02:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Invalid packet received\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
avio_write(data->fragment, buf, len);
|
|
|
|
data->cur_size += len;
|
|
|
|
|
|
|
|
if (data->cur_size < data->adu_size)
|
|
|
|
return AVERROR(EAGAIN);
|
|
|
|
|
|
|
|
err = ff_rtp_finalize_packet(pkt, &data->fragment, st->index);
|
|
|
|
if (err < 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
|
|
|
"Error occurred when getting fragment buffer.\n");
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
RTPDynamicProtocolHandler ff_mpeg_audio_robust_dynamic_handler = {
|
2015-02-23 21:51:59 +02:00
|
|
|
.enc_name = "mpa-robust",
|
2015-02-13 23:51:33 +02:00
|
|
|
.codec_type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.codec_id = AV_CODEC_ID_MP3ADU,
|
2015-02-23 21:51:05 +02:00
|
|
|
.need_parsing = AVSTREAM_PARSE_HEADERS,
|
2015-02-23 22:18:32 +02:00
|
|
|
.priv_data_size = sizeof(PayloadContext),
|
2015-02-24 17:01:48 +02:00
|
|
|
.close = mpa_robust_close_context,
|
2015-02-13 23:51:33 +02:00
|
|
|
.parse_packet = mpa_robust_parse_packet,
|
|
|
|
};
|