2009-03-17 12:34:57 +00:00
|
|
|
/*
|
|
|
|
* Microsoft RTP/ASF support.
|
|
|
|
* Copyright (c) 2008 Ronald S. Bultje
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 14:45:34 +00:00
|
|
|
* @file
|
2009-03-17 12:34:57 +00:00
|
|
|
* @brief Microsoft RTP/ASF support
|
|
|
|
* @author Ronald S. Bultje <rbultje@ronald.bitfreak.net>
|
|
|
|
*/
|
|
|
|
|
2014-10-23 14:08:51 +00:00
|
|
|
#include "libavutil/avassert.h"
|
2010-07-19 11:43:33 +00:00
|
|
|
#include "libavutil/base64.h"
|
|
|
|
#include "libavutil/avstring.h"
|
|
|
|
#include "libavutil/intreadwrite.h"
|
2009-03-20 01:11:08 +00:00
|
|
|
#include "rtp.h"
|
2010-07-30 12:04:27 +00:00
|
|
|
#include "rtpdec_formats.h"
|
2009-03-17 12:34:57 +00:00
|
|
|
#include "rtsp.h"
|
|
|
|
#include "asf.h"
|
2011-02-20 10:04:13 +00:00
|
|
|
#include "avio_internal.h"
|
2011-11-29 18:28:15 +00:00
|
|
|
#include "internal.h"
|
2009-03-17 12:34:57 +00:00
|
|
|
|
2009-07-27 14:00:10 +00:00
|
|
|
/**
|
|
|
|
* From MSDN 2.2.1.4, we learn that ASF data packets over RTP should not
|
|
|
|
* contain any padding. Unfortunately, the header min/max_pktsize are not
|
|
|
|
* updated (thus making min_pktsize invalid). Here, we "fix" these faulty
|
|
|
|
* min_pktsize values in the ASF file header.
|
|
|
|
* @return 0 on success, <0 on failure (currently -1).
|
|
|
|
*/
|
2009-07-27 14:32:54 +00:00
|
|
|
static int rtp_asf_fix_header(uint8_t *buf, int len)
|
2009-07-27 14:00:10 +00:00
|
|
|
{
|
|
|
|
uint8_t *p = buf, *end = buf + len;
|
|
|
|
|
|
|
|
if (len < sizeof(ff_asf_guid) * 2 + 22 ||
|
|
|
|
memcmp(p, ff_asf_header, sizeof(ff_asf_guid))) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
p += sizeof(ff_asf_guid) + 14;
|
|
|
|
do {
|
|
|
|
uint64_t chunksize = AV_RL64(p + sizeof(ff_asf_guid));
|
2015-04-12 15:41:23 +00:00
|
|
|
int skip = 6 * 8 + 3 * 4 + sizeof(ff_asf_guid) * 2;
|
2009-07-27 14:00:10 +00:00
|
|
|
if (memcmp(p, ff_asf_file_header, sizeof(ff_asf_guid))) {
|
|
|
|
if (chunksize > end - p)
|
|
|
|
return -1;
|
|
|
|
p += chunksize;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-04-12 15:41:23 +00:00
|
|
|
if (end - p < 8 + skip)
|
|
|
|
break;
|
2009-07-27 14:00:10 +00:00
|
|
|
/* skip most of the file header, to min_pktsize */
|
2015-04-12 15:41:23 +00:00
|
|
|
p += skip;
|
|
|
|
if (AV_RL32(p) == AV_RL32(p + 4)) {
|
2009-07-27 14:00:10 +00:00
|
|
|
/* and set that to zero */
|
|
|
|
AV_WL32(p, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
} while (end - p >= sizeof(ff_asf_guid) + 8);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-02-20 10:04:12 +00:00
|
|
|
* The following code is basically a buffered AVIOContext,
|
2009-07-27 14:00:10 +00:00
|
|
|
* with the added benefit of returning -EAGAIN (instead of 0)
|
|
|
|
* on packet boundaries, such that the ASF demuxer can return
|
|
|
|
* safely and resume business at the next packet.
|
|
|
|
*/
|
2009-07-27 14:32:54 +00:00
|
|
|
static int packetizer_read(void *opaque, uint8_t *buf, int buf_size)
|
2009-07-27 14:00:10 +00:00
|
|
|
{
|
|
|
|
return AVERROR(EAGAIN);
|
|
|
|
}
|
|
|
|
|
2011-02-20 10:04:12 +00:00
|
|
|
static void init_packetizer(AVIOContext *pb, uint8_t *buf, int len)
|
2009-07-27 14:00:10 +00:00
|
|
|
{
|
2011-02-20 10:04:13 +00:00
|
|
|
ffio_init_context(pb, buf, len, 0, NULL, packetizer_read, NULL, NULL);
|
2009-07-27 14:00:10 +00:00
|
|
|
|
|
|
|
/* this "fills" the buffer with its current content */
|
|
|
|
pb->pos = len;
|
|
|
|
pb->buf_end = buf + len;
|
|
|
|
}
|
|
|
|
|
2010-07-05 16:46:00 +00:00
|
|
|
int ff_wms_parse_sdp_a_line(AVFormatContext *s, const char *p)
|
2009-03-17 12:34:57 +00:00
|
|
|
{
|
2010-07-05 16:46:00 +00:00
|
|
|
int ret = 0;
|
2009-03-17 12:34:57 +00:00
|
|
|
if (av_strstart(p, "pgmpu:data:application/vnd.ms.wms-hdr.asfv1;base64,", &p)) {
|
2016-07-23 21:47:39 +00:00
|
|
|
AVIOContext pb = { 0 };
|
2009-03-17 12:34:57 +00:00
|
|
|
RTSPState *rt = s->priv_data;
|
2012-04-06 13:58:48 +00:00
|
|
|
AVDictionary *opts = NULL;
|
2009-03-17 12:34:57 +00:00
|
|
|
int len = strlen(p) * 6 / 8;
|
|
|
|
char *buf = av_mallocz(len);
|
2019-03-20 17:52:38 +00:00
|
|
|
ff_const59 AVInputFormat *iformat;
|
2014-10-25 14:28:03 +00:00
|
|
|
|
2015-06-03 13:16:50 +00:00
|
|
|
if (!buf)
|
|
|
|
return AVERROR(ENOMEM);
|
2009-03-17 12:34:57 +00:00
|
|
|
av_base64_decode(buf, p, len);
|
|
|
|
|
2009-07-27 14:00:10 +00:00
|
|
|
if (rtp_asf_fix_header(buf, len) < 0)
|
|
|
|
av_log(s, AV_LOG_ERROR,
|
|
|
|
"Failed to fix invalid RTSP-MS/ASF min_pktsize\n");
|
|
|
|
init_packetizer(&pb, buf, len);
|
2009-03-17 12:34:57 +00:00
|
|
|
if (rt->asf_ctx) {
|
2011-12-11 09:38:28 +00:00
|
|
|
avformat_close_input(&rt->asf_ctx);
|
2009-03-17 12:34:57 +00:00
|
|
|
}
|
2015-06-08 21:05:01 +00:00
|
|
|
|
2014-10-25 14:28:03 +00:00
|
|
|
if (!(iformat = av_find_input_format("asf")))
|
|
|
|
return AVERROR_DEMUXER_NOT_FOUND;
|
2015-06-08 21:05:01 +00:00
|
|
|
|
2015-06-03 13:16:50 +00:00
|
|
|
rt->asf_ctx = avformat_alloc_context();
|
|
|
|
if (!rt->asf_ctx) {
|
|
|
|
av_free(buf);
|
2011-06-04 15:36:30 +00:00
|
|
|
return AVERROR(ENOMEM);
|
2015-06-03 13:16:50 +00:00
|
|
|
}
|
2011-06-04 15:36:30 +00:00
|
|
|
rt->asf_ctx->pb = &pb;
|
2012-04-06 13:58:48 +00:00
|
|
|
av_dict_set(&opts, "no_resync_search", "1", 0);
|
2014-10-23 14:08:51 +00:00
|
|
|
|
2016-03-03 17:14:26 +00:00
|
|
|
if ((ret = ff_copy_whiteblacklists(rt->asf_ctx, s)) < 0) {
|
2014-10-24 17:23:23 +00:00
|
|
|
av_dict_free(&opts);
|
|
|
|
return ret;
|
|
|
|
}
|
2014-10-23 14:08:51 +00:00
|
|
|
|
2014-10-25 14:28:03 +00:00
|
|
|
ret = avformat_open_input(&rt->asf_ctx, "", iformat, &opts);
|
2012-04-06 13:58:48 +00:00
|
|
|
av_dict_free(&opts);
|
2015-06-03 13:16:50 +00:00
|
|
|
if (ret < 0) {
|
2017-11-06 16:25:18 +00:00
|
|
|
av_free(pb.buffer);
|
2010-07-05 16:46:00 +00:00
|
|
|
return ret;
|
2015-06-03 13:16:50 +00:00
|
|
|
}
|
2011-05-22 10:46:29 +00:00
|
|
|
av_dict_copy(&s->metadata, rt->asf_ctx->metadata, 0);
|
2011-03-03 19:11:45 +00:00
|
|
|
rt->asf_pb_pos = avio_tell(&pb);
|
2017-11-06 16:25:18 +00:00
|
|
|
av_free(pb.buffer);
|
2009-03-17 12:34:57 +00:00
|
|
|
rt->asf_ctx->pb = NULL;
|
|
|
|
}
|
2010-07-05 16:46:00 +00:00
|
|
|
return ret;
|
2009-03-17 12:34:57 +00:00
|
|
|
}
|
2009-03-20 01:11:08 +00:00
|
|
|
|
2009-07-27 14:32:54 +00:00
|
|
|
static int asfrtp_parse_sdp_line(AVFormatContext *s, int stream_index,
|
|
|
|
PayloadContext *asf, const char *line)
|
2009-03-20 01:11:08 +00:00
|
|
|
{
|
2012-04-06 20:07:12 +00:00
|
|
|
if (stream_index < 0)
|
|
|
|
return 0;
|
2009-03-20 01:11:08 +00:00
|
|
|
if (av_strstart(line, "stream:", &line)) {
|
|
|
|
RTSPState *rt = s->priv_data;
|
|
|
|
|
|
|
|
s->streams[stream_index]->id = strtol(line, NULL, 10);
|
|
|
|
|
|
|
|
if (rt->asf_ctx) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < rt->asf_ctx->nb_streams; i++) {
|
|
|
|
if (s->streams[stream_index]->id == rt->asf_ctx->streams[i]->id) {
|
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
|
|
|
avcodec_parameters_copy(s->streams[stream_index]->codecpar,
|
|
|
|
rt->asf_ctx->streams[i]->codecpar);
|
2014-01-23 12:07:46 +00:00
|
|
|
s->streams[stream_index]->need_parsing =
|
|
|
|
rt->asf_ctx->streams[i]->need_parsing;
|
2011-11-29 18:28:15 +00:00
|
|
|
avpriv_set_pts_info(s->streams[stream_index], 32, 1, 1000);
|
2009-03-20 01:11:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-07-27 14:00:10 +00:00
|
|
|
struct PayloadContext {
|
2011-02-20 10:04:12 +00:00
|
|
|
AVIOContext *pktbuf, pb;
|
2010-06-27 09:07:35 +00:00
|
|
|
uint8_t *buf;
|
2009-07-27 14:00:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return 0 when a packet was written into /p pkt, and no more data is left;
|
|
|
|
* 1 when a packet was written into /p pkt, and more packets might be left;
|
|
|
|
* <0 when not enough data was provided to return a full packet, or on error.
|
|
|
|
*/
|
2009-07-27 14:32:54 +00:00
|
|
|
static int asfrtp_parse_packet(AVFormatContext *s, PayloadContext *asf,
|
|
|
|
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)
|
2009-07-27 14:00:10 +00:00
|
|
|
{
|
2011-02-20 10:04:12 +00:00
|
|
|
AVIOContext *pb = &asf->pb;
|
2009-07-27 14:00:10 +00:00
|
|
|
int res, mflags, len_off;
|
|
|
|
RTSPState *rt = s->priv_data;
|
|
|
|
|
|
|
|
if (!rt->asf_ctx)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (len > 0) {
|
2010-07-21 17:17:35 +00:00
|
|
|
int off, out_len = 0;
|
2009-07-27 14:00:10 +00:00
|
|
|
|
|
|
|
if (len < 4)
|
|
|
|
return -1;
|
|
|
|
|
2010-07-21 17:17:35 +00:00
|
|
|
av_freep(&asf->buf);
|
|
|
|
|
2014-08-27 21:53:53 +00:00
|
|
|
ffio_init_context(pb, (uint8_t *)buf, len, 0, NULL, NULL, NULL, NULL);
|
2010-07-21 17:17:35 +00:00
|
|
|
|
2011-03-03 19:11:45 +00:00
|
|
|
while (avio_tell(pb) + 4 < len) {
|
|
|
|
int start_off = avio_tell(pb);
|
2010-07-21 17:25:09 +00:00
|
|
|
|
2011-02-21 15:43:01 +00:00
|
|
|
mflags = avio_r8(pb);
|
|
|
|
len_off = avio_rb24(pb);
|
2010-07-21 17:25:09 +00:00
|
|
|
if (mflags & 0x20) /**< relative timestamp */
|
2011-03-15 08:14:38 +00:00
|
|
|
avio_skip(pb, 4);
|
2010-07-21 17:25:09 +00:00
|
|
|
if (mflags & 0x10) /**< has duration */
|
2011-03-15 08:14:38 +00:00
|
|
|
avio_skip(pb, 4);
|
2010-07-21 17:25:09 +00:00
|
|
|
if (mflags & 0x8) /**< has location ID */
|
2011-03-15 08:14:38 +00:00
|
|
|
avio_skip(pb, 4);
|
2011-03-03 19:11:45 +00:00
|
|
|
off = avio_tell(pb);
|
2010-07-21 17:25:09 +00:00
|
|
|
|
|
|
|
if (!(mflags & 0x40)) {
|
|
|
|
/**
|
|
|
|
* If 0x40 is not set, the len_off field specifies an offset
|
|
|
|
* of this packet's payload data in the complete (reassembled)
|
|
|
|
* ASF packet. This is used to spread one ASF packet over
|
|
|
|
* multiple RTP packets.
|
|
|
|
*/
|
2011-03-03 19:11:45 +00:00
|
|
|
if (asf->pktbuf && len_off != avio_tell(asf->pktbuf)) {
|
2015-02-24 11:37:03 +00:00
|
|
|
ffio_free_dyn_buf(&asf->pktbuf);
|
2010-07-21 17:25:09 +00:00
|
|
|
}
|
|
|
|
if (!len_off && !asf->pktbuf &&
|
2011-03-17 07:13:34 +00:00
|
|
|
(res = avio_open_dyn_buf(&asf->pktbuf)) < 0)
|
2010-07-21 17:25:09 +00:00
|
|
|
return res;
|
|
|
|
if (!asf->pktbuf)
|
|
|
|
return AVERROR(EIO);
|
|
|
|
|
2011-02-21 18:28:17 +00:00
|
|
|
avio_write(asf->pktbuf, buf + off, len - off);
|
2011-03-15 08:14:38 +00:00
|
|
|
avio_skip(pb, len - off);
|
2010-07-21 17:25:09 +00:00
|
|
|
if (!(flags & RTP_FLAG_MARKER))
|
|
|
|
return -1;
|
2011-03-17 07:16:07 +00:00
|
|
|
out_len = avio_close_dyn_buf(asf->pktbuf, &asf->buf);
|
2009-07-27 14:00:10 +00:00
|
|
|
asf->pktbuf = NULL;
|
2010-07-21 17:25:09 +00:00
|
|
|
} else {
|
|
|
|
/**
|
|
|
|
* If 0x40 is set, the len_off field specifies the length of
|
|
|
|
* the next ASF packet that can be read from this payload
|
|
|
|
* data alone. This is commonly the same as the payload size,
|
|
|
|
* but could be less in case of packet splitting (i.e.
|
|
|
|
* multiple ASF packets in one RTP packet).
|
|
|
|
*/
|
|
|
|
|
|
|
|
int cur_len = start_off + len_off - off;
|
|
|
|
int prev_len = out_len;
|
|
|
|
out_len += cur_len;
|
2011-09-07 12:12:42 +00:00
|
|
|
if (FFMIN(cur_len, len - off) < 0)
|
2011-09-07 12:12:42 +00:00
|
|
|
return -1;
|
2013-09-18 16:12:36 +00:00
|
|
|
if ((res = av_reallocp(&asf->buf, out_len)) < 0)
|
|
|
|
return res;
|
2010-08-13 20:06:18 +00:00
|
|
|
memcpy(asf->buf + prev_len, buf + off,
|
|
|
|
FFMIN(cur_len, len - off));
|
2011-03-15 08:14:38 +00:00
|
|
|
avio_skip(pb, cur_len);
|
2009-07-27 14:00:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
init_packetizer(pb, asf->buf, out_len);
|
|
|
|
pb->pos += rt->asf_pb_pos;
|
|
|
|
pb->eof_reached = 0;
|
|
|
|
rt->asf_ctx->pb = pb;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
int i;
|
|
|
|
|
2012-03-04 20:41:59 +00:00
|
|
|
res = ff_read_packet(rt->asf_ctx, pkt);
|
2011-03-03 19:11:45 +00:00
|
|
|
rt->asf_pb_pos = avio_tell(pb);
|
2009-07-27 14:00:10 +00:00
|
|
|
if (res != 0)
|
|
|
|
break;
|
|
|
|
for (i = 0; i < s->nb_streams; i++) {
|
|
|
|
if (s->streams[i]->id == rt->asf_ctx->streams[pkt->stream_index]->id) {
|
|
|
|
pkt->stream_index = i;
|
|
|
|
return 1; // FIXME: return 0 if last packet
|
|
|
|
}
|
|
|
|
}
|
2015-10-23 09:11:31 +00:00
|
|
|
av_packet_unref(pkt);
|
2009-07-27 14:00:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res == 1 ? -1 : res;
|
|
|
|
}
|
|
|
|
|
2015-02-24 15:01:48 +00:00
|
|
|
static void asfrtp_close_context(PayloadContext *asf)
|
2009-07-27 14:00:10 +00:00
|
|
|
{
|
2015-02-24 11:37:03 +00:00
|
|
|
ffio_free_dyn_buf(&asf->pktbuf);
|
2009-07-27 14:00:10 +00:00
|
|
|
av_freep(&asf->buf);
|
|
|
|
}
|
|
|
|
|
2009-03-20 01:11:08 +00:00
|
|
|
#define RTP_ASF_HANDLER(n, s, t) \
|
2018-02-12 21:21:06 +00:00
|
|
|
const RTPDynamicProtocolHandler ff_ms_rtp_ ## n ## _handler = { \
|
2009-07-27 14:29:06 +00:00
|
|
|
.enc_name = s, \
|
|
|
|
.codec_type = t, \
|
2012-08-05 09:11:04 +00:00
|
|
|
.codec_id = AV_CODEC_ID_NONE, \
|
2015-02-23 20:18:32 +00:00
|
|
|
.priv_data_size = sizeof(PayloadContext), \
|
2009-07-27 14:29:06 +00:00
|
|
|
.parse_sdp_a_line = asfrtp_parse_sdp_line, \
|
2015-02-24 15:01:48 +00:00
|
|
|
.close = asfrtp_close_context, \
|
2009-07-27 14:29:06 +00:00
|
|
|
.parse_packet = asfrtp_parse_packet, \
|
2011-02-10 12:04:25 +00:00
|
|
|
}
|
2009-03-20 01:11:08 +00:00
|
|
|
|
2010-03-30 23:30:55 +00:00
|
|
|
RTP_ASF_HANDLER(asf_pfv, "x-asf-pf", AVMEDIA_TYPE_VIDEO);
|
|
|
|
RTP_ASF_HANDLER(asf_pfa, "x-asf-pf", AVMEDIA_TYPE_AUDIO);
|