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

Ronald S. Bultje rsbultje
Tue Mar 24 13:17:54 CET 2009


Hi Luca,

On Tue, Mar 24, 2009 at 8:09 AM, Luca Abeni <lucabe72 at email.it> wrote:
> Where can I find the patch? (a link to ml archives would be ok).

Please find it attached.

> I should not comment before reading the actual patch, but...
> Judging from the comments above, I suspect this increases again the
> confusion in the rtp demuxer: currently, the rtp demuxer is not supposed
> to return a full frame, but uses a "parser" provided by libavcodec to
> split the stream in frames. If I understand correctly what you wrote
> above, your patch is changing this behaviour, but only for some payloads...

I'm not sure if that would work here, the RTP/ASF documents on MSDN
and X-QT documents on the Apple website are very clear that splitting
/ merging is a payload feature. I always thought that was what the
marker bit was for.

Ronald
-------------- next part --------------
Index: ffmpeg-svn/libavformat/rtp_asf.c
===================================================================
--- ffmpeg-svn.orig/libavformat/rtp_asf.c	2009-03-19 21:09:22.000000000 -0400
+++ ffmpeg-svn/libavformat/rtp_asf.c	2009-03-21 15:12:02.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,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;
+        if ((res = ff_asf_get_packet(rt->asf_ctx, pb)) < 0)
+            return res;
+    }
+
+    for (;;) {
+        int i;
+
+        res = ff_asf_parse_packet(rt->asf_ctx, pb, 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_extradata (void)
+{
+    return av_mallocz(sizeof(PayloadContext));
+}
+
+static void
+asfrtp_free_extradata (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_extradata,  \
+    asfrtp_free_extradata, \
+    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-03-19 21:09:22.000000000 -0400
+++ ffmpeg-svn/libavformat/rtpdec.c	2009-03-19 22:12:11.000000000 -0400
@@ -557,3 +557,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-03-19 21:09:22.000000000 -0400
+++ ffmpeg-svn/libavformat/rtp.h	2009-03-19 22:12:11.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-03-19 21:09:22.000000000 -0400
+++ ffmpeg-svn/libavformat/rtsp.h	2009-03-21 15:11:00.000000000 -0400
@@ -228,6 +228,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;
 



More information about the ffmpeg-devel mailing list