[FFmpeg-devel] [PATCH] RTSP-MS 14/15: ASF packet parsing
Ronald S. Bultje
rsbultje
Fri Apr 17 21:23:30 CEST 2009
Hi,
On Wed, Apr 15, 2009 at 9:24 AM, Ronald S. Bultje <rsbultje at gmail.com> wrote:
> On Thu, Mar 26, 2009 at 4:16 PM, Luca Abeni <lucabe72 at email.it> wrote:
>> But I do not think that the ff_rtp_merge_data_packet() function is ok. What
>> is needed is a buffer where storing data until a frame is complete. And I
>> suspect libavformat already provides this kind of functionality.
>
> Does anyone know if such a function exists in lavf or related?
Ping?
Patch so far attached, this works against current SVN, but only so
because it does a fresh ff_asf_get_packet() for every new RTP payload,
because otherwise we lose sync (see discussion in the 15/15 thread)
because the packet size does not conform to the min/max_pktsize in the
header...
Ronald
-------------- next part --------------
Index: ffmpeg-svn/libavformat/rtp_asf.c
===================================================================
--- ffmpeg-svn.orig/libavformat/rtp_asf.c 2009-04-17 10:11:28.000000000 -0400
+++ ffmpeg-svn/libavformat/rtp_asf.c 2009-04-17 15:19:22.000000000 -0400
@@ -27,6 +27,7 @@
#include <libavutil/base64.h>
#include <libavutil/avstring.h>
+#include <libavcodec/internal.h>
#include "rtp.h"
#include "rtp_asf.h"
#include "rtsp.h"
@@ -47,6 +48,7 @@
rt->asf_ctx = NULL;
}
av_open_input_stream(&rt->asf_ctx, &pb, "", &asf_demuxer, NULL);
+ rt->asf_pb_pos = url_ftell(&pb);
av_free(buf);
rt->asf_ctx->pb = NULL;
}
@@ -79,12 +81,108 @@
return 0;
}
+struct PayloadContext {
+ ByteIOContext pb;
+ char *buf;
+ int pos;
+};
+
+/**< return 0 on packet, no more left, 1 on packet, 1 on partial packet... */
+static int
+asfrtp_parse_packet (AVFormatContext *s, PayloadContext *asf, AVStream *st,
+ AVPacket *pkt, uint32_t *timestamp,
+ const uint8_t *buf, int len, int flags)
+{
+ ByteIOContext *pb = &asf->pb;
+ int res, mflags, len_off;
+ RTSPState *rt = s->priv_data;
+
+ if (!rt->asf_ctx)
+ return -1;
+
+ if (len > 0) {
+ int off;
+
+ if (len < 4)
+ return -1;
+
+ init_put_byte(pb, buf, len, 0, NULL, NULL, NULL, NULL);
+ mflags = get_byte(pb);
+ if (mflags & 0x80)
+ flags |= RTP_FLAG_KEY;
+ len_off = get_be24(pb);
+ if (mflags & 0x20) /* relative timestamp */
+ url_fskip(pb, 4);
+ if (mflags & 0x10) /* has duration */
+ url_fskip(pb, 4);
+ if (mflags & 0x8) /* has location ID */
+ url_fskip(pb, 4);
+ off = url_ftell(pb);
+
+ if (!(mflags & 0x40)) {
+ if ((res = ff_rtp_merge_data_packet(buf + off, len - off, len_off,
+ &asf->buf, &asf->pos, flags)))
+ return res;
+ } else if (len_off != len) {
+ ff_log_missing_feature(s,
+ "RTSP-MS packet splitting", 1);
+ return -1;
+ } else {
+ av_freep(&asf->buf);
+ asf->buf = av_malloc(len - off);
+ asf->pos = len - off;
+ memcpy(asf->buf, buf + off, len - off);
+ }
+
+ init_put_byte(pb, asf->buf, asf->pos, 0, NULL, NULL, NULL, NULL);
+ pb->pos += rt->asf_pb_pos;
+ pb->eof_reached = 0;
+ rt->asf_ctx->pb = pb;
+ if ((res = ff_asf_get_packet(rt->asf_ctx, pb)) < 0)
+ return res;
+ }
+
+ for (;;) {
+ int i;
+
+ res = av_read_packet(rt->asf_ctx, pkt);
+ rt->asf_pb_pos = url_ftell(pb);
+ 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
+ }
+ }
+ av_free_packet(pkt);
+ }
+
+ return res == 1 ? -1 : res;
+}
+
+static PayloadContext *
+asfrtp_new_context (void)
+{
+ return av_mallocz(sizeof(PayloadContext));
+}
+
+static void
+asfrtp_free_context (PayloadContext *asf)
+{
+ av_freep(&asf->buf);
+ av_free(asf);
+}
+
#define RTP_ASF_HANDLER(n, s, t) \
RTPDynamicProtocolHandler ff_ms_rtp_ ## n ## _handler = { \
s, \
t, \
CODEC_ID_NONE, \
asfrtp_parse_sdp_line, \
+ asfrtp_new_context, \
+ asfrtp_free_context, \
+ asfrtp_parse_packet, \
};
RTP_ASF_HANDLER(asf_pfv, "x-asf-pf", CODEC_TYPE_VIDEO);
Index: ffmpeg-svn/libavformat/rtpdec.c
===================================================================
--- ffmpeg-svn.orig/libavformat/rtpdec.c 2009-04-17 10:11:28.000000000 -0400
+++ ffmpeg-svn/libavformat/rtpdec.c 2009-04-17 10:11:39.000000000 -0400
@@ -559,3 +559,22 @@
}
av_free(s);
}
+
+int ff_rtp_merge_data_packet(const char *ibuf, int ilen, int ioff,
+ char **obuf, int *olen, int flags)
+{
+ if (ioff == 0) {
+ av_freep(*obuf);
+ *olen = 0;
+ *obuf = av_malloc(ilen);
+ } else if (ioff == *olen) {
+ *obuf = av_realloc(*obuf, *olen + ilen);
+ } else {
+ av_freep(*obuf);
+ *olen = 0;
+ return -1;
+ }
+ memcpy(*obuf + *olen, ibuf, ilen);
+ *olen += ilen;
+ return !(flags & RTP_FLAG_MARKER);
+}
Index: ffmpeg-svn/libavformat/rtp.h
===================================================================
--- ffmpeg-svn.orig/libavformat/rtp.h 2009-04-17 10:11:28.000000000 -0400
+++ ffmpeg-svn/libavformat/rtp.h 2009-04-17 10:11:39.000000000 -0400
@@ -68,6 +68,23 @@
*/
enum CodecID ff_rtp_codec_id(const char *buf, enum CodecType codec_type);
+/**
+ * Add RTP packet data into a collection buffer. The input data offset
+ * is checked against the existing data length to ensure packet integrity.
+ * @param ibuf input data buffer data, i.e. the RTP packet data minus header
+ * @param ilen length of the input data buffer
+ * @param ioff offset of the input data buffer against the start of the data.
+ * olen will be checked against this value, and the function will
+ * return an error if the two are not the same. -1 if unknown.
+ * @param obuf pointer to output data, may be re-allocated or discarded
+ * @param olen pointer to length of the output data, may be changed
+ * @param flags RTP packet flags, to check whether the RTP marker bit is set.
+ * @return 0 if the packet was completed (RTP marker bit was set), <0 on error
+ * or 1 if more data is needed to complete the RTP packet.
+ */
+int ff_rtp_merge_data_packet(const char *ibuf, int ilen, int ioff,
+ char **obuf, int *olen, int flags);
+
#define RTP_PT_PRIVATE 96
#define RTP_VERSION 2
#define RTP_MAX_SDES 256 /**< maximum text length for SDES */
Index: ffmpeg-svn/libavformat/rtsp.h
===================================================================
--- ffmpeg-svn.orig/libavformat/rtsp.h 2009-04-17 10:11:28.000000000 -0400
+++ ffmpeg-svn/libavformat/rtsp.h 2009-04-17 10:11:39.000000000 -0400
@@ -248,6 +248,10 @@
//@{
/** ASF demuxer context for the embedded ASF stream from WMS servers */
AVFormatContext *asf_ctx;
+
+ /** cache for position of the asf demuxer, since we load a new
+ * data packet in the bytecontext for each incoming RTSP packet. */
+ uint64_t asf_pb_pos;
//@}
} RTSPState;
Index: ffmpeg-svn/Changelog
===================================================================
--- ffmpeg-svn.orig/Changelog 2009-04-17 10:11:28.000000000 -0400
+++ ffmpeg-svn/Changelog 2009-04-17 10:11:39.000000000 -0400
@@ -13,6 +13,7 @@
- RTP packetization of H.263
- RTP packetization of AMR
- RTP depacketization of Vorbis
+- RTP depacketization of ASF and RTSP from WMS servers
More information about the ffmpeg-devel
mailing list