Browse code

Add RTP packetization of Theora and Vorbis

Patch by Josh Allmann, joshua dot allmann at gmail

Originally committed as revision 24735 to svn://svn.ffmpeg.org/ffmpeg/trunk

Josh Allmann authored on 2010/08/07 20:16:07
Showing 8 changed files
... ...
@@ -27,6 +27,7 @@ version <next>:
27 27
 - SubRip subtitle file muxer and demuxer
28 28
 - Chinese AVS encoding via libxavs
29 29
 - ffprobe -show_packets option added
30
+- RTP packetization of Theora and Vorbis
30 31
 
31 32
 
32 33
 
... ...
@@ -219,6 +219,7 @@ OBJS-$(CONFIG_RTP_MUXER)                 += rtp.o         \
219 219
                                             rtpenc_mpv.o     \
220 220
                                             rtpenc.o      \
221 221
                                             rtpenc_h264.o \
222
+                                            rtpenc_xiph.o \
222 223
                                             avc.o
223 224
 OBJS-$(CONFIG_RTSP_DEMUXER)              += rtsp.o httpauth.o
224 225
 OBJS-$(CONFIG_RTSP_MUXER)                += rtsp.o rtspenc.o httpauth.o
... ...
@@ -23,7 +23,7 @@
23 23
 
24 24
 #define LIBAVFORMAT_VERSION_MAJOR 52
25 25
 #define LIBAVFORMAT_VERSION_MINOR 78
26
-#define LIBAVFORMAT_VERSION_MICRO  0
26
+#define LIBAVFORMAT_VERSION_MICRO  1
27 27
 
28 28
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
29 29
                                                LIBAVFORMAT_VERSION_MINOR, \
... ...
@@ -53,6 +53,8 @@ static int is_supported(enum CodecID id)
53 53
     case CODEC_ID_MPEG2TS:
54 54
     case CODEC_ID_AMR_NB:
55 55
     case CODEC_ID_AMR_WB:
56
+    case CODEC_ID_VORBIS:
57
+    case CODEC_ID_THEORA:
56 58
         return 1;
57 59
     default:
58 60
         return 0;
... ...
@@ -135,6 +137,13 @@ static int rtp_write_header(AVFormatContext *s1)
135 135
             s->nal_length_size = (st->codec->extradata[4] & 0x03) + 1;
136 136
         }
137 137
         break;
138
+    case CODEC_ID_VORBIS:
139
+    case CODEC_ID_THEORA:
140
+        if (!s->max_frames_per_packet) s->max_frames_per_packet = 15;
141
+        s->max_frames_per_packet = av_clip(s->max_frames_per_packet, 1, 15);
142
+        s->max_payload_size -= 6; // ident+frag+tdt/vdt+pkt_num+pkt_length
143
+        s->num_frames = 0;
144
+        goto defaultcase;
138 145
     case CODEC_ID_AMR_NB:
139 146
     case CODEC_ID_AMR_WB:
140 147
         if (!s->max_frames_per_packet)
... ...
@@ -155,6 +164,7 @@ static int rtp_write_header(AVFormatContext *s1)
155 155
     case CODEC_ID_AAC:
156 156
         s->num_frames = 0;
157 157
     default:
158
+defaultcase:
158 159
         if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
159 160
             av_set_pts_info(st, 32, 1, st->codec->sample_rate);
160 161
         }
... ...
@@ -393,6 +403,10 @@ static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt)
393 393
     case CODEC_ID_H263P:
394 394
         ff_rtp_send_h263(s1, pkt->data, size);
395 395
         break;
396
+    case CODEC_ID_VORBIS:
397
+    case CODEC_ID_THEORA:
398
+        ff_rtp_send_xiph(s1, pkt->data, size);
399
+        break;
396 400
     default:
397 401
         /* better than nothing : send the codec raw data */
398 402
         rtp_send_raw(s1, pkt->data, size);
... ...
@@ -67,5 +67,6 @@ void ff_rtp_send_h263(AVFormatContext *s1, const uint8_t *buf1, int size);
67 67
 void ff_rtp_send_aac(AVFormatContext *s1, const uint8_t *buff, int size);
68 68
 void ff_rtp_send_amr(AVFormatContext *s1, const uint8_t *buff, int size);
69 69
 void ff_rtp_send_mpegvideo(AVFormatContext *s1, const uint8_t *buf1, int size);
70
+void ff_rtp_send_xiph(AVFormatContext *s1, const uint8_t *buff, int size);
70 71
 
71 72
 #endif /* AVFORMAT_RTPENC_H */
