2008-09-03 07:44:58 +03:00
|
|
|
/*
|
|
|
|
* Realmedia RTSP protocol (RDT) support.
|
|
|
|
* Copyright (c) 2007 Ronald S. Bultje
|
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* This file is part of Libav.
|
2008-09-03 07:44:58 +03:00
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2008-09-03 07:44:58 +03: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 19:35:10 +02:00
|
|
|
* Libav is distributed in the hope that it will be useful,
|
2008-09-03 07:44:58 +03: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 19:35:10 +02:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2008-09-03 07:44:58 +03:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 17:45:34 +03:00
|
|
|
* @file
|
2008-09-03 07:44:58 +03:00
|
|
|
* @brief Realmedia RTSP protocol (RDT) support
|
|
|
|
* @author Ronald S. Bultje <rbultje@ronald.bitfreak.net>
|
|
|
|
*/
|
|
|
|
|
2016-04-16 17:58:30 +02:00
|
|
|
#include "libavcodec/bitstream.h"
|
|
|
|
|
2008-09-03 07:44:58 +03:00
|
|
|
#include "avformat.h"
|
|
|
|
#include "libavutil/avstring.h"
|
2009-02-06 12:35:52 +02:00
|
|
|
#include "rtpdec.h"
|
2008-09-03 07:44:58 +03:00
|
|
|
#include "rdt.h"
|
|
|
|
#include "libavutil/base64.h"
|
|
|
|
#include "libavutil/md5.h"
|
|
|
|
#include "rm.h"
|
|
|
|
#include "internal.h"
|
2011-02-20 12:04:13 +02:00
|
|
|
#include "avio_internal.h"
|
2008-09-03 07:44:58 +03:00
|
|
|
|
2008-10-04 07:19:15 +03:00
|
|
|
struct RDTDemuxContext {
|
2008-11-23 18:54:31 +02:00
|
|
|
AVFormatContext *ic; /**< the containing (RTSP) demux context */
|
2008-12-01 00:24:35 +02:00
|
|
|
/** Each RDT stream-set (represented by one RTSPStream) can contain
|
|
|
|
* multiple streams (of the same content, but with possibly different
|
|
|
|
* codecs/bitrates). Each such stream is represented by one AVStream
|
|
|
|
* in the AVFormatContext, and this variable points to the offset in
|
|
|
|
* that array such that the first is the first stream of this set. */
|
|
|
|
AVStream **streams;
|
2016-04-27 19:45:23 +02:00
|
|
|
int n_streams; /**< streams with identical content in this set */
|
2008-10-04 07:19:15 +03:00
|
|
|
void *dynamic_protocol_context;
|
|
|
|
DynamicPayloadPacketHandlerProc parse_packet;
|
2008-11-17 20:10:02 +02:00
|
|
|
uint32_t prev_timestamp;
|
2008-12-01 02:08:42 +02:00
|
|
|
int prev_set_id, prev_stream_id;
|
2008-10-04 07:19:15 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
RDTDemuxContext *
|
2008-11-30 22:39:16 +02:00
|
|
|
ff_rdt_parse_open(AVFormatContext *ic, int first_stream_of_set_idx,
|
2008-10-04 07:19:15 +03:00
|
|
|
void *priv_data, RTPDynamicProtocolHandler *handler)
|
|
|
|
{
|
|
|
|
RDTDemuxContext *s = av_mallocz(sizeof(RDTDemuxContext));
|
|
|
|
if (!s)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
s->ic = ic;
|
2008-12-01 00:24:35 +02:00
|
|
|
s->streams = &ic->streams[first_stream_of_set_idx];
|
|
|
|
do {
|
|
|
|
s->n_streams++;
|
|
|
|
} while (first_stream_of_set_idx + s->n_streams < ic->nb_streams &&
|
2011-02-11 23:36:47 +02:00
|
|
|
s->streams[s->n_streams]->id == s->streams[0]->id);
|
2008-11-15 04:22:47 +02:00
|
|
|
s->prev_set_id = -1;
|
2008-12-01 02:08:42 +02:00
|
|
|
s->prev_stream_id = -1;
|
2008-11-15 04:22:47 +02:00
|
|
|
s->prev_timestamp = -1;
|
2010-01-08 01:05:19 +02:00
|
|
|
s->parse_packet = handler ? handler->parse_packet : NULL;
|
2008-10-04 07:19:15 +03:00
|
|
|
s->dynamic_protocol_context = priv_data;
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ff_rdt_parse_close(RDTDemuxContext *s)
|
|
|
|
{
|
|
|
|
av_free(s);
|
|
|
|
}
|
|
|
|
|
2008-10-04 07:11:12 +03:00
|
|
|
struct PayloadContext {
|
2008-09-07 04:21:24 +03:00
|
|
|
AVFormatContext *rmctx;
|
2010-08-10 01:58:54 +03:00
|
|
|
int nb_rmst;
|
|
|
|
RMStream **rmst;
|
2008-09-07 04:21:24 +03:00
|
|
|
uint8_t *mlti_data;
|
|
|
|
unsigned int mlti_data_size;
|
2015-06-29 23:48:34 +02:00
|
|
|
char buffer[RTP_MAX_PACKET_LENGTH + AV_INPUT_BUFFER_PADDING_SIZE];
|
2008-12-28 07:55:09 +02:00
|
|
|
int audio_pkt_cnt; /**< remaining audio packets in rmdec */
|
2008-10-04 07:11:12 +03:00
|
|
|
};
|
2008-09-07 04:21:24 +03:00
|
|
|
|
2008-09-03 07:44:58 +03:00
|
|
|
void
|
|
|
|
ff_rdt_calc_response_and_checksum(char response[41], char chksum[9],
|
|
|
|
const char *challenge)
|
|
|
|
{
|
|
|
|
int ch_len = strlen (challenge), i;
|
|
|
|
unsigned char zres[16],
|
|
|
|
buf[64] = { 0xa1, 0xe9, 0x14, 0x9d, 0x0e, 0x6b, 0x3b, 0x59 };
|
|
|
|
#define XOR_TABLE_SIZE 37
|
2015-11-11 18:38:43 +02:00
|
|
|
static const unsigned char xor_table[XOR_TABLE_SIZE] = {
|
2008-09-03 07:44:58 +03:00
|
|
|
0x05, 0x18, 0x74, 0xd0, 0x0d, 0x09, 0x02, 0x53,
|
|
|
|
0xc0, 0x01, 0x05, 0x05, 0x67, 0x03, 0x19, 0x70,
|
|
|
|
0x08, 0x27, 0x66, 0x10, 0x10, 0x72, 0x08, 0x09,
|
|
|
|
0x63, 0x11, 0x03, 0x71, 0x08, 0x08, 0x70, 0x02,
|
|
|
|
0x10, 0x57, 0x05, 0x18, 0x54 };
|
|
|
|
|
|
|
|
/* some (length) checks */
|
|
|
|
if (ch_len == 40) /* what a hack... */
|
|
|
|
ch_len = 32;
|
|
|
|
else if (ch_len > 56)
|
|
|
|
ch_len = 56;
|
|
|
|
memcpy(buf + 8, challenge, ch_len);
|
|
|
|
|
|
|
|
/* xor challenge bytewise with xor_table */
|
|
|
|
for (i = 0; i < XOR_TABLE_SIZE; i++)
|
|
|
|
buf[8 + i] ^= xor_table[i];
|
|
|
|
|
|
|
|
av_md5_sum(zres, buf, 64);
|
2010-03-25 09:13:20 +02:00
|
|
|
ff_data_to_hex(response, zres, 16, 1);
|
2008-09-03 07:44:58 +03:00
|
|
|
|
|
|
|
/* add tail */
|
|
|
|
strcpy (response + 32, "01d0a8e3");
|
|
|
|
|
|
|
|
/* calculate checksum */
|
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
chksum[i] = response[i * 4];
|
|
|
|
chksum[8] = 0;
|
|
|
|
}
|
2008-09-07 04:21:24 +03:00
|
|
|
|
|
|
|
static int
|
2008-10-04 07:11:12 +03:00
|
|
|
rdt_load_mdpr (PayloadContext *rdt, AVStream *st, int rule_nr)
|
2008-09-07 04:21:24 +03:00
|
|
|
{
|
2011-02-20 12:04:12 +02:00
|
|
|
AVIOContext pb;
|
2014-12-04 13:00:01 +02:00
|
|
|
unsigned int size;
|
2008-09-07 04:21:24 +03:00
|
|
|
uint32_t tag;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Layout of the MLTI chunk:
|
2010-07-02 15:48:32 +03:00
|
|
|
* 4: MLTI
|
|
|
|
* 2: number of streams
|
2008-09-07 04:21:24 +03:00
|
|
|
* Then for each stream ([number_of_streams] times):
|
2010-07-02 15:48:32 +03:00
|
|
|
* 2: mdpr index
|
|
|
|
* 2: number of mdpr chunks
|
2008-09-07 04:21:24 +03:00
|
|
|
* Then for each mdpr chunk ([number_of_mdpr_chunks] times):
|
2010-07-02 15:48:32 +03:00
|
|
|
* 4: size
|
|
|
|
* [size]: data
|
2008-09-07 04:21:24 +03:00
|
|
|
* we skip MDPR chunks until we reach the one of the stream
|
|
|
|
* we're interested in, and forward that ([size]+[data]) to
|
|
|
|
* the RM demuxer to parse the stream-specific header data.
|
|
|
|
*/
|
|
|
|
if (!rdt->mlti_data)
|
|
|
|
return -1;
|
2011-02-20 12:04:13 +02:00
|
|
|
ffio_init_context(&pb, rdt->mlti_data, rdt->mlti_data_size, 0,
|
2008-11-17 16:23:20 +02:00
|
|
|
NULL, NULL, NULL, NULL);
|
2011-02-21 17:43:01 +02:00
|
|
|
tag = avio_rl32(&pb);
|
2008-09-07 04:21:24 +03:00
|
|
|
if (tag == MKTAG('M', 'L', 'T', 'I')) {
|
|
|
|
int num, chunk_nr;
|
|
|
|
|
|
|
|
/* read index of MDPR chunk numbers */
|
2011-02-21 17:43:01 +02:00
|
|
|
num = avio_rb16(&pb);
|
2008-09-07 04:21:24 +03:00
|
|
|
if (rule_nr < 0 || rule_nr >= num)
|
|
|
|
return -1;
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(&pb, rule_nr * 2);
|
2011-02-21 17:43:01 +02:00
|
|
|
chunk_nr = avio_rb16(&pb);
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(&pb, (num - 1 - rule_nr) * 2);
|
2008-09-07 04:21:24 +03:00
|
|
|
|
|
|
|
/* read MDPR chunks */
|
2011-02-21 17:43:01 +02:00
|
|
|
num = avio_rb16(&pb);
|
2008-09-07 04:21:24 +03:00
|
|
|
if (chunk_nr >= num)
|
|
|
|
return -1;
|
|
|
|
while (chunk_nr--)
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(&pb, avio_rb32(&pb));
|
2011-02-21 17:43:01 +02:00
|
|
|
size = avio_rb32(&pb);
|
2008-09-07 04:21:24 +03:00
|
|
|
} else {
|
|
|
|
size = rdt->mlti_data_size;
|
2011-02-28 15:57:54 +02:00
|
|
|
avio_seek(&pb, 0, SEEK_SET);
|
2008-09-07 04:21:24 +03:00
|
|
|
}
|
2009-01-07 16:45:13 +02:00
|
|
|
if (ff_rm_read_mdpr_codecdata(rdt->rmctx, &pb, st, rdt->rmst[st->index], size) < 0)
|
2008-09-07 04:21:24 +03:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-07 04:25:47 +03:00
|
|
|
/**
|
|
|
|
* Actual data handling.
|
|
|
|
*/
|
|
|
|
|
2008-10-01 15:37:07 +03:00
|
|
|
int
|
|
|
|
ff_rdt_parse_header(const uint8_t *buf, int len,
|
2008-11-17 16:28:12 +02:00
|
|
|
int *pset_id, int *pseq_no, int *pstream_id,
|
|
|
|
int *pis_keyframe, uint32_t *ptimestamp)
|
2008-09-07 04:25:47 +03:00
|
|
|
{
|
2016-04-16 17:58:30 +02:00
|
|
|
BitstreamContext bc;
|
2008-11-17 16:32:01 +02:00
|
|
|
int consumed = 0, set_id, seq_no, stream_id, is_keyframe,
|
|
|
|
len_included, need_reliable;
|
2008-11-17 16:30:31 +02:00
|
|
|
uint32_t timestamp;
|
2008-09-07 04:25:47 +03:00
|
|
|
|
2008-11-15 22:45:26 +02:00
|
|
|
/* skip status packets */
|
|
|
|
while (len >= 5 && buf[1] == 0xFF /* status packet */) {
|
|
|
|
int pkt_len;
|
|
|
|
|
|
|
|
if (!(buf[0] & 0x80))
|
|
|
|
return -1; /* not followed by a data packet */
|
|
|
|
|
|
|
|
pkt_len = AV_RB16(buf+3);
|
|
|
|
buf += pkt_len;
|
|
|
|
len -= pkt_len;
|
|
|
|
consumed += pkt_len;
|
2008-09-07 04:25:47 +03:00
|
|
|
}
|
2008-11-17 16:32:01 +02:00
|
|
|
if (len < 16)
|
2008-10-01 15:37:07 +03:00
|
|
|
return -1;
|
2008-11-14 20:37:58 +02:00
|
|
|
/**
|
|
|
|
* Layout of the header (in bits):
|
|
|
|
* 1: len_included
|
|
|
|
* Flag indicating whether this header includes a length field;
|
|
|
|
* this can be used to concatenate multiple RDT packets in a
|
|
|
|
* single UDP/TCP data frame and is used to precede RDT data
|
|
|
|
* by stream status packets
|
|
|
|
* 1: need_reliable
|
|
|
|
* Flag indicating whether this header includes a "reliable
|
|
|
|
* sequence number"; these are apparently sequence numbers of
|
|
|
|
* data packets alone. For data packets, this flag is always
|
|
|
|
* set, according to the Real documentation [1]
|
|
|
|
* 5: set_id
|
|
|
|
* ID of a set of streams of identical content, possibly with
|
|
|
|
* different codecs or bitrates
|
|
|
|
* 1: is_reliable
|
|
|
|
* Flag set for certain streams deemed less tolerable for packet
|
|
|
|
* loss
|
|
|
|
* 16: seq_no
|
|
|
|
* Packet sequence number; if >=0xFF00, this is a non-data packet
|
|
|
|
* containing stream status info, the second byte indicates the
|
|
|
|
* type of status packet (see wireshark docs / source code [2])
|
|
|
|
* if (len_included) {
|
|
|
|
* 16: packet_len
|
|
|
|
* } else {
|
|
|
|
* packet_len = remainder of UDP/TCP frame
|
|
|
|
* }
|
|
|
|
* 1: is_back_to_back
|
|
|
|
* Back-to-Back flag; used for timing, set for one in every 10
|
|
|
|
* packets, according to the Real documentation [1]
|
|
|
|
* 1: is_slow_data
|
|
|
|
* Slow-data flag; currently unused, according to Real docs [1]
|
|
|
|
* 5: stream_id
|
|
|
|
* ID of the stream within this particular set of streams
|
|
|
|
* 1: is_no_keyframe
|
|
|
|
* Non-keyframe flag (unset if packet belongs to a keyframe)
|
|
|
|
* 32: timestamp (PTS)
|
|
|
|
* if (set_id == 0x1F) {
|
|
|
|
* 16: set_id (extended set-of-streams ID; see set_id)
|
|
|
|
* }
|
|
|
|
* if (need_reliable) {
|
|
|
|
* 16: reliable_seq_no
|
|
|
|
* Reliable sequence number (see need_reliable)
|
|
|
|
* }
|
|
|
|
* if (stream_id == 0x3F) {
|
|
|
|
* 16: stream_id (extended stream ID; see stream_id)
|
|
|
|
* }
|
|
|
|
* [1] https://protocol.helixcommunity.org/files/2005/devdocs/RDT_Feature_Level_20.txt
|
|
|
|
* [2] http://www.wireshark.org/docs/dfref/r/rdt.html and
|
|
|
|
* http://anonsvn.wireshark.org/viewvc/trunk/epan/dissectors/packet-rdt.c
|
|
|
|
*/
|
2016-04-16 17:58:30 +02:00
|
|
|
bitstream_init(&bc, buf, len << 3);
|
|
|
|
len_included = bitstream_read_bit(&bc);
|
|
|
|
need_reliable = bitstream_read_bit(&bc);
|
|
|
|
set_id = bitstream_read(&bc, 5);
|
|
|
|
bitstream_skip(&bc, 1);
|
|
|
|
seq_no = bitstream_read(&bc, 16);
|
2008-11-17 16:32:01 +02:00
|
|
|
if (len_included)
|
2016-04-16 17:58:30 +02:00
|
|
|
bitstream_skip(&bc, 16);
|
|
|
|
bitstream_skip(&bc, 2);
|
|
|
|
stream_id = bitstream_read(&bc, 5);
|
|
|
|
is_keyframe = !bitstream_read_bit(&bc);
|
|
|
|
timestamp = bitstream_read(&bc, 32);
|
2008-11-17 16:32:01 +02:00
|
|
|
if (set_id == 0x1f)
|
2016-04-16 17:58:30 +02:00
|
|
|
set_id = bitstream_read(&bc, 16);
|
2008-11-17 16:32:01 +02:00
|
|
|
if (need_reliable)
|
2016-04-16 17:58:30 +02:00
|
|
|
bitstream_skip(&bc, 16);
|
2008-11-17 16:32:01 +02:00
|
|
|
if (stream_id == 0x1f)
|
2016-04-16 17:58:30 +02:00
|
|
|
stream_id = bitstream_read(&bc, 16);
|
2008-09-07 04:25:47 +03:00
|
|
|
|
2008-11-17 16:30:31 +02:00
|
|
|
if (pset_id) *pset_id = set_id;
|
|
|
|
if (pseq_no) *pseq_no = seq_no;
|
|
|
|
if (pstream_id) *pstream_id = stream_id;
|
|
|
|
if (pis_keyframe) *pis_keyframe = is_keyframe;
|
|
|
|
if (ptimestamp) *ptimestamp = timestamp;
|
|
|
|
|
2016-04-16 17:58:30 +02:00
|
|
|
return consumed + (bitstream_tell(&bc) >> 3);
|
2008-09-07 04:25:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**< return 0 on packet, no more left, 1 on packet, 1 on partial packet... */
|
|
|
|
static int
|
2009-02-06 03:37:19 +02:00
|
|
|
rdt_parse_packet (AVFormatContext *ctx, PayloadContext *rdt, AVStream *st,
|
2008-10-04 07:15:06 +03:00
|
|
|
AVPacket *pkt, uint32_t *timestamp,
|
2012-12-10 15:38:32 +03:00
|
|
|
const uint8_t *buf, int len, uint16_t rtp_seq, int flags)
|
2008-09-07 04:25:47 +03:00
|
|
|
{
|
|
|
|
int seq = 1, res;
|
2011-02-20 12:04:12 +02:00
|
|
|
AVIOContext pb;
|
2008-09-07 04:25:47 +03:00
|
|
|
|
2008-12-13 23:37:27 +02:00
|
|
|
if (rdt->audio_pkt_cnt == 0) {
|
2015-03-04 10:00:08 +02:00
|
|
|
int pos, rmflags;
|
2008-09-07 04:25:47 +03:00
|
|
|
|
2011-02-20 12:04:13 +02:00
|
|
|
ffio_init_context(&pb, buf, len, 0, NULL, NULL, NULL, NULL);
|
2015-03-04 10:00:08 +02:00
|
|
|
rmflags = (flags & RTP_FLAG_KEY) ? 2 : 0;
|
2009-01-07 16:45:13 +02:00
|
|
|
res = ff_rm_parse_packet (rdt->rmctx, &pb, st, rdt->rmst[st->index], len, pkt,
|
2015-03-04 10:00:08 +02:00
|
|
|
&seq, rmflags, *timestamp);
|
2011-03-03 21:11:45 +02:00
|
|
|
pos = avio_tell(&pb);
|
2008-09-07 04:25:47 +03:00
|
|
|
if (res < 0)
|
|
|
|
return res;
|
2009-03-16 14:57:49 +02:00
|
|
|
if (res > 0) {
|
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_id == AV_CODEC_ID_AAC) {
|
2009-03-16 15:01:02 +02:00
|
|
|
memcpy (rdt->buffer, buf + pos, len - pos);
|
2011-02-20 16:03:29 +02:00
|
|
|
rdt->rmctx->pb = avio_alloc_context (rdt->buffer, len - pos, 0,
|
2009-03-16 15:01:02 +02:00
|
|
|
NULL, NULL, NULL, NULL);
|
2009-03-16 14:57:49 +02:00
|
|
|
}
|
|
|
|
goto get_cache;
|
2008-09-07 04:25:47 +03:00
|
|
|
}
|
|
|
|
} else {
|
2009-03-16 14:57:49 +02:00
|
|
|
get_cache:
|
2008-12-28 07:58:23 +02:00
|
|
|
rdt->audio_pkt_cnt =
|
2008-12-28 07:59:11 +02:00
|
|
|
ff_rm_retrieve_cache (rdt->rmctx, rdt->rmctx->pb,
|
2009-01-07 16:45:13 +02:00
|
|
|
st, rdt->rmst[st->index], pkt);
|
2008-12-28 07:55:09 +02:00
|
|
|
if (rdt->audio_pkt_cnt == 0 &&
|
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_id == AV_CODEC_ID_AAC)
|
2008-11-17 16:23:20 +02:00
|
|
|
av_freep(&rdt->rmctx->pb);
|
2008-09-07 04:25:47 +03:00
|
|
|
}
|
|
|
|
pkt->stream_index = st->index;
|
|
|
|
pkt->pts = *timestamp;
|
|
|
|
|
2008-12-28 07:55:09 +02:00
|
|
|
return rdt->audio_pkt_cnt > 0;
|
2008-09-07 04:25:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-10-04 07:19:15 +03:00
|
|
|
ff_rdt_parse_packet(RDTDemuxContext *s, AVPacket *pkt,
|
2010-10-01 20:43:27 +03:00
|
|
|
uint8_t **bufptr, int len)
|
2008-09-07 04:25:47 +03:00
|
|
|
{
|
2010-10-01 20:43:27 +03:00
|
|
|
uint8_t *buf = bufptr ? *bufptr : NULL;
|
2008-11-15 22:41:59 +02:00
|
|
|
int seq_no, flags = 0, stream_id, set_id, is_keyframe;
|
2008-09-07 04:25:47 +03:00
|
|
|
uint32_t timestamp;
|
|
|
|
int rv= 0;
|
|
|
|
|
2008-10-01 15:38:41 +03:00
|
|
|
if (!s->parse_packet)
|
|
|
|
return -1;
|
|
|
|
|
2008-12-01 02:08:42 +02:00
|
|
|
if (!buf && s->prev_stream_id != -1) {
|
2008-09-07 04:25:47 +03:00
|
|
|
/* return the next packets, if any */
|
|
|
|
timestamp= 0; ///< Should not be used if buf is NULL, but should be set to the timestamp of the packet returned....
|
2009-02-06 03:37:19 +02:00
|
|
|
rv= s->parse_packet(s->ic, s->dynamic_protocol_context,
|
2008-12-01 02:08:42 +02:00
|
|
|
s->streams[s->prev_stream_id],
|
2012-12-10 15:38:32 +03:00
|
|
|
pkt, ×tamp, NULL, 0, 0, flags);
|
2008-09-07 04:25:47 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len < 12)
|
|
|
|
return -1;
|
2008-11-15 22:41:59 +02:00
|
|
|
rv = ff_rdt_parse_header(buf, len, &set_id, &seq_no, &stream_id, &is_keyframe, ×tamp);
|
2008-09-07 04:25:47 +03:00
|
|
|
if (rv < 0)
|
|
|
|
return rv;
|
2008-12-01 02:08:42 +02:00
|
|
|
if (is_keyframe &&
|
|
|
|
(set_id != s->prev_set_id || timestamp != s->prev_timestamp ||
|
|
|
|
stream_id != s->prev_stream_id)) {
|
2009-02-26 16:23:05 +02:00
|
|
|
flags |= RTP_FLAG_KEY;
|
2008-11-15 04:22:47 +02:00
|
|
|
s->prev_set_id = set_id;
|
|
|
|
s->prev_timestamp = timestamp;
|
2008-10-01 15:37:07 +03:00
|
|
|
}
|
2008-12-01 02:08:42 +02:00
|
|
|
s->prev_stream_id = stream_id;
|
2008-09-07 04:25:47 +03:00
|
|
|
buf += rv;
|
|
|
|
len -= rv;
|
|
|
|
|
2008-12-01 02:08:42 +02:00
|
|
|
if (s->prev_stream_id >= s->n_streams) {
|
|
|
|
s->prev_stream_id = -1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-02-06 03:37:19 +02:00
|
|
|
rv = s->parse_packet(s->ic, s->dynamic_protocol_context,
|
2008-12-01 02:08:42 +02:00
|
|
|
s->streams[s->prev_stream_id],
|
2012-12-10 15:38:32 +03:00
|
|
|
pkt, ×tamp, buf, len, 0, flags);
|
2008-09-07 04:25:47 +03:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2008-09-07 04:22:18 +03:00
|
|
|
void
|
2008-09-30 16:23:33 +03:00
|
|
|
ff_rdt_subscribe_rule (char *cmd, int size,
|
2008-09-07 04:22:18 +03:00
|
|
|
int stream_nr, int rule_nr)
|
|
|
|
{
|
|
|
|
av_strlcatf(cmd, size, "stream=%d;rule=%d,stream=%d;rule=%d",
|
2008-09-30 16:23:33 +03:00
|
|
|
stream_nr, rule_nr * 2, stream_nr, rule_nr * 2 + 1);
|
|
|
|
}
|
|
|
|
|
2008-09-07 04:21:24 +03:00
|
|
|
static unsigned char *
|
|
|
|
rdt_parse_b64buf (unsigned int *target_len, const char *p)
|
|
|
|
{
|
|
|
|
unsigned char *target;
|
|
|
|
int len = strlen(p);
|
|
|
|
if (*p == '\"') {
|
|
|
|
p++;
|
|
|
|
len -= 2; /* skip embracing " at start/end */
|
|
|
|
}
|
|
|
|
*target_len = len * 3 / 4;
|
2015-06-29 23:48:34 +02:00
|
|
|
target = av_mallocz(*target_len + AV_INPUT_BUFFER_PADDING_SIZE);
|
2014-05-23 16:42:02 +03:00
|
|
|
if (!target)
|
|
|
|
return NULL;
|
2008-09-07 04:21:24 +03:00
|
|
|
av_base64_decode(target, p, *target_len);
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2008-12-06 20:41:17 +02:00
|
|
|
rdt_parse_sdp_line (AVFormatContext *s, int st_index,
|
|
|
|
PayloadContext *rdt, const char *line)
|
2008-09-07 04:21:24 +03:00
|
|
|
{
|
2008-12-06 20:41:17 +02:00
|
|
|
AVStream *stream = s->streams[st_index];
|
2008-09-07 04:21:24 +03:00
|
|
|
const char *p = line;
|
|
|
|
|
|
|
|
if (av_strstart(p, "OpaqueData:buffer;", &p)) {
|
|
|
|
rdt->mlti_data = rdt_parse_b64buf(&rdt->mlti_data_size, p);
|
|
|
|
} else if (av_strstart(p, "StartTime:integer;", &p))
|
|
|
|
stream->first_dts = atoi(p);
|
2009-01-07 16:45:13 +02:00
|
|
|
else if (av_strstart(p, "ASMRuleBook:string;", &p)) {
|
2010-04-02 17:55:01 +03:00
|
|
|
int n, first = -1;
|
2009-01-07 16:45:13 +02:00
|
|
|
|
|
|
|
for (n = 0; n < s->nb_streams; n++)
|
2011-02-11 23:36:47 +02:00
|
|
|
if (s->streams[n]->id == stream->id) {
|
2013-09-18 19:12:36 +03:00
|
|
|
int count = s->streams[n]->index + 1, err;
|
2009-01-07 16:45:13 +02:00
|
|
|
if (first == -1) first = n;
|
2010-08-10 01:58:54 +03:00
|
|
|
if (rdt->nb_rmst < count) {
|
2013-09-18 19:12:36 +03:00
|
|
|
if ((err = av_reallocp(&rdt->rmst,
|
2013-09-26 16:37:02 +03:00
|
|
|
count * sizeof(*rdt->rmst))) < 0) {
|
|
|
|
rdt->nb_rmst = 0;
|
2013-09-18 19:12:36 +03:00
|
|
|
return err;
|
2013-09-26 16:37:02 +03:00
|
|
|
}
|
2013-09-18 19:12:36 +03:00
|
|
|
memset(rdt->rmst + rdt->nb_rmst, 0,
|
|
|
|
(count - rdt->nb_rmst) * sizeof(*rdt->rmst));
|
2010-08-10 01:58:54 +03:00
|
|
|
rdt->nb_rmst = count;
|
|
|
|
}
|
2009-01-07 16:45:13 +02:00
|
|
|
rdt->rmst[s->streams[n]->index] = ff_rm_alloc_rmstream();
|
2015-02-01 20:19:46 +02:00
|
|
|
if (!rdt->rmst[s->streams[n]->index])
|
|
|
|
return AVERROR(ENOMEM);
|
2009-01-07 16:45:13 +02:00
|
|
|
rdt_load_mdpr(rdt, s->streams[n], (n - first) * 2);
|
|
|
|
}
|
|
|
|
}
|
2008-09-07 04:21:24 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-01-07 16:41:40 +02:00
|
|
|
static void
|
|
|
|
real_parse_asm_rule(AVStream *st, const char *p, const char *end)
|
|
|
|
{
|
|
|
|
do {
|
|
|
|
/* can be either averagebandwidth= or AverageBandwidth= */
|
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 (sscanf(p, " %*1[Aa]verage%*1[Bb]andwidth=%d", &st->codecpar->bit_rate) == 1)
|
2009-01-07 16:41:40 +02:00
|
|
|
break;
|
|
|
|
if (!(p = strchr(p, ',')) || p > end)
|
|
|
|
p = end;
|
|
|
|
p++;
|
|
|
|
} while (p < end);
|
|
|
|
}
|
|
|
|
|
2009-01-07 16:38:44 +02:00
|
|
|
static AVStream *
|
|
|
|
add_dstream(AVFormatContext *s, AVStream *orig_st)
|
|
|
|
{
|
|
|
|
AVStream *st;
|
|
|
|
|
2011-06-18 12:43:24 +03:00
|
|
|
if (!(st = avformat_new_stream(s, NULL)))
|
2009-01-07 16:38:44 +02:00
|
|
|
return NULL;
|
2011-06-18 12:43:24 +03:00
|
|
|
st->id = orig_st->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 21:42:52 +03:00
|
|
|
st->codecpar->codec_type = orig_st->codecpar->codec_type;
|
2009-01-07 16:38:44 +02:00
|
|
|
st->first_dts = orig_st->first_dts;
|
|
|
|
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
real_parse_asm_rulebook(AVFormatContext *s, AVStream *orig_st,
|
|
|
|
const char *p)
|
|
|
|
{
|
|
|
|
const char *end;
|
2010-08-10 01:58:54 +03:00
|
|
|
int n_rules = 0, odd = 0;
|
2009-01-07 16:38:44 +02:00
|
|
|
AVStream *st;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The ASMRuleBook contains a list of comma-separated strings per rule,
|
|
|
|
* and each rule is separated by a ;. The last one also has a ; at the
|
|
|
|
* end so we can use it as delimiter.
|
|
|
|
* Every rule occurs twice, once for when the RTSP packet header marker
|
|
|
|
* is set and once for if it isn't. We only read the first because we
|
|
|
|
* don't care much (that's what the "odd" variable is for).
|
|
|
|
* Each rule contains a set of one or more statements, optionally
|
2011-10-05 15:12:42 +03:00
|
|
|
* preceded by a single condition. If there's a condition, the rule
|
2009-01-07 16:38:44 +02:00
|
|
|
* starts with a '#'. Multiple conditions are merged between brackets,
|
|
|
|
* so there are never multiple conditions spread out over separate
|
|
|
|
* statements. Generally, these conditions are bitrate limits (min/max)
|
|
|
|
* for multi-bitrate streams.
|
|
|
|
*/
|
|
|
|
if (*p == '\"') p++;
|
2010-08-10 01:58:54 +03:00
|
|
|
while (1) {
|
2009-01-07 16:38:44 +02:00
|
|
|
if (!(end = strchr(p, ';')))
|
|
|
|
break;
|
|
|
|
if (!odd && end != p) {
|
|
|
|
if (n_rules > 0)
|
|
|
|
st = add_dstream(s, orig_st);
|
|
|
|
else
|
|
|
|
st = orig_st;
|
2010-08-10 01:58:54 +03:00
|
|
|
if (!st)
|
|
|
|
break;
|
2009-01-07 16:41:40 +02:00
|
|
|
real_parse_asm_rule(st, p, end);
|
2009-01-07 16:38:44 +02:00
|
|
|
n_rules++;
|
|
|
|
}
|
|
|
|
p = end + 1;
|
|
|
|
odd ^= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ff_real_parse_sdp_a_line (AVFormatContext *s, int stream_index,
|
|
|
|
const char *line)
|
|
|
|
{
|
|
|
|
const char *p = line;
|
|
|
|
|
|
|
|
if (av_strstart(p, "ASMRuleBook:string;", &p))
|
|
|
|
real_parse_asm_rulebook(s, s->streams[stream_index], p);
|
|
|
|
}
|
|
|
|
|
2015-02-24 12:43:07 +02:00
|
|
|
static av_cold int rdt_init(AVFormatContext *s, int st_index, PayloadContext *rdt)
|
2008-09-07 04:21:24 +03:00
|
|
|
{
|
2015-02-24 12:43:07 +02:00
|
|
|
return avformat_open_input(&rdt->rmctx, "", &ff_rdt_demuxer, NULL);
|
2008-09-07 04:21:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-02-24 17:01:48 +02:00
|
|
|
rdt_close_context (PayloadContext *rdt)
|
2008-09-07 04:21:24 +03:00
|
|
|
{
|
2009-01-07 16:45:13 +02:00
|
|
|
int i;
|
|
|
|
|
2010-08-10 01:58:54 +03:00
|
|
|
for (i = 0; i < rdt->nb_rmst; i++)
|
2009-01-07 16:45:13 +02:00
|
|
|
if (rdt->rmst[i]) {
|
|
|
|
ff_rm_free_rmstream(rdt->rmst[i]);
|
|
|
|
av_freep(&rdt->rmst[i]);
|
|
|
|
}
|
2008-09-07 04:21:24 +03:00
|
|
|
if (rdt->rmctx)
|
2011-12-11 12:38:28 +03:00
|
|
|
avformat_close_input(&rdt->rmctx);
|
2008-09-07 04:21:24 +03:00
|
|
|
av_freep(&rdt->mlti_data);
|
2010-08-10 01:58:54 +03:00
|
|
|
av_freep(&rdt->rmst);
|
2008-09-07 04:21:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#define RDT_HANDLER(n, s, t) \
|
2013-04-20 22:47:15 +03:00
|
|
|
static RTPDynamicProtocolHandler rdt_ ## n ## _handler = { \
|
2009-07-27 17:29:06 +03:00
|
|
|
.enc_name = s, \
|
|
|
|
.codec_type = t, \
|
2012-08-05 12:11:04 +03:00
|
|
|
.codec_id = AV_CODEC_ID_NONE, \
|
2015-02-24 12:43:07 +02:00
|
|
|
.priv_data_size = sizeof(PayloadContext), \
|
|
|
|
.init = rdt_init, \
|
2009-07-27 17:29:06 +03:00
|
|
|
.parse_sdp_a_line = rdt_parse_sdp_line, \
|
2015-02-24 17:01:48 +02:00
|
|
|
.close = rdt_close_context, \
|
2009-07-27 17:29:06 +03:00
|
|
|
.parse_packet = rdt_parse_packet \
|
2011-02-10 14:04:25 +02:00
|
|
|
}
|
2008-09-07 04:21:24 +03:00
|
|
|
|
2010-03-31 02:30:55 +03:00
|
|
|
RDT_HANDLER(live_video, "x-pn-multirate-realvideo-live", AVMEDIA_TYPE_VIDEO);
|
|
|
|
RDT_HANDLER(live_audio, "x-pn-multirate-realaudio-live", AVMEDIA_TYPE_AUDIO);
|
|
|
|
RDT_HANDLER(video, "x-pn-realvideo", AVMEDIA_TYPE_VIDEO);
|
|
|
|
RDT_HANDLER(audio, "x-pn-realaudio", AVMEDIA_TYPE_AUDIO);
|
2008-09-07 04:21:24 +03:00
|
|
|
|
2013-10-28 09:31:13 +03:00
|
|
|
void ff_register_rdt_dynamic_payload_handlers(void)
|
2008-09-07 04:21:24 +03:00
|
|
|
{
|
2013-04-20 22:47:15 +03:00
|
|
|
ff_register_dynamic_payload_handler(&rdt_video_handler);
|
|
|
|
ff_register_dynamic_payload_handler(&rdt_audio_handler);
|
|
|
|
ff_register_dynamic_payload_handler(&rdt_live_video_handler);
|
|
|
|
ff_register_dynamic_payload_handler(&rdt_live_audio_handler);
|
2008-09-07 04:21:24 +03:00
|
|
|
}
|