annotate rtpenc_xiph.c @ 6349:93c7a56fa912 libavformat

Add RTP packetization of Theora and Vorbis Patch by Josh Allmann, joshua dot allmann at gmail
author mstorsjo
date Sat, 07 Aug 2010 11:16:07 +0000
parents
children c0bd24801ac1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6349
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
1 /*
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
2 * RTP packetization for Xiph audio and video
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
3 * Copyright (c) 2010 Josh Allmann
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
4 *
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
5 * This file is part of FFmpeg.
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
6 *
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
11 *
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
15 * Lesser General Public License for more details.
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
16 *
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
20 */
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
21
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
22 #include "avformat.h"
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
23 #include "rtpenc.h"
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
24
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
25 /**
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
26 * Packetize Xiph frames into RTP according to
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
27 * RFC 5215 (Vorbis) and the Theora RFC draft.
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
28 * (http://svn.xiph.org/trunk/theora/doc/draft-ietf-avt-rtp-theora-00.txt)
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
29 */
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
30 void ff_rtp_send_xiph(AVFormatContext *s1, const uint8_t *buff, int size)
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
31 {
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
32 RTPMuxContext *s = s1->priv_data;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
33 int max_pkt_size, xdt, frag;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
34 uint8_t *q;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
35
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
36 max_pkt_size = s->max_payload_size;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
37
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
38 // set xiph data type
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
39 switch (*buff) {
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
40 case 0x01: // vorbis id
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
41 case 0x05: // vorbis setup
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
42 case 0x80: // theora header
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
43 case 0x82: // theora tables
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
44 xdt = 1; // packed config payload
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
45 break;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
46 case 0x03: // vorbis comments
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
47 case 0x81: // theora comments
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
48 xdt = 2; // comment payload
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
49 break;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
50 default:
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
51 xdt = 0; // raw data payload
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
52 break;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
53 }
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
54
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
55 // Set ident. Must match the one in sdp.c
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
56 // Probably need a non-fixed way of generating
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
57 // this, but it has to be done in SDP and passed in from there.
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
58 q = s->buf;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
59 *q++ = 0xfe;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
60 *q++ = 0xcd;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
61 *q++ = 0xba;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
62
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
63 // set fragment
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
64 // 0 - whole frame (possibly multiple frames)
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
65 // 1 - first fragment
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
66 // 2 - fragment continuation
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
67 // 3 - last fragmement
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
68 frag = size <= max_pkt_size ? 0 : 1;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
69
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
70 if (!frag && !xdt) { // do we have a whole frame of raw data?
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
71 unsigned end_ptr = (unsigned)s->buf + 6 + max_pkt_size; // what we're allowed to write
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
72 unsigned ptr = (unsigned)s->buf_ptr + 2 + size; // what we're going to write
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
73 int remaining = end_ptr - ptr;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
74
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
75 if ((s->num_frames > 0 && remaining < 0) ||
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
76 s->num_frames >= s->max_frames_per_packet) {
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
77 // send previous packets now; no room for new data
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
78 ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
79 s->num_frames = 0;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
80 }
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
81
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
82 // buffer current frame to send later
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
83 if (0 == s->num_frames) s->timestamp = s->cur_timestamp;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
84 s->num_frames++;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
85
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
86 // Set packet header. Normally, this is OR'd with frag and xdt,
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
87 // but those are zero, so omitted here
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
88 *q++ = s->num_frames;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
89
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
90 if (s->num_frames > 1) q = s->buf_ptr; // jump ahead if needed
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
91 *q++ = (size >> 8) & 0xff;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
92 *q++ = size & 0xff;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
93 memcpy(q, buff, size);
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
94 q += size;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
95 s->buf_ptr = q;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
96
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
97 return;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
98 } else if (s->num_frames) {
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
99 // immediately send buffered frames if buffer is not raw data,
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
100 // or if current frame is fragmented.
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
101 ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
102 }
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
103
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
104 s->timestamp = s->cur_timestamp;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
105 s->num_frames = 0;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
106 s->buf_ptr = q;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
107 while (size > 0) {
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
108 int len = (!frag || frag == 3) ? size : max_pkt_size;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
109 q = s->buf_ptr;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
110
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
111 // set packet headers
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
112 *q++ = (frag << 6) | (xdt << 4); // num_frames = 0
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
113 *q++ = (len >> 8) & 0xff;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
114 *q++ = len & 0xff;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
115 // set packet body
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
116 memcpy(q, buff, len);
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
117 q += len;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
118 buff += len;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
119 size -= len;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
120
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
121 ff_rtp_send_data(s1, s->buf, q - s->buf, 0);
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
122
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
123 frag = size <= max_pkt_size ? 3 : 2;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
124 }
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
125 }