72 73
new file mode 100644
... ...
@@ -0,0 +1,125 @@
0
+/*
1
+ * RTP packetization for Xiph audio and video
2
+ * Copyright (c) 2010 Josh Allmann
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include "avformat.h"
22
+#include "rtpenc.h"
23
+
24
+/**
25
+ * Packetize Xiph frames into RTP according to
26
+ * RFC 5215 (Vorbis) and the Theora RFC draft.
27
+ * (http://svn.xiph.org/trunk/theora/doc/draft-ietf-avt-rtp-theora-00.txt)
28
+ */
29
+void ff_rtp_send_xiph(AVFormatContext *s1, const uint8_t *buff, int size)
30
+{
31
+    RTPMuxContext *s = s1->priv_data;
32
+    int max_pkt_size, xdt, frag;
33
+    uint8_t *q;
34
+
35
+    max_pkt_size = s->max_payload_size;
36
+
37
+    // set xiph data type
38
+    switch (*buff) {
39
+    case 0x01:   // vorbis id
40
+    case 0x05:   // vorbis setup
41
+    case 0x80:   // theora header
42
+    case 0x82:   // theora tables
43
+        xdt = 1; // packed config payload
44
+        break;
45
+    case 0x03:   // vorbis comments
46
+    case 0x81:   // theora comments
47
+        xdt = 2; // comment payload
48
+        break;
49
+    default:
50
+        xdt = 0; // raw data payload
51
+        break;
52
+    }
53
+
54
+    // Set ident. Must match the one in sdp.c
55
+    // Probably need a non-fixed way of generating
56
+    // this, but it has to be done in SDP and passed in from there.
57
+    q = s->buf;
58
+    *q++ = 0xfe;
59
+    *q++ = 0xcd;
60
+    *q++ = 0xba;
61
+
62
+    // set fragment
63
+    // 0 - whole frame (possibly multiple frames)
64
+    // 1 - first fragment
65
+    // 2 - fragment continuation
66
+    // 3 - last fragmement
67
+    frag = size <= max_pkt_size ? 0 : 1;
68
+
69
+    if (!frag && !xdt) { // do we have a whole frame of raw data?
70
+        unsigned end_ptr = (unsigned)s->buf + 6 + max_pkt_size; // what we're allowed to write
71
+        unsigned ptr     = (unsigned)s->buf_ptr + 2 + size; // what we're going to write
72
+        int remaining    = end_ptr - ptr;
73
+
74
+        if ((s->num_frames > 0 && remaining < 0) ||
75
+            s->num_frames >= s->max_frames_per_packet) {
76
+            // send previous packets now; no room for new data
77
+            ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
78
+            s->num_frames = 0;
79
+        }
80
+
81
+        // buffer current frame to send later
82
+        if (0 == s->num_frames) s->timestamp = s->cur_timestamp;
83
+        s->num_frames++;
84
+
85
+        // Set packet header. Normally, this is OR'd with frag and xdt,
86
+        // but those are zero, so omitted here
87
+        *q++ = s->num_frames;
88
+
89
+        if (s->num_frames > 1) q = s->buf_ptr; // jump ahead if needed
90
+        *q++ = (size >> 8) & 0xff;
91
+        *q++ = size & 0xff;
92
+        memcpy(q, buff, size);
93
+        q += size;
94
+        s->buf_ptr = q;
95
+
96
+        return;
97
+    } else if (s->num_frames) {
98
+        // immediately send buffered frames if buffer is not raw data,
99
+        // or if current frame is fragmented.
100
+        ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
101
+    }
102
+
103
+    s->timestamp = s->cur_timestamp;
104
+    s->num_frames = 0;
105
+    s->buf_ptr = q;
106
+    while (size > 0) {
107
+        int len = (!frag || frag == 3) ? size : max_pkt_size;
108
+        q = s->buf_ptr;
109
+
110
+        // set packet headers
111
+        *q++ = (frag << 6) | (xdt << 4); // num_frames = 0
112
+        *q++ = (len >> 8) & 0xff;
113
+        *q++ = len & 0xff;
114
+        // set packet body
115
+        memcpy(q, buff, len);
116
+        q += len;
117
+        buff += len;
118
+        size -= len;
119
+
120
+        ff_rtp_send_data(s1, s->buf, q - s->buf, 0);
121
+
122
+        frag = size <= max_pkt_size ? 3 : 2;
123
+    }
124
+}
... ...
@@ -52,7 +52,7 @@ int rtsp_default_protocols = (1 << RTSP_LOWER_TRANSPORT_UDP);
52 52
 #define SELECT_TIMEOUT_MS 100
53 53
 #define READ_PACKET_TIMEOUT_S 10
54 54
 #define MAX_TIMEOUTS READ_PACKET_TIMEOUT_S * 1000 / SELECT_TIMEOUT_MS
55
-#define SDP_MAX_SIZE 8192
55
+#define SDP_MAX_SIZE 16384
56 56
 
57 57
 static void get_word_until_chars(char *buf, int buf_size,
58 58
                                  const char *sep, const char **pp)
... ...
@@ -21,6 +21,7 @@
21 21
 #include <string.h>
22 22
 #include "libavutil/avstring.h"
23 23
 #include "libavutil/base64.h"
24
+#include "libavcodec/xiph.h"
24 25
 #include "avformat.h"
25 26
 #include "internal.h"
26 27
 #include "avc.h"
... ...
@@ -220,6 +221,75 @@ static char *extradata2config(AVCodecContext *c)
220 220
     return config;
221 221
 }
222 222
 
