Hi,

On 12 August 2010 13:15, Martin Storsjö <[email protected]> wrote:
> On Thu, 12 Aug 2010, Luca Abeni wrote:
>
>> On 12/08/10 21:56, Martin Storsjö wrote:
>> [...]
>> > > > The packetizer looks good to me now, do you agree, Luca A?
>> > > Sorry, I think I did not receive the patch... I'll have a look at the
>> > > ffmpeg-soc archives.
>> > > Anyway, I think the warning should be printed with av_log() (printing it
>> > > in
>> > > the SDP as a comment might be an idea, but I'd like to see a warning on
>> > > stderr
>> > > too).
>> >
>> > Yeah, it is printed with av_log, he just prints it in sdp.c instead of
>> > adding a separate VP8-specific case in the rtpenc initialization (where
>> > none is needed otherwise).
>> Ah, I see... But I think that adding a VP8 case in rtp_write_header() would 
>> be
>> a better idea (for some reason, people might stream VP8 over RTP without
>> calling avf_sdp_create()).
>
> Hmm, good point.
>

Done. I'm not sure whether AV_LOG_WARNING prints to stderr?

>> Also, after reading the patch I think it would be nicer to have 
>> rtp_send_vp8()
>> defined in a separate rtpenc_vp8.c file (sorry, I did not read the full
>> archives, so I do not know if this has been discussed before).
>
> No, it hasn't been discussed before - I guess Josh thought it was so short
> that it didn't require a separate file, but if you'd prefer that, I guess
> he can change it that way. :-)
>

Done. It feels a bit unwieldy though -- not only is the function
really short, but the diff now touches rtpenc.h, the Makefile, and
adds another non-static function to the ff_ namespace. My preference
would be to keep it in rtpenc.c, but I'm not maintainer.

Also added the vp8->data check that was requested by Martin earlier.

The proposal also moved to the webm website, so I've updated the
in-code docs accordingly.
http://www.webmproject.org/code/specs/rtp/

Finally, updated the Changelog, bumped micro, and rebased against the
LATM patch so everything should be up to date.

Josh
From 54ba7a83ac3e6150f6c55d3190e30fdb7996a1f2 Mon Sep 17 00:00:00 2001
From: Josh Allmann <[email protected]>
Date: Wed, 28 Jul 2010 00:30:09 -0700
Subject: [PATCH 1/2] Add RTP packetization of VP8.

---
 libavformat/Makefile     |    1 +
 libavformat/rtpenc.c     |    7 ++++++
 libavformat/rtpenc.h     |    1 +
 libavformat/rtpenc_vp8.c |   47 ++++++++++++++++++++++++++++++++++++++++++++++
 libavformat/sdp.c        |    4 +++
 5 files changed, 60 insertions(+), 0 deletions(-)
 create mode 100644 libavformat/rtpenc_vp8.c

diff --git a/libavformat/Makefile b/libavformat/Makefile
index ed0940f..09cfe8b 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -219,6 +219,7 @@ OBJS-$(CONFIG_RTP_MUXER)                 += rtp.o         \
                                             rtpenc_mpv.o     \
                                             rtpenc.o      \
                                             rtpenc_h264.o \
+                                            rtpenc_vp8.o  \
                                             rtpenc_xiph.o \
                                             avc.o
 OBJS-$(CONFIG_RTSP_DEMUXER)              += rtsp.o httpauth.o
diff --git a/libavformat/rtpenc.c b/libavformat/rtpenc.c
index 9335e8c..a6ba3e5 100644
--- a/libavformat/rtpenc.c
+++ b/libavformat/rtpenc.c
@@ -55,6 +55,7 @@ static int is_supported(enum CodecID id)
     case CODEC_ID_AMR_WB:
     case CODEC_ID_VORBIS:
     case CODEC_ID_THEORA:
+    case CODEC_ID_VP8:
         return 1;
     default:
         return 0;
@@ -144,6 +145,9 @@ static int rtp_write_header(AVFormatContext *s1)
         s->max_payload_size -= 6; // ident+frag+tdt/vdt+pkt_num+pkt_length
         s->num_frames = 0;
         goto defaultcase;
