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

Ronald S. Bultje rsbultje
Thu Mar 19 00:29:30 CET 2009


Hi,

On Tue, Feb 3, 2009 at 11:46 AM, Ronald S. Bultje <rsbultje at gmail.com> wrote:
> Oops. I'll resubmit the patch prop'ed up.

Attached patch (sorry it took a while) fixes this.

It also adds a new function ff_rtp_merge_data_packet() as suggested in
another thread (I forgot which one), because this mechanism is shared
by a whole lot of RTP payloads (e.g. X-QT, which I'll submit soon, I
hope). Basically this takes data from a RTP packet and appends it to a
buffer until a marker bit is set on a RTP packet, at which point it is
returned by the demuxer (this is the mechanism by which small UDP/RTP
packets can make up one big video frame, for example).

Ronald
-------------- next part --------------
Index: ffmpeg-svn/libavformat/rtp_asf.c
===================================================================
--- ffmpeg-svn.orig/libavformat/rtp_asf.c	2009-03-17 16:42:53.000000000 -0400
+++ ffmpeg-svn/libavformat/rtp_asf.c	2009-03-18 19:23:30.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-18 19:16:55.000000000 -0400
+++ ffmpeg-svn/libavformat/rtpdec.c	2009-03-18 19:23:30.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-17 08:45:56.000000000 -0400
+++ ffmpeg-svn/libavformat/rtp.h	2009-03-18 19:23:30.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-17 08:45:56.000000000 -0400
+++ ffmpeg-svn/libavformat/rtsp.h	2009-03-18 19:23:30.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