223
+static char *xiph_extradata2config(AVCodecContext *c)
224
+{
225
+    char *config, *encoded_config;
226
+    uint8_t *header_start[3];
227
+    int headers_len, header_len[3], config_len;
228
+    int first_header_size;
229
+
230
+    switch (c->codec_id) {
231
+    case CODEC_ID_THEORA:
232
+        first_header_size = 42;
233
+        break;
234
+    case CODEC_ID_VORBIS:
235
+        first_header_size = 30;
236
+        break;
237
+    default:
238
+        av_log(c, AV_LOG_ERROR, "Unsupported Xiph codec ID\n");
239
+        return NULL;
240
+    }
241
+
242
+    if (ff_split_xiph_headers(c->extradata, c->extradata_size,
243
+                              first_header_size, header_start,
244
+                              header_len) < 0) {
245
+        av_log(c, AV_LOG_ERROR, "Extradata corrupt.\n");
246
+        return NULL;
247
+    }
248
+
249
+    headers_len = header_len[0] + header_len[2];
250
+    config_len = 4 +          // count
251
+                 3 +          // ident
252
+                 2 +          // packet size
253
+                 1 +          // header count
254
+                 2 +          // header size
255
+                 headers_len; // and the rest
256
+
257
+    config = av_malloc(config_len);
258
+    if (!config)
259
+        goto xiph_fail;
260
+
261
+    encoded_config = av_malloc(AV_BASE64_SIZE(config_len));
262
+    if (!encoded_config) {
263
+        av_free(config);
264
+        goto xiph_fail;
265
+    }
266
+
267
+    config[0] = config[1] = config[2] = 0;
268
+    config[3] = 1;
269
+    config[4] = 0xfe; // ident must match the one in rtpenc_xiph.c
270
+    config[5] = 0xcd;
271
+    config[6] = 0xba;
272
+    config[7] = (headers_len >> 8) & 0xff;
273
+    config[8] = headers_len & 0xff;
274
+    config[9] = 2;
275
+    config[10] = header_len[0];
276
+    config[11] = 0; // size of comment header; nonexistent
277
+    memcpy(config + 12, header_start[0], header_len[0]);
278
+    memcpy(config + 12 + header_len[0], header_start[2], header_len[2]);
279
+
280
+    av_base64_encode(encoded_config, AV_BASE64_SIZE(config_len),
281
+                     config, config_len);
282
+    av_free(config);
283
+
284
+    return encoded_config;
285
+
286
+xiph_fail:
287
+    av_log(c, AV_LOG_ERROR,
288
+           "Not enough memory for configuration string\n");
289
+    return NULL;
290
+}
291
+
223 292
 static char *sdp_write_media_attributes(char *buff, int size, AVCodecContext *c, int payload_type)
224 293
 {
225 294
     char *config = NULL;
... ...
@@ -297,6 +367,51 @@ static char *sdp_write_media_attributes(char *buff, int size, AVCodecContext *c,
297 297
                                      payload_type, c->sample_rate, c->channels,
298 298
                                      payload_type);
299 299
             break;
300
+        case CODEC_ID_VORBIS:
301
+            if (c->extradata_size)
302
+                config = xiph_extradata2config(c);
303
+            else
304
+                av_log(c, AV_LOG_ERROR, "Vorbis configuration info missing\n");
305
+            if (!config)
306
+                return NULL;
307
+
308
+            av_strlcatf(buff, size, "a=rtpmap:%d vorbis/%d/%d\r\n"
309
+                                    "a=fmtp:%d configuration=%s\r\n",
310
+                                    payload_type, c->sample_rate, c->channels,
311
+                                    payload_type, config);
312
+            break;
313
+        case CODEC_ID_THEORA: {
314
+            const char *pix_fmt;
315
+            if (c->extradata_size)
316
+                config = xiph_extradata2config(c);
317
+            else
318
+                av_log(c, AV_LOG_ERROR, "Theora configuation info missing\n");
319
+            if (!config)
320
+                return NULL;
321
+
322
+            switch (c->pix_fmt) {
323
+            case PIX_FMT_YUV420P:
324
+                pix_fmt = "YCbCr-4:2:0";
325
+                break;
326
+            case PIX_FMT_YUV422P:
327
+                pix_fmt = "YCbCr-4:2:2";
328
+                break;
329
+            case PIX_FMT_YUV444P:
330
+                pix_fmt = "YCbCr-4:4:4";
331
+                break;
332
+            default:
333
+                av_log(c, AV_LOG_ERROR, "Unsupported pixel format.\n");
334
+                return NULL;
335
+            }
336
+
337
+            av_strlcatf(buff, size, "a=rtpmap:%d theora/90000\r\n"
338
+                                    "a=fmtp:%d delivery-method=inline; "
339
+                                    "width=%d; height=%d; sampling=%s; "
340
+                                    "configuration=%s\r\n",
341
+                                    payload_type, payload_type,
342
+                                    c->width, c->height, pix_fmt, config);
343
+            break;
344
+        }
300 345
         default:
301 346
             /* Nothing special to do here... */
302 347
             break;