2010-10-06 15:42:18 +03:00
|
|
|
/*
|
|
|
|
* RTP/Quicktime support.
|
|
|
|
* Copyright (c) 2009 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief Quicktime-style RTP support
|
|
|
|
* @author Ronald S. Bultje <rbultje@ronald.bitfreak.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "avformat.h"
|
2011-11-29 21:28:15 +03:00
|
|
|
#include "internal.h"
|
2011-02-20 12:04:13 +02:00
|
|
|
#include "avio_internal.h"
|
2010-10-06 15:42:18 +03:00
|
|
|
#include "rtp.h"
|
|
|
|
#include "rtpdec.h"
|
|
|
|
#include "isom.h"
|
|
|
|
#include "libavcodec/get_bits.h"
|
|
|
|
|
|
|
|
struct PayloadContext {
|
2021-01-29 16:55:40 +02:00
|
|
|
AVPacket *pkt;
|
2010-10-06 15:42:18 +03:00
|
|
|
int bytes_per_frame, remaining;
|
|
|
|
uint32_t timestamp;
|
|
|
|
};
|
|
|
|
|
2021-01-29 16:55:40 +02:00
|
|
|
static av_cold int qt_rtp_init(AVFormatContext *ctx, int st_index,
|
|
|
|
PayloadContext *qt)
|
|
|
|
{
|
|
|
|
qt->pkt = av_packet_alloc();
|
|
|
|
if (!qt->pkt)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-10-06 15:42:18 +03:00
|
|
|
static int qt_rtp_parse_packet(AVFormatContext *s, PayloadContext *qt,
|
|
|
|
AVStream *st, AVPacket *pkt,
|
|
|
|
uint32_t *timestamp, const uint8_t *buf,
|
2012-12-10 15:38:32 +03:00
|
|
|
int len, uint16_t seq, int flags)
|
2010-10-06 15:42:18 +03:00
|
|
|
{
|
2021-08-04 16:52:07 +02:00
|
|
|
FFIOContext pb0;
|
|
|
|
AVIOContext *const pb = &pb0.pub;
|
2010-10-06 15:42:18 +03:00
|
|
|
GetBitContext gb;
|
|
|
|
int packing_scheme, has_payload_desc, has_packet_info, alen,
|
2015-03-04 09:57:02 +02:00
|
|
|
has_marker_bit = flags & RTP_FLAG_MARKER,
|
2019-12-10 23:59:53 +02:00
|
|
|
keyframe, ret;
|
2010-10-06 15:42:18 +03:00
|
|
|
|
|
|
|
if (qt->remaining) {
|
2021-01-29 16:55:40 +02:00
|
|
|
int num = qt->pkt->size / qt->bytes_per_frame;
|
2010-10-06 15:42:18 +03:00
|
|
|
|
2019-12-10 23:59:53 +02:00
|
|
|
if ((ret = av_new_packet(pkt, qt->bytes_per_frame)) < 0)
|
|
|
|
return ret;
|
2010-10-06 15:42:18 +03:00
|
|
|
pkt->stream_index = st->index;
|
2021-01-29 16:55:40 +02:00
|
|
|
pkt->flags = qt->pkt->flags;
|
2010-10-06 15:42:18 +03:00
|
|
|
memcpy(pkt->data,
|
2021-01-29 16:55:40 +02:00
|
|
|
&qt->pkt->data[(num - qt->remaining) * qt->bytes_per_frame],
|
2010-10-06 15:42:18 +03:00
|
|
|
qt->bytes_per_frame);
|
|
|
|
if (--qt->remaining == 0) {
|
2021-01-29 16:55:40 +02:00
|
|
|
av_freep(&qt->pkt->data);
|
|
|
|
qt->pkt->size = 0;
|
2010-10-06 15:42:18 +03:00
|
|
|
}
|
|
|
|
return qt->remaining > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The RTP payload is described in:
|
|
|
|
* http://developer.apple.com/quicktime/icefloe/dispatch026.html
|
|
|
|
*/
|
|
|
|
init_get_bits(&gb, buf, len << 3);
|
2021-08-04 16:52:07 +02:00
|
|
|
ffio_init_context(&pb0, (uint8_t*)buf, len, 0, NULL, NULL, NULL, NULL);
|
2010-10-06 15:42:18 +03:00
|
|
|
|
|
|
|
if (len < 4)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
skip_bits(&gb, 4); // version
|
|
|
|
if ((packing_scheme = get_bits(&gb, 2)) == 0)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2015-03-04 09:57:02 +02:00
|
|
|
keyframe = get_bits1(&gb);
|
2010-10-06 15:42:18 +03:00
|
|
|
has_payload_desc = get_bits1(&gb);
|
|
|
|
has_packet_info = get_bits1(&gb);
|
|
|
|
skip_bits(&gb, 23); // reserved:7, cache payload info:1, payload ID:15
|
|
|
|
|
|
|
|
if (has_payload_desc) {
|
|
|
|
int data_len, pos, is_start, is_finish;
|
|
|
|
uint32_t tag;
|
|
|
|
|
|
|
|
pos = get_bits_count(&gb) >> 3;
|
|
|
|
if (pos + 12 > len)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2016-04-27 19:45:23 +02:00
|
|
|
skip_bits(&gb, 2); // has non-I-frames:1, is sparse:1
|
2010-10-06 15:42:18 +03:00
|
|
|
is_start = get_bits1(&gb);
|
|
|
|
is_finish = get_bits1(&gb);
|
|
|
|
if (!is_start || !is_finish) {
|
2013-02-26 01:54:28 +03:00
|
|
|
avpriv_request_sample(s, "RTP-X-QT with payload description "
|
|
|
|
"split over several packets");
|
2012-10-09 01:41:34 +03:00
|
|
|
return AVERROR_PATCHWELCOME;
|
2010-10-06 15:42:18 +03:00
|
|
|
}
|
|
|
|
skip_bits(&gb, 12); // reserved
|
|
|
|
data_len = get_bits(&gb, 16);
|
|
|
|
|
2021-08-04 16:52:07 +02:00
|
|
|
avio_seek(pb, pos + 4, SEEK_SET);
|
|
|
|
tag = avio_rl32(pb);
|
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 21:42:52 +03:00
|
|
|
if ((st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
|
2010-10-06 15:42:18 +03:00
|
|
|
tag != MKTAG('v','i','d','e')) ||
|
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 21:42:52 +03:00
|
|
|
(st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
|
2010-10-06 15:42:18 +03:00
|
|
|
tag != MKTAG('s','o','u','n')))
|
|
|
|
return AVERROR_INVALIDDATA;
|
2021-08-04 16:52:07 +02:00
|
|
|
avpriv_set_pts_info(st, 32, 1, avio_rb32(pb));
|
2010-10-06 15:42:18 +03:00
|
|
|
|
|
|
|
if (pos + data_len > len)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
/* TLVs */
|
2021-08-04 16:52:07 +02:00
|
|
|
while (avio_tell(pb) + 4 < pos + data_len) {
|
|
|
|
int tlv_len = avio_rb16(pb);
|
|
|
|
tag = avio_rl16(pb);
|
|
|
|
if (avio_tell(pb) + tlv_len > pos + data_len)
|
2010-10-06 15:42:18 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
#define MKTAG16(a,b) MKTAG(a,b,0,0)
|
|
|
|
switch (tag) {
|
|
|
|
case MKTAG16('s','d'): {
|
|
|
|
MOVStreamContext *msc;
|
|
|
|
void *priv_data = st->priv_data;
|
|
|
|
int nb_streams = s->nb_streams;
|
|
|
|
MOVContext *mc = av_mallocz(sizeof(*mc));
|
|
|
|
if (!mc)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
mc->fc = s;
|
|
|
|
st->priv_data = msc = av_mallocz(sizeof(MOVStreamContext));
|
|
|
|
if (!msc) {
|
|
|
|
av_free(mc);
|
|
|
|
st->priv_data = priv_data;
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
/* ff_mov_read_stsd_entries updates stream s->nb_streams-1,
|
|
|
|
* so set it temporarily to indicate which stream to update. */
|
|
|
|
s->nb_streams = st->index + 1;
|
2021-08-04 16:52:07 +02:00
|
|
|
ff_mov_read_stsd_entries(mc, pb, 1);
|
2010-10-06 15:42:18 +03:00
|
|
|
qt->bytes_per_frame = msc->bytes_per_frame;
|
|
|
|
av_free(msc);
|
|
|
|
av_free(mc);
|
|
|
|
st->priv_data = priv_data;
|
|
|
|
s->nb_streams = nb_streams;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2021-08-04 16:52:07 +02:00
|
|
|
avio_skip(pb, tlv_len);
|
2010-10-06 15:42:18 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 32-bit alignment */
|
2021-08-04 16:52:07 +02:00
|
|
|
avio_skip(pb, ((avio_tell(pb) + 3) & ~3) - avio_tell(pb));
|
2010-10-06 15:42:18 +03:00
|
|
|
} else
|
2021-08-04 16:52:07 +02:00
|
|
|
avio_seek(pb, 4, SEEK_SET);
|
2010-10-06 15:42:18 +03:00
|
|
|
|
|
|
|
if (has_packet_info) {
|
2013-02-26 01:54:28 +03:00
|
|
|
avpriv_request_sample(s, "RTP-X-QT with packet-specific info");
|
2012-10-09 01:41:34 +03:00
|
|
|
return AVERROR_PATCHWELCOME;
|
2010-10-06 15:42:18 +03:00
|
|
|
}
|
|
|
|
|
2021-08-04 16:52:07 +02:00
|
|
|
alen = len - avio_tell(pb);
|
2010-10-06 15:42:18 +03:00
|
|
|
if (alen <= 0)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
switch (packing_scheme) {
|
|
|
|
case 3: /* one data packet spread over 1 or multiple RTP packets */
|
2021-01-29 16:55:40 +02:00
|
|
|
if (qt->pkt->size > 0 && qt->timestamp == *timestamp) {
|
2013-09-18 19:12:36 +03:00
|
|
|
int err;
|
2021-01-29 16:55:40 +02:00
|
|
|
if ((err = av_reallocp(&qt->pkt->data, qt->pkt->size + alen +
|
2015-06-29 23:48:34 +02:00
|
|
|
AV_INPUT_BUFFER_PADDING_SIZE)) < 0) {
|
2021-01-29 16:55:40 +02:00
|
|
|
qt->pkt->size = 0;
|
2013-09-18 19:12:36 +03:00
|
|
|
return err;
|
2013-09-26 16:37:02 +03:00
|
|
|
}
|
2010-10-06 15:42:18 +03:00
|
|
|
} else {
|
2021-01-29 16:55:40 +02:00
|
|
|
av_freep(&qt->pkt->data);
|
|
|
|
av_packet_unref(qt->pkt);
|
|
|
|
qt->pkt->data = av_realloc(NULL, alen + AV_INPUT_BUFFER_PADDING_SIZE);
|
|
|
|
if (!qt->pkt->data)
|
2013-09-26 16:34:59 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2021-01-29 16:55:40 +02:00
|
|
|
qt->pkt->size = 0;
|
2010-10-06 15:42:18 +03:00
|
|
|
qt->timestamp = *timestamp;
|
|
|
|
}
|
2021-08-04 16:52:07 +02:00
|
|
|
memcpy(qt->pkt->data + qt->pkt->size, buf + avio_tell(pb), alen);
|
2021-01-29 16:55:40 +02:00
|
|
|
qt->pkt->size += alen;
|
2010-10-06 15:42:18 +03:00
|
|
|
if (has_marker_bit) {
|
2021-01-29 16:55:40 +02:00
|
|
|
int ret = av_packet_from_data(pkt, qt->pkt->data, qt->pkt->size);
|
2012-10-31 10:53:18 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2021-01-29 16:55:40 +02:00
|
|
|
qt->pkt->size = 0;
|
|
|
|
qt->pkt->data = NULL;
|
2015-03-04 09:57:02 +02:00
|
|
|
pkt->flags = keyframe ? AV_PKT_FLAG_KEY : 0;
|
2010-10-06 15:42:18 +03:00
|
|
|
pkt->stream_index = st->index;
|
2015-06-29 23:48:34 +02:00
|
|
|
memset(pkt->data + pkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
|
2010-10-06 15:42:18 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return AVERROR(EAGAIN);
|
|
|
|
|
|
|
|
case 1: /* constant packet size, multiple packets per RTP packet */
|
|
|
|
if (qt->bytes_per_frame == 0 ||
|
|
|
|
alen % qt->bytes_per_frame != 0)
|
|
|
|
return AVERROR_INVALIDDATA; /* wrongly padded */
|
|
|
|
qt->remaining = (alen / qt->bytes_per_frame) - 1;
|
2019-12-10 23:59:53 +02:00
|
|
|
if ((ret = av_new_packet(pkt, qt->bytes_per_frame)) < 0)
|
|
|
|
return ret;
|
2021-08-04 16:52:07 +02:00
|
|
|
memcpy(pkt->data, buf + avio_tell(pb), qt->bytes_per_frame);
|
2015-03-04 09:57:02 +02:00
|
|
|
pkt->flags = keyframe ? AV_PKT_FLAG_KEY : 0;
|
2010-10-06 15:42:18 +03:00
|
|
|
pkt->stream_index = st->index;
|
|
|
|
if (qt->remaining > 0) {
|
2021-01-29 16:55:40 +02:00
|
|
|
av_freep(&qt->pkt->data);
|
|
|
|
qt->pkt->data = av_realloc(NULL, qt->remaining * qt->bytes_per_frame);
|
|
|
|
if (!qt->pkt->data) {
|
2015-10-23 11:11:31 +02:00
|
|
|
av_packet_unref(pkt);
|
2010-10-06 15:42:18 +03:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
2021-01-29 16:55:40 +02:00
|
|
|
qt->pkt->size = qt->remaining * qt->bytes_per_frame;
|
|
|
|
memcpy(qt->pkt->data,
|
2021-08-04 16:52:07 +02:00
|
|
|
buf + avio_tell(pb) + qt->bytes_per_frame,
|
2010-10-06 15:42:18 +03:00
|
|
|
qt->remaining * qt->bytes_per_frame);
|
2021-01-29 16:55:40 +02:00
|
|
|
qt->pkt->flags = pkt->flags;
|
2010-10-06 15:42:18 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
default: /* unimplemented */
|
2013-02-26 01:54:28 +03:00
|
|
|
avpriv_request_sample(NULL, "RTP-X-QT with packing scheme 2");
|
2012-10-09 01:41:34 +03:00
|
|
|
return AVERROR_PATCHWELCOME;
|
2010-10-06 15:42:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-24 17:01:48 +02:00
|
|
|
static void qt_rtp_close(PayloadContext *qt)
|
2010-10-06 15:42:18 +03:00
|
|
|
{
|
2021-01-29 16:55:40 +02:00
|
|
|
av_freep(&qt->pkt->data);
|
|
|
|
av_packet_free(&qt->pkt);
|
2010-10-06 15:42:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#define RTP_QT_HANDLER(m, n, s, t) \
|
2020-12-30 14:39:09 +02:00
|
|
|
const RTPDynamicProtocolHandler ff_ ## m ## _rtp_ ## n ## _handler = { \
|
2010-11-29 18:04:04 +02:00
|
|
|
.enc_name = s, \
|
|
|
|
.codec_type = t, \
|
2012-08-05 12:11:04 +03:00
|
|
|
.codec_id = AV_CODEC_ID_NONE, \
|
2015-02-23 22:18:32 +02:00
|
|
|
.priv_data_size = sizeof(PayloadContext), \
|
2021-01-29 16:55:40 +02:00
|
|
|
.init = qt_rtp_init, \
|
2015-02-24 17:01:48 +02:00
|
|
|
.close = qt_rtp_close, \
|
2010-11-29 18:04:04 +02:00
|
|
|
.parse_packet = qt_rtp_parse_packet, \
|
2011-02-10 14:04:25 +02:00
|
|
|
}
|
2010-10-06 15:42:18 +03:00
|
|
|
|
2011-02-03 15:34:00 +02:00
|
|
|
RTP_QT_HANDLER(qt, vid, "X-QT", AVMEDIA_TYPE_VIDEO);
|
|
|
|
RTP_QT_HANDLER(qt, aud, "X-QT", AVMEDIA_TYPE_AUDIO);
|
|
|
|
RTP_QT_HANDLER(quicktime, vid, "X-QUICKTIME", AVMEDIA_TYPE_VIDEO);
|
|
|
|
RTP_QT_HANDLER(quicktime, aud, "X-QUICKTIME", AVMEDIA_TYPE_AUDIO);
|