Add RTP depacketization of the X-QT QuickTime format
authorMartin Storsjö <martin@martin.st>
Wed, 6 Oct 2010 12:42:18 +0000 (12:42 +0000)
committerMartin Storsjö <martin@martin.st>
Wed, 6 Oct 2010 12:42:18 +0000 (12:42 +0000)
Originally committed as revision 25371 to svn://svn.ffmpeg.org/ffmpeg/trunk

Changelog
configure
libavformat/Makefile
libavformat/avformat.h
libavformat/rtpdec.c
libavformat/rtpdec_formats.h
libavformat/rtpdec_qt.c [new file with mode: 0644]

index 13f53a5..b2c72bc 100644 (file)
--- a/Changelog
+++ b/Changelog
@@ -43,6 +43,7 @@ version <next>:
 - yadif filter
 - blackframe filter
 - Demuxer for Leitch/Harris' VR native stream format (LXF)
+- RTP depacketization of the X-QT QuickTime format
 
 
 version 0.6:
index 0b7af01..7cb843f 100755 (executable)
--- a/configure
+++ b/configure
@@ -1363,7 +1363,7 @@ ogg_demuxer_select="golomb"
 psp_muxer_select="mov_muxer"
 rtsp_demuxer_select="http_protocol sdp_demuxer"
 rtsp_muxer_select="rtp_muxer http_protocol sdp_demuxer"
-sdp_demuxer_select="asf_demuxer rm_demuxer rtp_protocol mpegts_demuxer"
+sdp_demuxer_select="asf_demuxer rm_demuxer rtp_protocol mpegts_demuxer mov_demuxer"
 spdif_muxer_select="aac_parser"
 tg2_muxer_select="mov_muxer"
 tgp_muxer_select="mov_muxer"
index b3ae72a..1f1c576 100644 (file)
@@ -240,6 +240,7 @@ OBJS-$(CONFIG_SDP_DEMUXER)               += rtsp.o        \
                                             rtpdec_latm.o \
                                             rtpdec_mpeg4.o \
                                             rtpdec_qdm2.o \
+                                            rtpdec_qt.o   \
                                             rtpdec_svq3.o \
                                             rtpdec_vp8.o  \
                                             rtpdec_xiph.o
index 362a056..feb285b 100644 (file)
@@ -22,7 +22,7 @@
 #define AVFORMAT_AVFORMAT_H
 
 #define LIBAVFORMAT_VERSION_MAJOR 52
-#define LIBAVFORMAT_VERSION_MINOR 79
+#define LIBAVFORMAT_VERSION_MINOR 80
 #define LIBAVFORMAT_VERSION_MICRO  0
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
index d349ce7..8c8d1fa 100644 (file)
@@ -70,6 +70,11 @@ void av_register_rtp_dynamic_payload_handlers(void)
 
     ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfv_handler);
     ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfa_handler);
+
+    ff_register_dynamic_payload_handler(&ff_qt_rtp_aud_handler);
+    ff_register_dynamic_payload_handler(&ff_qt_rtp_vid_handler);
+    ff_register_dynamic_payload_handler(&ff_quicktime_rtp_aud_handler);
+    ff_register_dynamic_payload_handler(&ff_quicktime_rtp_vid_handler);
 }
 
 static int rtcp_parse_packet(RTPDemuxContext *s, const unsigned char *buf, int len)
index 4b30879..aab189e 100644 (file)
@@ -42,6 +42,10 @@ extern RTPDynamicProtocolHandler ff_mpeg4_generic_dynamic_handler;
 extern RTPDynamicProtocolHandler ff_ms_rtp_asf_pfa_handler;
 extern RTPDynamicProtocolHandler ff_ms_rtp_asf_pfv_handler;
 extern RTPDynamicProtocolHandler ff_qdm2_dynamic_handler;
+extern RTPDynamicProtocolHandler ff_qt_rtp_aud_handler;
+extern RTPDynamicProtocolHandler ff_qt_rtp_vid_handler;
+extern RTPDynamicProtocolHandler ff_quicktime_rtp_aud_handler;
+extern RTPDynamicProtocolHandler ff_quicktime_rtp_vid_handler;
 extern RTPDynamicProtocolHandler ff_svq3_dynamic_handler;
 extern RTPDynamicProtocolHandler ff_theora_dynamic_handler;
 extern RTPDynamicProtocolHandler ff_vorbis_dynamic_handler;