+    case CODEC_ID_VP8:
+        av_log(s1, AV_LOG_WARNING, "RTP VP8 payload is still experimental\n");
+        break;
     case CODEC_ID_AMR_NB:
     case CODEC_ID_AMR_WB:
         if (!s->max_frames_per_packet)
@@ -407,6 +411,9 @@ static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt)
     case CODEC_ID_THEORA:
         ff_rtp_send_xiph(s1, pkt->data, size);
         break;
+    case CODEC_ID_VP8:
+        ff_rtp_send_vp8(s1, pkt->data, size);
+        break;
     default:
         /* better than nothing : send the codec raw data */
         rtp_send_raw(s1, pkt->data, size);
diff --git a/libavformat/rtpenc.h b/libavformat/rtpenc.h
index d5d8b99..b9663c5 100644
--- a/libavformat/rtpenc.h
+++ b/libavformat/rtpenc.h
@@ -68,5 +68,6 @@ void ff_rtp_send_aac(AVFormatContext *s1, const uint8_t *buff, int size);
 void ff_rtp_send_amr(AVFormatContext *s1, const uint8_t *buff, int size);
 void ff_rtp_send_mpegvideo(AVFormatContext *s1, const uint8_t *buf1, int size);
 void ff_rtp_send_xiph(AVFormatContext *s1, const uint8_t *buff, int size);
+void ff_rtp_send_vp8(AVFormatContext *s1, const uint8_t *buff, int size);
 
 #endif /* AVFORMAT_RTPENC_H */
diff --git a/libavformat/rtpenc_vp8.c b/libavformat/rtpenc_vp8.c
new file mode 100644
index 0000000..e865514
--- /dev/null
+++ b/libavformat/rtpenc_vp8.c
@@ -0,0 +1,47 @@
+/*
+ * RTP VP8 Packetizer
+ * Copyright (c) 2010 Josh Allmann
+ *
+ * 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
+ */
+
+#include "rtpenc.h"
+
+/* Based on a draft spec for VP8 RTP.
+ * ( http://www.webmproject.org/code/specs/rtp/ ) */
+void ff_rtp_send_vp8(AVFormatContext *s1, const uint8_t *buf, int size)
+{
+    RTPMuxContext *s = s1->priv_data;
+    int len, max_packet_size;
+
+    s->buf_ptr      = s->buf;
+    s->timestamp    = s->cur_timestamp;
+    max_packet_size = s->max_payload_size - 1; // minus one for header byte
+
+    *s->buf_ptr++ = 1; // 0b1 indicates start of frame
+    while (size > 0) {
+        len = FFMIN(size, max_packet_size);
+
+        memcpy(s->buf_ptr, buf, len);
+        ff_rtp_send_data(s1, s->buf, len+1, size == len); // marker bit is last packet in frame
+
+        size         -= len;
+        buf          += len;
+        s->buf_ptr    = s->buf;
+        *s->buf_ptr++ = 0; // payload descriptor
+    }
+}
diff --git a/libavformat/sdp.c b/libavformat/sdp.c
index ab5ef40..ad06906 100644
--- a/libavformat/sdp.c
+++ b/libavformat/sdp.c
@@ -412,6 +412,10 @@ static char *sdp_write_media_attributes(char *buff, int size, AVCodecContext *c,
                                     c->width, c->height, pix_fmt, config);
             break;
         }
+        case CODEC_ID_VP8:
+            av_strlcatf(buff, size, "a=rtpmap:%d VP8/90000\r\n",
+                                     payload_type);
+            break;
         default:
             /* Nothing special to do here... */
             break;
-- 
1.7.0.4

From 983937880668947ad8994bb612926c719364b679 Mon Sep 17 00:00:00 2001
From: Josh Allmann <[email protected]>
Date: Thu, 29 Jul 2010 03:51:36 -0700
Subject: [PATCH 2/2] Add RTP depacketization of VP8.

---
 Changelog                    |    1 +
 libavformat/Makefile         |    1 +
 libavformat/avformat.h       |    2 +-
 libavformat/rtpdec.c         |    1 +
 libavformat/rtpdec_formats.h |    1 +
 libavformat/rtpdec_vp8.c     |  153 ++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 158 insertions(+), 1 deletions(-)
 create mode 100644 libavformat/rtpdec_vp8.c

