You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

378 lines
12KB

  1. /*
  2. * Xiph RTP Protocols
  3. * Copyright (c) 2009 Colin McQuillian
  4. * Copyright (c) 2010 Josh Allmann
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * @brief Xiph / RTP Code
  25. * @author Colin McQuillan <m.niloc@gmail.com>
  26. * @author Josh Allmann <joshua.allmann@gmail.com>
  27. */
  28. #include "libavutil/avstring.h"
  29. #include "libavutil/base64.h"
  30. #include "libavcodec/bytestream.h"
  31. #include <assert.h>
  32. #include "rtpdec.h"
  33. #include "rtpdec_xiph.h"
  34. /**
  35. * RTP/Xiph specific private data.
  36. */
  37. struct PayloadContext {
  38. unsigned ident; ///< 24-bit stream configuration identifier
  39. uint32_t timestamp;
  40. ByteIOContext* fragment; ///< buffer for split payloads
  41. };
  42. static PayloadContext *xiph_new_context(void)
  43. {
  44. return av_mallocz(sizeof(PayloadContext));
  45. }
  46. static inline void free_fragment_if_needed(PayloadContext * data)
  47. {
  48. if (data->fragment) {
  49. uint8_t* p;
  50. url_close_dyn_buf(data->fragment, &p);
  51. av_free(p);
  52. data->fragment = NULL;
  53. }
  54. }
  55. static void xiph_free_context(PayloadContext * data)
  56. {
  57. free_fragment_if_needed(data);
  58. av_free(data);
  59. }
  60. static int xiph_handle_packet(AVFormatContext * ctx,
  61. PayloadContext * data,
  62. AVStream * st,
  63. AVPacket * pkt,
  64. uint32_t * timestamp,
  65. const uint8_t * buf, int len, int flags)
  66. {
  67. int ident, fragmented, tdt, num_pkts, pkt_len;
  68. if (len < 6) {
  69. av_log(ctx, AV_LOG_ERROR, "Invalid %d byte packet\n", len);
  70. return AVERROR_INVALIDDATA;
  71. }
  72. // read xiph rtp headers
  73. ident = AV_RB24(buf);
  74. fragmented = buf[3] >> 6;
  75. tdt = (buf[3] >> 4) & 3;
  76. num_pkts = buf[3] & 7;
  77. pkt_len = AV_RB16(buf + 4);
  78. if (pkt_len > len - 6) {
  79. av_log(ctx, AV_LOG_ERROR,
  80. "Invalid packet length %d in %d byte packet\n", pkt_len,
  81. len);
  82. return AVERROR_INVALIDDATA;
  83. }
  84. if (ident != data->ident) {
  85. av_log(ctx, AV_LOG_ERROR,
  86. "Unimplemented Xiph SDP configuration change detected\n");
  87. return AVERROR_PATCHWELCOME;
  88. }
  89. if (tdt) {
  90. av_log(ctx, AV_LOG_ERROR,
  91. "Unimplemented RTP Xiph packet settings (%d,%d,%d)\n",
  92. fragmented, tdt, num_pkts);
  93. return AVERROR_PATCHWELCOME;
  94. }
  95. buf += 6; // move past header bits
  96. len -= 6;
  97. if (fragmented == 0) {
  98. // whole frame(s)
  99. int i, data_len, write_len;
  100. buf -= 2;
  101. len += 2;
  102. // fast first pass to calculate total length
  103. for (i = 0, data_len = 0; (i < num_pkts) && (len >= 2); i++) {
  104. int off = data_len + (i << 1);
  105. pkt_len = AV_RB16(buf + off);
  106. data_len += pkt_len;
  107. len -= pkt_len + 2;
  108. }
  109. if (len < 0 || i < num_pkts) {
  110. av_log(ctx, AV_LOG_ERROR,
  111. "Bad packet: %d bytes left at frame %d of %d\n",
  112. len, i, num_pkts);
  113. return AVERROR_INVALIDDATA;
  114. }
  115. if (av_new_packet(pkt, data_len)) {
  116. av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
  117. return AVERROR(ENOMEM);
  118. }
  119. pkt->stream_index = st->index;
  120. // concatenate frames
  121. for (i = 0, write_len = 0; write_len < data_len; i++) {
  122. pkt_len = AV_RB16(buf);
  123. buf += 2;
  124. memcpy(pkt->data + write_len, buf, pkt_len);
  125. write_len += pkt_len;
  126. buf += pkt_len;
  127. }
  128. assert(write_len == data_len);
  129. return 0;
  130. } else if (fragmented == 1) {
  131. // start of xiph data fragment
  132. int res;
  133. // end packet has been lost somewhere, so drop buffered data
  134. free_fragment_if_needed(data);
  135. if((res = url_open_dyn_buf(&data->fragment)) < 0)
  136. return res;
  137. put_buffer(data->fragment, buf, pkt_len);
  138. data->timestamp = *timestamp;
  139. } else {
  140. assert(fragmented < 4);
  141. if (data->timestamp != *timestamp) {
  142. // skip if fragmented timestamp is incorrect;
  143. // a start packet has been lost somewhere
  144. free_fragment_if_needed(data);
  145. av_log(ctx, AV_LOG_ERROR, "RTP timestamps don't match!\n");
  146. return AVERROR_INVALIDDATA;
  147. }
  148. // copy data to fragment buffer
  149. put_buffer(data->fragment, buf, pkt_len);
  150. if (fragmented == 3) {
  151. // end of xiph data packet
  152. uint8_t* xiph_data;
  153. int frame_size = url_close_dyn_buf(data->fragment, &xiph_data);
  154. if (frame_size < 0) {
  155. av_log(ctx, AV_LOG_ERROR,
  156. "Error occurred when getting fragment buffer.");
  157. return frame_size;
  158. }
  159. if (av_new_packet(pkt, frame_size)) {
  160. av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
  161. return AVERROR(ENOMEM);
  162. }
  163. memcpy(pkt->data, xiph_data, frame_size);
  164. pkt->stream_index = st->index;
  165. av_free(xiph_data);
  166. data->fragment = NULL;
  167. return 0;
  168. }
  169. }
  170. return AVERROR(EAGAIN);
  171. }
  172. /**
  173. * Length encoding described in RFC5215 section 3.1.1.
  174. */
  175. static int get_base128(const uint8_t ** buf, const uint8_t * buf_end)
  176. {
  177. int n = 0;
  178. for (; *buf < buf_end; ++*buf) {
  179. n <<= 7;
  180. n += **buf & 0x7f;
  181. if (!(**buf & 0x80)) {
  182. ++*buf;
  183. return n;
  184. }
  185. }
  186. return 0;
  187. }
  188. /**
  189. * Based off parse_packed_headers in Vorbis RTP
  190. */
  191. static unsigned int
  192. parse_packed_headers(const uint8_t * packed_headers,
  193. const uint8_t * packed_headers_end,
  194. AVCodecContext * codec, PayloadContext * xiph_data)
  195. {
  196. unsigned num_packed, num_headers, length, length1, length2, extradata_alloc;
  197. uint8_t *ptr;
  198. if (packed_headers_end - packed_headers < 9) {
  199. av_log(codec, AV_LOG_ERROR,
  200. "Invalid %td byte packed header.",
  201. packed_headers_end - packed_headers);
  202. return AVERROR_INVALIDDATA;
  203. }
  204. num_packed = bytestream_get_be32(&packed_headers);
  205. xiph_data->ident = bytestream_get_be24(&packed_headers);
  206. length = bytestream_get_be16(&packed_headers);
  207. num_headers = get_base128(&packed_headers, packed_headers_end);
  208. length1 = get_base128(&packed_headers, packed_headers_end);
  209. length2 = get_base128(&packed_headers, packed_headers_end);
  210. if (num_packed != 1 || num_headers > 3) {
  211. av_log(codec, AV_LOG_ERROR,
  212. "Unimplemented number of headers: %d packed headers, %d headers\n",
  213. num_packed, num_headers);
  214. return AVERROR_PATCHWELCOME;
  215. }
  216. if (packed_headers_end - packed_headers != length ||
  217. length1 > length || length2 > length - length1) {
  218. av_log(codec, AV_LOG_ERROR,
  219. "Bad packed header lengths (%d,%d,%td,%d)\n", length1,
  220. length2, packed_headers_end - packed_headers, length);
  221. return AVERROR_INVALIDDATA;
  222. }
  223. /* allocate extra space:
  224. * -- length/255 +2 for xiphlacing
  225. * -- one for the '2' marker
  226. * -- FF_INPUT_BUFFER_PADDING_SIZE required */
  227. extradata_alloc = length + length/255 + 3 + FF_INPUT_BUFFER_PADDING_SIZE;
  228. ptr = codec->extradata = av_malloc(extradata_alloc);
  229. if (!ptr) {
  230. av_log(codec, AV_LOG_ERROR, "Out of memory\n");
  231. return AVERROR(ENOMEM);
  232. }
  233. *ptr++ = 2;
  234. ptr += av_xiphlacing(ptr, length1);
  235. ptr += av_xiphlacing(ptr, length2);
  236. memcpy(ptr, packed_headers, length);
  237. ptr += length;
  238. codec->extradata_size = ptr - codec->extradata;
  239. // clear out remaining parts of the buffer
  240. memset(ptr, 0, extradata_alloc - codec->extradata_size);
  241. return 0;
  242. }
  243. static int xiph_parse_fmtp_pair(AVStream* stream,
  244. PayloadContext *xiph_data,
  245. char *attr, char *value)
  246. {
  247. AVCodecContext *codec = stream->codec;
  248. int result = 0;
  249. if (!strcmp(attr, "sampling")) {
  250. return AVERROR_PATCHWELCOME;
  251. } else if (!strcmp(attr, "width")) {
  252. /* This is an integer between 1 and 1048561
  253. * and MUST be in multiples of 16. */
  254. codec->width = atoi(value);
  255. return 0;
  256. } else if (!strcmp(attr, "height")) {
  257. /* This is an integer between 1 and 1048561
  258. * and MUST be in multiples of 16. */
  259. codec->height = atoi(value);
  260. return 0;
  261. } else if (!strcmp(attr, "delivery-method")) {
  262. /* Possible values are: inline, in_band, out_band/specific_name. */
  263. return AVERROR_PATCHWELCOME;
  264. } else if (!strcmp(attr, "configuration-uri")) {
  265. /* NOTE: configuration-uri is supported only under 2 conditions:
  266. *--after the delivery-method tag
  267. * --with a delivery-method value of out_band */
  268. return AVERROR_PATCHWELCOME;
  269. } else if (!strcmp(attr, "configuration")) {
  270. /* NOTE: configuration is supported only AFTER the delivery-method tag
  271. * The configuration value is a base64 encoded packed header */
  272. uint8_t *decoded_packet = NULL;
  273. int packet_size;
  274. size_t decoded_alloc = strlen(value) / 4 * 3 + 4;
  275. if (decoded_alloc <= INT_MAX) {
  276. decoded_packet = av_malloc(decoded_alloc);
  277. if (decoded_packet) {
  278. packet_size =
  279. av_base64_decode(decoded_packet, value, decoded_alloc);
  280. result = parse_packed_headers
  281. (decoded_packet, decoded_packet + packet_size, codec,
  282. xiph_data);
  283. } else {
  284. av_log(codec, AV_LOG_ERROR,
  285. "Out of memory while decoding SDP configuration.\n");
  286. result = AVERROR(ENOMEM);
  287. }
  288. } else {
  289. av_log(codec, AV_LOG_ERROR, "Packet too large\n");
  290. result = AVERROR_INVALIDDATA;
  291. }
  292. av_free(decoded_packet);
  293. }
  294. return result;
  295. }
  296. static int xiph_parse_sdp_line(AVFormatContext *s, int st_index,
  297. PayloadContext *data, const char *line)
  298. {
  299. const char *p;
  300. if (av_strstart(line, "fmtp:", &p)) {
  301. return ff_parse_fmtp(s->streams[st_index], data, p,
  302. xiph_parse_fmtp_pair);
  303. }
  304. return 0;
  305. }
  306. RTPDynamicProtocolHandler ff_theora_dynamic_handler = {
  307. .enc_name = "theora",
  308. .codec_type = AVMEDIA_TYPE_VIDEO,
  309. .codec_id = CODEC_ID_THEORA,
  310. .parse_sdp_a_line = xiph_parse_sdp_line,
  311. .open = xiph_new_context,
  312. .close = xiph_free_context,
  313. .parse_packet = xiph_handle_packet
  314. };
  315. RTPDynamicProtocolHandler ff_vorbis_dynamic_handler = {
  316. .enc_name = "vorbis",
  317. .codec_type = AVMEDIA_TYPE_AUDIO,
  318. .codec_id = CODEC_ID_VORBIS,
  319. .parse_sdp_a_line = xiph_parse_sdp_line,
  320. .open = xiph_new_context,
  321. .close = xiph_free_context,
  322. .parse_packet = xiph_handle_packet
  323. };