diff --git a/libavformat/rtpdec_qt.c b/libavformat/rtpdec_qt.c
new file mode 100644 (file)
index 0000000..7dfc71f
--- /dev/null
@@ -0,0 +1,255 @@
+/*
+ * RTP/Quicktime support.
+ * Copyright (c) 2009 Ronald S. Bultje
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * 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.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * 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
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * @brief Quicktime-style RTP support
+ * @author Ronald S. Bultje <rbultje@ronald.bitfreak.net>
+ */
+
+#include "avformat.h"
+#include "rtp.h"
+#include "rtpdec.h"
+#include "isom.h"
+#include "libavcodec/get_bits.h"
+
+struct PayloadContext {
+    AVPacket pkt;
+    int bytes_per_frame, remaining;
+    uint32_t timestamp;
+};
+
+static int qt_rtp_parse_packet(AVFormatContext *s, PayloadContext *qt,
+                               AVStream *st, AVPacket *pkt,
+                               uint32_t *timestamp, const uint8_t *buf,
+                               int len, int flags)
+{
+    ByteIOContext pb;
+    GetBitContext gb;
+    int packing_scheme, has_payload_desc, has_packet_info, alen,
+        has_marker_bit = flags & RTP_FLAG_MARKER;
+
+    if (qt->remaining) {
+        int num = qt->pkt.size / qt->bytes_per_frame;
+
+        if (av_new_packet(pkt, qt->bytes_per_frame))
+            return AVERROR(ENOMEM);
+        pkt->stream_index = st->index;
+        pkt->flags        = qt->pkt.flags;
+        memcpy(pkt->data,
+               &qt->pkt.data[(num - qt->remaining) * qt->bytes_per_frame],
+               qt->bytes_per_frame);
+        if (--qt->remaining == 0) {
+            av_freep(&qt->pkt.data);
+            qt->pkt.size = 0;
+        }
+        return qt->remaining > 0;
+    }
+
+    /**
+     * The RTP payload is described in:
+     * http://developer.apple.com/quicktime/icefloe/dispatch026.html
+     */
+    init_get_bits(&gb, buf, len << 3);
+    init_put_byte(&pb, buf, len, 0, NULL, NULL, NULL, NULL);
+
+    if (len < 4)
+        return AVERROR_INVALIDDATA;
+
+    skip_bits(&gb, 4); // version
+    if ((packing_scheme = get_bits(&gb, 2)) == 0)
+        return AVERROR_INVALIDDATA;
+    if (get_bits1(&gb))
+        flags          |= RTP_FLAG_KEY;
+    has_payload_desc    = get_bits1(&gb);
+    has_packet_info     = get_bits1(&gb);
+    skip_bits(&gb, 23); // reserved:7, cache payload info:1, payload ID:15
+
+    if (has_payload_desc) {
+        int data_len, pos, is_start, is_finish;
+        uint32_t tag;
+
+        pos = get_bits_count(&gb) >> 3;
+        if (pos + 12 > len)
+            return AVERROR_INVALIDDATA;
+
+        skip_bits(&gb, 2); // has non-I frames:1, is sparse:1
+        is_start  = get_bits1(&gb);
+        is_finish = get_bits1(&gb);
+        if (!is_start || !is_finish) {
+            av_log_missing_feature(s, "RTP-X-QT with payload description "
+                                      "split over several packets", 1);
+            return AVERROR_NOTSUPP;
+        }
+        skip_bits(&gb, 12); // reserved
+        data_len = get_bits(&gb, 16);
+
+        url_fseek(&pb, pos + 4, SEEK_SET);
+        tag = get_le32(&pb);
+        if ((st->codec->codec_type == CODEC_TYPE_VIDEO &&
+                 tag != MKTAG('v','i','d','e')) ||
+            (st->codec->codec_type == CODEC_TYPE_AUDIO &&
+                 tag != MKTAG('s','o','u','n')))
+            return AVERROR_INVALIDDATA;
+        av_set_pts_info(st, 32, 1, get_be32(&pb));
+
+        if (pos + data_len > len)
+            return AVERROR_INVALIDDATA;
+        /* TLVs */
+        while (url_ftell(&pb) + 4 < pos + data_len) {
+            int tlv_len = get_be16(&pb);
+            tag = get_le16(&pb);
+            if (url_ftell(&pb) + tlv_len > pos + data_len)
+                return AVERROR_INVALIDDATA;
+
+#define MKTAG16(a,b) MKTAG(a,b,0,0)
+            switch (tag) {
+            case MKTAG16('s','d'): {
+                MOVStreamContext *msc;
+                void *priv_data = st->priv_data;
+                int nb_streams = s->nb_streams;
+                MOVContext *mc = av_mallocz(sizeof(*mc));
+                if (!mc)
+                    return AVERROR(ENOMEM);
+                mc->fc = s;
+                st->priv_data = msc = av_mallocz(sizeof(MOVStreamContext));
+                if (!msc) {
+                    av_free(mc);
+                    st->priv_data = priv_data;
+                    return AVERROR(ENOMEM);
+                }
+                /* ff_mov_read_stsd_entries updates stream s->nb_streams-1,
+                 * so set it temporarily to indicate which stream to update. */
+                s->nb_streams = st->index + 1;
+                ff_mov_read_stsd_entries(mc, &pb, 1);
+                qt->bytes_per_frame = msc->bytes_per_frame;
+                av_free(msc);
+                av_free(mc);
+                st->priv_data = priv_data;
+                s->nb_streams = nb_streams;
+                break;
+            }
+            default:
+                url_fskip(&pb, tlv_len);
+                break;
+            }
+        }
+
+        /* 32-bit alignment */
+        url_fskip(&pb, ((url_ftell(&pb) + 3) & ~3) - url_ftell(&pb));
+    } else
+        url_fseek(&pb, 4, SEEK_SET);
+
+    if (has_packet_info) {
+        av_log_missing_feature(s, "RTP-X-QT with packet specific info", 1);
+        return AVERROR_NOTSUPP;
+    }
+
+    alen = len - url_ftell(&pb);
+    if (alen <= 0)
+        return AVERROR_INVALIDDATA;
+
+    switch (packing_scheme) {
+    case 3: /* one data packet spread over 1 or multiple RTP packets */
+        if (qt->pkt.size > 0 && qt->timestamp == *timestamp) {
+            qt->pkt.data = av_realloc(qt->pkt.data, qt->pkt.size + alen +
+                                      FF_INPUT_BUFFER_PADDING_SIZE);
+        } else {
+            av_freep(&qt->pkt.data);
+            av_init_packet(&qt->pkt);
+            qt->pkt.data = av_malloc(alen + FF_INPUT_BUFFER_PADDING_SIZE);
+            qt->pkt.size = 0;
+            qt->timestamp = *timestamp;
+        }
+        if (!qt->pkt.data)
+            return AVERROR(ENOMEM);
+        memcpy(qt->pkt.data + qt->pkt.size, buf + url_ftell(&pb), alen);
+        qt->pkt.size += alen;
+        if (has_marker_bit) {
+            *pkt = qt->pkt;
+            qt->pkt.size = 0;
+            qt->pkt.data = NULL;
+            pkt->flags        = flags & RTP_FLAG_KEY ? AV_PKT_FLAG_KEY : 0;
+            pkt->stream_index = st->index;
+            pkt->destruct     = av_destruct_packet;
+            memset(pkt->data + pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
+            return 0;
+        }
+        return AVERROR(EAGAIN);
+
+    case 1: /* constant packet size, multiple packets per RTP packet */
+        if (qt->bytes_per_frame == 0 ||
+            alen % qt->bytes_per_frame != 0)
+            return AVERROR_INVALIDDATA; /* wrongly padded */
+        qt->remaining = (alen / qt->bytes_per_frame) - 1;
+        if (av_new_packet(pkt, qt->bytes_per_frame))
+            return AVERROR(ENOMEM);
+        memcpy(pkt->data, buf + url_ftell(&pb), qt->bytes_per_frame);
+        pkt->flags = flags & RTP_FLAG_KEY ? AV_PKT_FLAG_KEY : 0;
+        pkt->stream_index = st->index;
+        if (qt->remaining > 0) {
+            av_freep(&qt->pkt.data);
+            qt->pkt.data = av_malloc(qt->remaining * qt->bytes_per_frame);
+            if (!qt->pkt.data) {
+                av_free_packet(pkt);
+                return AVERROR(ENOMEM);
+            }
+            qt->pkt.size = qt->remaining * qt->bytes_per_frame;
+            memcpy(qt->pkt.data,
+                   buf + url_ftell(&pb) + qt->bytes_per_frame,
+                   qt->remaining * qt->bytes_per_frame);
+            qt->pkt.flags = pkt->flags;
+            return 1;
+        }
+        return 0;
+
+    default:  /* unimplemented */
+        av_log_missing_feature(NULL, "RTP-X-QT with packing scheme 2", 1);
+        return AVERROR_NOTSUPP;
+    }
+}
+
+static PayloadContext *qt_rtp_new(void)
+{
+    return av_mallocz(sizeof(PayloadContext));
+}
+
+static void qt_rtp_free(PayloadContext *qt)
+{
+    av_freep(&qt->pkt.data);
+    av_free(qt);
+}
+
+#define RTP_QT_HANDLER(m, n, s, t) \
+RTPDynamicProtocolHandler ff_ ## m ## _rtp_ ## n ## _handler = { \
+    s, \
+    t, \
+    CODEC_ID_NONE, \
+    NULL,          \
+    qt_rtp_new,    \
+    qt_rtp_free,   \
+    qt_rtp_parse_packet, \
+};
+
+RTP_QT_HANDLER(qt,        vid, "X-QT",        CODEC_TYPE_VIDEO);
+RTP_QT_HANDLER(qt,        aud, "X-QT",        CODEC_TYPE_AUDIO);
+RTP_QT_HANDLER(quicktime, vid, "X-QUICKTIME", CODEC_TYPE_VIDEO);
+RTP_QT_HANDLER(quicktime, aud, "X-QUICKTIME", CODEC_TYPE_AUDIO);