annotate rtpenc_xiph.c @ 6491:b7f807b4cd88 libavformat tip

In mov demuxer, check that nb_streams is valid before using it in read_dac3
author bcoudurier
date Tue, 28 Sep 2010 00:33:21 +0000
parents f8fe5baa8b47
children
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
6355
3aa7765383b5 rtpenc_xiph: Set the ident value via a define
mstorsjo
parents: 6354
diff changeset
55 // Set ident.
6349
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;
6355
3aa7765383b5 rtpenc_xiph: Set the ident value via a define
mstorsjo
parents: 6354
diff changeset
59 *q++ = (RTP_XIPH_IDENT >> 16) & 0xff;
3aa7765383b5 rtpenc_xiph: Set the ident value via a define
mstorsjo
parents: 6354
diff changeset
60 *q++ = (RTP_XIPH_IDENT >> 8) & 0xff;
3aa7765383b5 rtpenc_xiph: Set the ident value via a define
mstorsjo
parents: 6354
diff changeset
61 *q++ = (RTP_XIPH_IDENT ) & 0xff;
6349
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?
6354
c0bd24801ac1 rtpenc_xiph: Don't needlessly cast pointers to integers
mstorsjo
parents: 6349
diff changeset
71 uint8_t *end_ptr = s->buf + 6 + max_pkt_size; // what we're allowed to write
c0bd24801ac1 rtpenc_xiph: Don't needlessly cast pointers to integers
mstorsjo
parents: 6349
diff changeset
72 uint8_t *ptr = s->buf_ptr + 2 + size; // what we're going to write
6349
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
6359
f8fe5baa8b47 rtpenc_xiph: Clarify that num_frames shouldn't ever get larger than max_frames_per_packet
mstorsjo
parents: 6355
diff changeset
75 assert(s->num_frames <= s->max_frames_per_packet);
6349
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
76 if ((s->num_frames > 0 && remaining < 0) ||
6359
f8fe5baa8b47 rtpenc_xiph: Clarify that num_frames shouldn't ever get larger than max_frames_per_packet
mstorsjo
parents: 6355
diff changeset
77 s->num_frames == s->max_frames_per_packet) {
6349
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
78 // send previous packets now; no room for new data
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
79 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
80 s->num_frames = 0;
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
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
83 // buffer current frame to send later
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
84 if (0 == s->num_frames) s->timestamp = s->cur_timestamp;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
85 s->num_frames++;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
86
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
87 // Set packet header. Normally, this is OR'd with frag and xdt,
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
88 // but those are zero, so omitted here
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
89 *q++ = s->num_frames;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
90
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
91 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
92 *q++ = (size >> 8) & 0xff;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
93 *q++ = size & 0xff;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
94 memcpy(q, buff, size);
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
95 q += size;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
96 s->buf_ptr = q;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
97
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
98 return;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
99 } else if (s->num_frames) {
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
100 // immediately send buffered frames if buffer is not raw data,
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
101 // or if current frame is fragmented.
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
102 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
103 }
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
104
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
105 s->timestamp = s->cur_timestamp;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
106 s->num_frames = 0;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
107 s->buf_ptr = q;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
108 while (size > 0) {
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
109 int len = (!frag || frag == 3) ? size : max_pkt_size;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
110 q = s->buf_ptr;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
111
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
112 // set packet headers
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
113 *q++ = (frag << 6) | (xdt << 4); // num_frames = 0
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
114 *q++ = (len >> 8) & 0xff;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
115 *q++ = len & 0xff;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
116 // set packet body
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
117 memcpy(q, buff, len);
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
118 q += len;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
119 buff += len;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
120 size -= len;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
121
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
122 ff_rtp_send_data(s1, s->buf, q - s->buf, 0);
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
123
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
124 frag = size <= max_pkt_size ? 3 : 2;
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
125 }
93c7a56fa912 Add RTP packetization of Theora and Vorbis
mstorsjo
parents:
diff changeset
126 }