[FFmpeg-devel] [PATCH] RTSP-MS 14/15: ASF packet parsing

Ronald S. Bultje rsbultje
Sat Apr 18 20:40:31 CEST 2009


Hi,

On Sat, Apr 18, 2009 at 1:57 PM, Michael Niedermayer <michaelni at gmx.at> wrote:
> no

Oops, sorry. That last hack is still (only one this time) necessary, I
was hoping to get to that next...

Anyway, first the rest of the patch attached for review.

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-18 10:20:47.000000000 -0400
@@ -27,11 +27,45 @@
 
 #include <libavutil/base64.h>
 #include <libavutil/avstring.h>
+#include <libavcodec/internal.h>
+#include <libavutil/intreadwrite.h>
 #include "rtp.h"
 #include "rtp_asf.h"
 #include "rtsp.h"
 #include "asf.h"
 
+static void
+rtp_asf_fix_header(uint8_t *buf, int len)
+{
+    /**
+     * 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.
+     */
+    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;
+    }
+    p += sizeof(ff_asf_guid) + 14;
+    do {
+        uint64_t len = AV_RL64(p + sizeof(ff_asf_guid));
+        if (memcmp(p, ff_asf_file_header, sizeof(ff_asf_guid))) {
+            p += len;
+            continue;
+        }
+        /* skip most of the file header, to min_pktsize */
+        p += 6 * 8 + 3 * 4 + sizeof(ff_asf_guid) * 2;
+        if (p + 8 <= end && AV_RL32(p) == AV_RL32(p + 4)) {
+            /* and set that to zero */
+            AV_WL32(p, 0);
+        }
+        break;
+    } while (end - p >= sizeof(ff_asf_guid) + 8);
+}
+
 void ff_wms_parse_sdp_a_line(AVFormatContext *s, const char *p)
 {
     if (av_strstart(p, "pgmpu:data:application/vnd.ms.wms-hdr.asfv1;base64,", &p)) {
@@ -41,12 +75,14 @@
         char *buf = av_mallocz(len);
         av_base64_decode(buf, p, len);
 
+        rtp_asf_fix_header(buf, len);
         init_put_byte(&pb, buf, len, 0, NULL, NULL, NULL, NULL);
         if (rt->asf_ctx) {
             av_close_input_stream(rt->asf_ctx);
             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 +115,106 @@
     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;
+    }
+
+    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
 
 
 
Index: ffmpeg-svn/libavformat/asfdec.c
===================================================================
--- ffmpeg-svn.orig/libavformat/asfdec.c	2009-04-17 10:11:28.000000000 -0400
+++ ffmpeg-svn/libavformat/asfdec.c	2009-04-18 10:24:43.000000000 -0400
@@ -572,6 +572,8 @@
     if (c != 0x82) {
         if (!url_feof(pb))
             av_log(s, AV_LOG_ERROR, "ff asf bad header %x  at:%"PRId64"\n", c, url_ftell(pb));
+        else
+            return AVERROR_EOF;
     }
     if ((c & 0x8f) == 0x82) {
         if (d || e) {



More information about the ffmpeg-devel mailing list