Hi guys,

Here is a first attempt at a RTP packetizer for the Xiph codecs.
This is still a work in progress; Vorbis likes to crash when rescaling
the timebase, and the Theora output only vaguely resembles what it
should. (There are some unrelated changes in there, particularly to
the depacketizer. I will separate those later.)

The only major architectural issue here is that the configuration
identifier (here hard-coded to 0xfedcba) needs to be carried across
from the SDP to the packetizer proper. Right now I don't believe there
is a clean way to do that without adding another field to
RTPMuxContext, a generic priv_data or something. The "best" way, IMO,
would be to structure RTP packetization in a way similar to how
depacketization is done -- through dynamic handlers.

We might be able to get away with a hard-coded configuration
identifier, but I don't know yet if my samples will periodically emit
in-band extradata. (The RFC provisions for this.) In that case, the
config ID needs to be refreshed, and kept across subsequent packets.

Josh
diff --git a/libavformat/Makefile b/libavformat/Makefile
index b140fe8..e150814 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -217,6 +217,7 @@ OBJS-$(CONFIG_RTP_MUXER)                 += rtp.o         \
                                             rtpenc_mpv.o     \
                                             rtpenc.o      \
                                             rtpenc_h264.o \
+                                            rtpenc_xiph.o \
                                             avc.o
 OBJS-$(CONFIG_RTSP_DEMUXER)              += rtsp.o httpauth.o
 OBJS-$(CONFIG_RTSP_MUXER)                += rtsp.o rtspenc.o httpauth.o
diff --git a/libavformat/rtpdec_xiph.c b/libavformat/rtpdec_xiph.c
index 4c9cad2..3ecbb26 100644
--- a/libavformat/rtpdec_xiph.c
+++ b/libavformat/rtpdec_xiph.c
@@ -288,7 +288,17 @@ static int xiph_parse_fmtp_pair(AVStream* stream,
     int result = 0;
 
     if (!strcmp(attr, "sampling")) {
-        return AVERROR_PATCHWELCOME;
+        if (!strcmp(value, "YCbCr-4:2:0")) {
+            codec->pix_fmt = PIX_FMT_YUV420P;
+        } else if (!strcmp(value, "YCbCr-4:4:2")) {
+            codec->pix_fmt = PIX_FMT_YUV422P;
+        } else if (!strcmp(value, "YCbCr-4:4:4")) {
+            codec->pix_fmt = PIX_FMT_YUV444P;
+        } else {
+            av_log(codec, AV_LOG_ERROR,
+                   "Unsupported pixel format %s\n", attr);
+            return AVERROR_INVALIDDATA;
+        }
     } else if (!strcmp(attr, "width")) {
         /* This is an integer between 1 and 1048561
          * and MUST be in multiples of 16. */
diff --git a/libavformat/rtpenc.c b/libavformat/rtpenc.c
index 4453f65..28be0bf 100644
--- a/libavformat/rtpenc.c
+++ b/libavformat/rtpenc.c
@@ -53,6 +53,8 @@ static int is_supported(enum CodecID id)
     case CODEC_ID_MPEG2TS:
     case CODEC_ID_AMR_NB:
     case CODEC_ID_AMR_WB:
+    case CODEC_ID_VORBIS:
+    case CODEC_ID_THEORA:
         return 1;
     default:
         return 0;
@@ -154,6 +156,11 @@ static int rtp_write_header(AVFormatContext *s1)
         }
     case CODEC_ID_AAC:
         s->num_frames = 0;
+    case CODEC_ID_VORBIS:
+    case CODEC_ID_THEORA:
+        if (!s->max_frames_per_packet || s->max_frames_per_packet > 15)
+            s->max_frames_per_packet = 15;
+        s->max_payload_size -= 6; // ident+frag+tdt/vdt+pkt_num+pkt_length
     default:
         if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
             av_set_pts_info(st, 32, 1, st->codec->sample_rate);
@@ -393,6 +400,10 @@ static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt)
     case CODEC_ID_H263P:
         ff_rtp_send_h263(s1, pkt->data, size);
         break;
+    case CODEC_ID_VORBIS:
+    case CODEC_ID_THEORA:
+        ff_rtp_send_xiph(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 95e70c1..d5d8b99 100644
--- a/libavformat/rtpenc.h
+++ b/libavformat/rtpenc.h
@@ -67,5 +67,6 @@ void ff_rtp_send_h263(AVFormatContext *s1, const uint8_t *buf1, int size);
 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);
 
 #endif /* AVFORMAT_RTPENC_H */
diff --git a/libavformat/rtsp.c b/libavformat/rtsp.c
index 9316f3c..6dc9c0d 100644
--- a/libavformat/rtsp.c
+++ b/libavformat/rtsp.c
@@ -1295,7 +1295,7 @@ static int rtsp_setup_output_streams(AVFormatContext *s, const char *addr)
     rt->start_time = av_gettime();
 
     /* Announce the stream */