diff --git a/Changelog b/Changelog
index 7c0413c..eb45741 100644
--- a/Changelog
+++ b/Changelog
@@ -29,6 +29,7 @@ version <next>:
 - ffprobe -show_packets option added
 - RTP packetization of Theora and Vorbis
 - RTP depacketization of MP4A-LATM
+- RTP packetization and depacketization of VP8
 
 
 version 0.6:
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 09cfe8b..b355f18 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -236,6 +236,7 @@ OBJS-$(CONFIG_SDP_DEMUXER)               += rtsp.o        \
                                             rtpdec_mpeg4.o \
                                             rtpdec_qdm2.o \
                                             rtpdec_svq3.o \
+                                            rtpdec_vp8.o  \
                                             rtpdec_xiph.o
 OBJS-$(CONFIG_SEGAFILM_DEMUXER)          += segafilm.o
 OBJS-$(CONFIG_SHORTEN_DEMUXER)           += raw.o
diff --git a/libavformat/avformat.h b/libavformat/avformat.h
index d4fdc52..a6c40ad 100644
--- a/libavformat/avformat.h
+++ b/libavformat/avformat.h
@@ -23,7 +23,7 @@
 
 #define LIBAVFORMAT_VERSION_MAJOR 52
 #define LIBAVFORMAT_VERSION_MINOR 78
-#define LIBAVFORMAT_VERSION_MICRO  2
+#define LIBAVFORMAT_VERSION_MICRO  3
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
                                                LIBAVFORMAT_VERSION_MINOR, \
diff --git a/libavformat/rtpdec.c b/libavformat/rtpdec.c
index fb58a9d..f350251 100644
--- a/libavformat/rtpdec.c
+++ b/libavformat/rtpdec.c
@@ -66,6 +66,7 @@ void av_register_rtp_dynamic_payload_handlers(void)
     ff_register_dynamic_payload_handler(&ff_qdm2_dynamic_handler);
     ff_register_dynamic_payload_handler(&ff_svq3_dynamic_handler);
     ff_register_dynamic_payload_handler(&ff_mp4a_latm_dynamic_handler);
+    ff_register_dynamic_payload_handler(&ff_vp8_dynamic_handler);
 
     ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfv_handler);
     ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfa_handler);
diff --git a/libavformat/rtpdec_formats.h b/libavformat/rtpdec_formats.h
index 2add8ec..4b30879 100644
--- a/libavformat/rtpdec_formats.h
+++ b/libavformat/rtpdec_formats.h
@@ -45,5 +45,6 @@ extern RTPDynamicProtocolHandler ff_qdm2_dynamic_handler;
 extern RTPDynamicProtocolHandler ff_svq3_dynamic_handler;
 extern RTPDynamicProtocolHandler ff_theora_dynamic_handler;
 extern RTPDynamicProtocolHandler ff_vorbis_dynamic_handler;
+extern RTPDynamicProtocolHandler ff_vp8_dynamic_handler;
 
 #endif /* AVFORMAT_RTPDEC_FORMATS_H */