-    sdp = av_mallocz(8192);
+    sdp = av_mallocz(16384); // massive SDP buffer due to Xiph extradata
     if (sdp == NULL)
         return AVERROR(ENOMEM);
     /* We create the SDP based on the RTSP AVFormatContext where we
@@ -1314,7 +1314,7 @@ static int rtsp_setup_output_streams(AVFormatContext *s, const char *addr)
     ff_url_join(sdp_ctx.filename, sizeof(sdp_ctx.filename),
                 "rtsp", NULL, addr, -1, NULL);
     ctx_array[0] = &sdp_ctx;
-    if (avf_sdp_create(ctx_array, 1, sdp, 8192)) {
+    if (avf_sdp_create(ctx_array, 1, sdp, 16384)) {
         av_free(sdp);
         return AVERROR_INVALIDDATA;
     }
diff --git a/libavformat/sdp.c b/libavformat/sdp.c
index b34b944..402eec1 100644
--- a/libavformat/sdp.c
+++ b/libavformat/sdp.c
@@ -21,6 +21,7 @@
 #include <string.h>
 #include "libavutil/avstring.h"
 #include "libavutil/base64.h"
+#include "libavcodec/xiph.h"
 #include "avformat.h"
 #include "internal.h"
 #include "avc.h"
@@ -220,6 +221,74 @@ static char *extradata2config(AVCodecContext *c)
     return config;
 }
 
+static char *xiph_extradata2config(AVCodecContext *c)
+{
+    char *config, *encoded_config;
+    uint8_t *header_start[3];
+    int headers_len, header_len[3], config_len;
+    int first_header_size;
+    uint8_t comment[] = {
+        ' ','h', 'i','r','o',' ','n','a','k','a','m','u','r','a'
+    };
+
+    switch (c->codec_id) {
+    case CODEC_ID_THEORA:
+        first_header_size = 42;
+        comment[0]        = 0x81;
+        break;
+    case CODEC_ID_VORBIS:
+        first_header_size = 30;
+        comment[0]        = 0x05;
+        break;
+    default:
+        av_log(c, AV_LOG_ERROR, "Unsupported Xiph codec ID\n");
+        return NULL;
+    }
+
+    if (ff_split_xiph_headers(c->extradata, c->extradata_size,
+                              first_header_size, header_start,
+                              header_len) < 0) {
+        av_log(c, AV_LOG_ERROR, "Extradata corrupt.");
+        return NULL;
+    }
+
+    headers_len = header_len[0]+sizeof(comment)+header_len[2];
+    config_len = 4 +          // count
+                 3 +          // ident
+                 2 +          // packet size
+                 1 +          // header count
+                 2 +          // header size
+                 headers_len; // and the rest
+    config = av_malloc(config_len);
+    encoded_config = av_malloc(AV_BASE64_SIZE(config_len));
+
+    if (!config || !encoded_config) {
+        av_log(c, AV_LOG_ERROR,
+               "Not enough memory for configuration string\n");
+        return NULL;
+    }
+
+    config[0] = config[1] = config[2] = 0;
+    config[3] = 1;
+    config[4] = 0xfe;
+    config[5] = 0xcd;
+    config[6] = 0xba;
+    config[7] = (headers_len >> 8) & 0xff;
+    config[8] = headers_len & 0xff;
+    config[9] = 2;
+    config[10] = header_len[0];
+    config[11] = sizeof(comment);
+    memcpy(config + 12, header_start[0], header_len[0]);
+    memcpy(config + 12 + header_len[0], comment, sizeof(comment));
+    memcpy(config + 12 + header_len[0] + sizeof(comment),
+           header_start[2], header_len[2]);
+
+    av_base64_encode(encoded_config, AV_BASE64_SIZE(config_len),
+                     config, config_len);
+
+    return encoded_config;
+}
+
 static char *sdp_write_media_attributes(char *buff, int size, AVCodecContext *c, int payload_type)
 {
     char *config = NULL;
@@ -297,6 +366,38 @@ static char *sdp_write_media_attributes(char *buff, int size, AVCodecContext *c,
                                      payload_type, c->sample_rate, c->channels,
                                      payload_type);
             break;
+        case CODEC_ID_THEORA: {
+            const char *pix_fmt;
+            if (c->extradata_size)
+                config = xiph_extradata2config(c);
+            else
+                av_log(c, AV_LOG_ERROR, "Theora configuation info missing\n");
+            if (!config)
+                return NULL;
+
+            switch (c->pix_fmt) {
+            case PIX_FMT_YUV420P:
+                pix_fmt = "YCbCr-4:2:0";
+                break;
+            case PIX_FMT_YUV422P:
+                pix_fmt = "YCbCr-4:2:2";
+                break;
+            case PIX_FMT_YUV444P:
+                pix_fmt = "YCbCr-4:4:4";
+                break;
+            default:
+                av_log(c, AV_LOG_ERROR, "Unsupported pixel format.\n");
+                return NULL;
+            }
+
+            av_strlcatf(buff, size, "a=rtpmap:%d theora/9000\r\n"
+                                    "a=fmtp:%d delivery-method=inline;"
+                                    "width=%d; height=%d; sampling=%s;"
+                                    "configuration=%s;\r\n",
+                                    payload_type, payload_type,
+                                    c->width, c->height, pix_fmt, config);
+            break;
+        }
         default:
             /* Nothing special to do here... */
             break;
_______________________________________________
FFmpeg-soc mailing list
[email protected]
https://lists.mplayerhq.hu/mailman/listinfo/ffmpeg-soc

Reply via email to