diff --git a/libavformat/rtpdec_vp8.c b/libavformat/rtpdec_vp8.c
new file mode 100644
index 0000000..8de89ff
--- /dev/null
+++ b/libavformat/rtpdec_vp8.c
@@ -0,0 +1,153 @@
+/*
+ * RTP VP8 Depacketizer
+ * Copyright (c) 2010 Josh Allmann
+ *
+ * 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 RTP support for the VP8 payload
+ * @author Josh Allmann <[email protected]>
+ * ( http://www.webmproject.org/code/specs/rtp/ )
+ */
+
+#include "libavcodec/bytestream.h"
+
+#include "rtpdec_formats.h"
+
+struct PayloadContext {
+    ByteIOContext *data;
+    uint32_t       timestamp;
+    int is_keyframe;
+};
+
+static void prepare_packet(AVPacket *pkt, PayloadContext *vp8, int stream)
+{
+    av_init_packet(pkt);
+    pkt->stream_index = stream;
+    pkt->flags        = vp8->is_keyframe ? AV_PKT_FLAG_KEY : 0;
+    pkt->size         = url_close_dyn_buf(vp8->data, &pkt->data);
+    pkt->destruct     = av_destruct_packet;
+    vp8->data         = NULL;
+}
+
+static int vp8_handle_packet(AVFormatContext *ctx,
+                             PayloadContext *vp8,
+                             AVStream *st,
+                             AVPacket *pkt,
+                             uint32_t *timestamp,
+                             const uint8_t *buf,
+                             int len, int flags)
+{
+    int start_packet, end_packet, has_au, ret = AVERROR(EAGAIN);
+
+    if (!buf) {
+        // only called when vp8_handle_packet returns 1
+        if (!vp8->data) {
+            av_log(ctx, AV_LOG_ERROR, "Invalid VP8 data passed\n");
+            return AVERROR_INVALIDDATA;
+        }
+        prepare_packet(pkt, vp8, st->index);
+        *timestamp = vp8->timestamp;
+        return 0;
+    }
+
+    start_packet = *buf & 1;
+    end_packet   = flags & RTP_FLAG_MARKER;
+    has_au       = *buf & 2;
+    buf++;
+    len--;
+
+    if (start_packet) {
+        int res;
+        uint32_t ts = *timestamp;
+        if (vp8->data) {
+            // missing end marker; return old frame anyway. untested
+            prepare_packet(pkt, vp8, st->index);
+            *timestamp = vp8->timestamp; // reset timestamp from old frame
+
+            // if current frame fits into one rtp packet, need to hold
+            // that for the next av_get_packet call
+            ret = end_packet ? 1 : 0;
+        }
+        if ((res = url_open_dyn_buf(&vp8->data)) < 0)
+            return res;
+        vp8->is_keyframe = *buf & 1;
+        vp8->timestamp   = ts;
+     }
+
+    if (!vp8->data || vp8->timestamp != *timestamp && ret == AVERROR(EAGAIN)) {
+        av_log(ctx, AV_LOG_WARNING,
+               "Received no start marker; dropping frame\n");
+        return AVERROR(EAGAIN);
+    }
+
+    // cycle through VP8AU headers if needed
+    // not tested with actual VP8AUs
+    while (len) {
+        int au_len = len;
+        if (has_au && len > 2) {
+            au_len = AV_RB16(buf);
+            buf += 2;
+            len -= 2;
+            if (buf + au_len > buf + len) {
+                av_log(ctx, AV_LOG_ERROR, "Invalid VP8AU length\n");
+                return AVERROR_INVALIDDATA;
+            }
+        }
+
+        put_buffer(vp8->data, buf, au_len);
+        buf += au_len;
+        len -= au_len;
+    }
+
+    if (ret != AVERROR(EAGAIN)) // did we miss a end marker?
+        return ret;
+
+    if (end_packet) {
+        prepare_packet(pkt, vp8, st->index);
+        return 0;
+    }
+
+    return AVERROR(EAGAIN);
+}
+
+static PayloadContext *vp8_new_context(void)
+{
+    av_log(NULL, AV_LOG_WARNING, "RTP VP8 payload is still experimental\n");
+    return av_mallocz(sizeof(PayloadContext));
+}
+
+static void vp8_free_context(PayloadContext *vp8)
+{
+    if (vp8->data) {
+        uint8_t *tmp;
+        url_close_dyn_buf(vp8->data, &tmp);
+        av_free(tmp);
+    }
+    av_free(vp8);
+}
+
+RTPDynamicProtocolHandler ff_vp8_dynamic_handler = {
+    .enc_name       = "VP8",
+    .codec_type     = AVMEDIA_TYPE_VIDEO,
+    .codec_id       = CODEC_ID_VP8,
+    .open           = vp8_new_context,
+    .close          = vp8_free_context,
+    .parse_packet   = vp8_handle_packet,
+};
-- 
1.7.0.4

_______________________________________________
FFmpeg-soc mailing list
[email protected]
https://lists.mplayerhq.hu/mailman/listinfo/ffmpeg-soc

Reply via email to