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.

390 lines
13KB

  1. /*
  2. * Xiph RTP Protocols
  3. * Copyright (c) 2009 Colin McQuillian
  4. * Copyright (c) 2010 Josh Allmann
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; 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/attributes.h"
  29. #include "libavutil/avstring.h"
  30. #include "libavutil/base64.h"
  31. #include "libavcodec/bytestream.h"
  32. #include <assert.h>
  33. #include "avio_internal.h"
  34. #include "rtpdec.h"
  35. #include "rtpdec_formats.h"
  36. /**
  37. * RTP/Xiph specific private data.
  38. */
  39. struct PayloadContext {
  40. unsigned ident; ///< 24-bit stream configuration identifier
  41. uint32_t timestamp;
  42. AVIOContext* fragment; ///< buffer for split payloads
  43. uint8_t *split_buf;
  44. int split_pos, split_buf_len, split_buf_size;
  45. int split_pkts;
  46. };
  47. static void xiph_close_context(PayloadContext * data)
  48. {
  49. ffio_free_dyn_buf(&data->fragment);
  50. av_free(data->split_buf);
  51. }
  52. static int xiph_handle_packet(AVFormatContext *ctx, PayloadContext *data,
  53. AVStream *st, AVPacket *pkt, uint32_t *timestamp,
  54. const uint8_t *buf, int len, uint16_t seq,
  55. int flags)
  56. {
  57. int ident, fragmented, tdt, num_pkts, pkt_len;
  58. if (!buf) {
  59. if (!data->split_buf || data->split_pos + 2 > data->split_buf_len ||
  60. data->split_pkts <= 0) {
  61. av_log(ctx, AV_LOG_ERROR, "No more data to return\n");
  62. return AVERROR_INVALIDDATA;
  63. }
  64. pkt_len = AV_RB16(data->split_buf + data->split_pos);
  65. data->split_pos += 2;
  66. if (data->split_pos + pkt_len > data->split_buf_len) {
  67. av_log(ctx, AV_LOG_ERROR, "Not enough data to return\n");
  68. return AVERROR_INVALIDDATA;
  69. }
  70. if (av_new_packet(pkt, pkt_len)) {
  71. av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
  72. return AVERROR(ENOMEM);
  73. }
  74. pkt->stream_index = st->index;
  75. memcpy(pkt->data, data->split_buf + data->split_pos, pkt_len);
  76. data->split_pos += pkt_len;
  77. data->split_pkts--;
  78. return data->split_pkts > 0;
  79. }
  80. if (len < 6) {
  81. av_log(ctx, AV_LOG_ERROR, "Invalid %d byte packet\n", len);
  82. return AVERROR_INVALIDDATA;
  83. }
  84. // read xiph rtp headers
  85. ident = AV_RB24(buf);
  86. fragmented = buf[3] >> 6;
  87. tdt = (buf[3] >> 4) & 3;
  88. num_pkts = buf[3] & 0xf;
  89. pkt_len = AV_RB16(buf + 4);
  90. if (pkt_len > len - 6) {
  91. av_log(ctx, AV_LOG_ERROR,
  92. "Invalid packet length %d in %d byte packet\n", pkt_len,
  93. len);
  94. return AVERROR_INVALIDDATA;
  95. }
  96. if (ident != data->ident) {
  97. av_log(ctx, AV_LOG_ERROR,
  98. "Unimplemented Xiph SDP configuration change detected\n");
  99. return AVERROR_PATCHWELCOME;
  100. }
  101. if (tdt) {
  102. av_log(ctx, AV_LOG_ERROR,
  103. "Unimplemented RTP Xiph packet settings (%d,%d,%d)\n",
  104. fragmented, tdt, num_pkts);
  105. return AVERROR_PATCHWELCOME;
  106. }
  107. buf += 6; // move past header bits
  108. len -= 6;
  109. if (fragmented == 0) {
  110. if (av_new_packet(pkt, pkt_len)) {
  111. av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
  112. return AVERROR(ENOMEM);
  113. }
  114. pkt->stream_index = st->index;
  115. memcpy(pkt->data, buf, pkt_len);
  116. buf += pkt_len;
  117. len -= pkt_len;
  118. num_pkts--;
  119. if (num_pkts > 0) {
  120. if (len > data->split_buf_size || !data->split_buf) {
  121. av_freep(&data->split_buf);
  122. data->split_buf_size = 2 * len;
  123. data->split_buf = av_malloc(data->split_buf_size);
  124. if (!data->split_buf) {
  125. av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
  126. av_packet_unref(pkt);
  127. return AVERROR(ENOMEM);
  128. }
  129. }
  130. memcpy(data->split_buf, buf, len);
  131. data->split_buf_len = len;
  132. data->split_pos = 0;
  133. data->split_pkts = num_pkts;
  134. return 1;
  135. }
  136. return 0;
  137. } else if (fragmented == 1) {
  138. // start of xiph data fragment
  139. int res;
  140. // end packet has been lost somewhere, so drop buffered data
  141. ffio_free_dyn_buf(&data->fragment);
  142. if((res = avio_open_dyn_buf(&data->fragment)) < 0)
  143. return res;
  144. avio_write(data->fragment, buf, pkt_len);
  145. data->timestamp = *timestamp;
  146. } else {
  147. assert(fragmented < 4);
  148. if (data->timestamp != *timestamp) {
  149. // skip if fragmented timestamp is incorrect;
  150. // a start packet has been lost somewhere
  151. ffio_free_dyn_buf(&data->fragment);
  152. av_log(ctx, AV_LOG_ERROR, "RTP timestamps don't match!\n");
  153. return AVERROR_INVALIDDATA;
  154. }
  155. if (!data->fragment) {
  156. av_log(ctx, AV_LOG_WARNING,
  157. "Received packet without a start fragment; dropping.\n");
  158. return AVERROR(EAGAIN);
  159. }
  160. // copy data to fragment buffer
  161. avio_write(data->fragment, buf, pkt_len);
  162. if (fragmented == 3) {
  163. // end of xiph data packet
  164. int ret = ff_rtp_finalize_packet(pkt, &data->fragment, st->index);
  165. if (ret < 0) {
  166. av_log(ctx, AV_LOG_ERROR,
  167. "Error occurred when getting fragment buffer.");
  168. return ret;
  169. }
  170. return 0;
  171. }
  172. }
  173. return AVERROR(EAGAIN);
  174. }
  175. /**
  176. * Length encoding described in RFC5215 section 3.1.1.
  177. */
  178. static int get_base128(const uint8_t ** buf, const uint8_t * buf_end)
  179. {
  180. int n = 0;
  181. for (; *buf < buf_end; ++*buf) {
  182. n <<= 7;
  183. n += **buf & 0x7f;
  184. if (!(**buf & 0x80)) {
  185. ++*buf;
  186. return n;
  187. }
  188. }
  189. return 0;
  190. }
  191. /**
  192. * Based off parse_packed_headers in Vorbis RTP
  193. */
  194. static int
  195. parse_packed_headers(AVFormatContext *s,
  196. const uint8_t * packed_headers,
  197. const uint8_t * packed_headers_end,
  198. AVCodecParameters *par, PayloadContext * xiph_data)
  199. {
  200. unsigned num_packed, num_headers, length, length1, length2, extradata_alloc;
  201. uint8_t *ptr;
  202. if (packed_headers_end - packed_headers < 9) {
  203. av_log(s, AV_LOG_ERROR,
  204. "Invalid %td byte packed header.",
  205. packed_headers_end - packed_headers);
  206. return AVERROR_INVALIDDATA;
  207. }
  208. num_packed = bytestream_get_be32(&packed_headers);
  209. xiph_data->ident = bytestream_get_be24(&packed_headers);
  210. length = bytestream_get_be16(&packed_headers);
  211. num_headers = get_base128(&packed_headers, packed_headers_end);
  212. length1 = get_base128(&packed_headers, packed_headers_end);
  213. length2 = get_base128(&packed_headers, packed_headers_end);
  214. if (num_packed != 1 || num_headers > 3) {
  215. av_log(s, AV_LOG_ERROR,
  216. "Unimplemented number of headers: %d packed headers, %d headers\n",
  217. num_packed, num_headers);
  218. return AVERROR_PATCHWELCOME;
  219. }
  220. if (packed_headers_end - packed_headers != length ||
  221. length1 > length || length2 > length - length1) {
  222. av_log(s, AV_LOG_ERROR,
  223. "Bad packed header lengths (%d,%d,%td,%d)\n", length1,
  224. length2, packed_headers_end - packed_headers, length);
  225. return AVERROR_INVALIDDATA;
  226. }
  227. /* allocate extra space:
  228. * -- length/255 +2 for xiphlacing
  229. * -- one for the '2' marker
  230. * -- AV_INPUT_BUFFER_PADDING_SIZE required */
  231. extradata_alloc = length + length/255 + 3 + AV_INPUT_BUFFER_PADDING_SIZE;
  232. ptr = par->extradata = av_malloc(extradata_alloc);
  233. if (!ptr) {
  234. av_log(s, AV_LOG_ERROR, "Out of memory\n");
  235. return AVERROR(ENOMEM);
  236. }
  237. *ptr++ = 2;
  238. ptr += av_xiphlacing(ptr, length1);
  239. ptr += av_xiphlacing(ptr, length2);
  240. memcpy(ptr, packed_headers, length);
  241. ptr += length;
  242. par->extradata_size = ptr - par->extradata;
  243. // clear out remaining parts of the buffer
  244. memset(ptr, 0, extradata_alloc - par->extradata_size);
  245. return 0;
  246. }
  247. static int xiph_parse_fmtp_pair(AVFormatContext *s,
  248. AVStream* stream,
  249. PayloadContext *xiph_data,
  250. const char *attr, const char *value)
  251. {
  252. AVCodecParameters *par = stream->codecpar;
  253. int result = 0;
  254. if (!strcmp(attr, "sampling")) {
  255. if (!strcmp(value, "YCbCr-4:2:0")) {
  256. par->format = AV_PIX_FMT_YUV420P;
  257. } else if (!strcmp(value, "YCbCr-4:4:2")) {
  258. par->format = AV_PIX_FMT_YUV422P;
  259. } else if (!strcmp(value, "YCbCr-4:4:4")) {
  260. par->format = AV_PIX_FMT_YUV444P;
  261. } else {
  262. av_log(s, AV_LOG_ERROR,
  263. "Unsupported pixel format %s\n", attr);
  264. return AVERROR_INVALIDDATA;
  265. }
  266. } else if (!strcmp(attr, "width")) {
  267. /* This is an integer between 1 and 1048561
  268. * and MUST be in multiples of 16. */
  269. par->width = atoi(value);
  270. return 0;
  271. } else if (!strcmp(attr, "height")) {
  272. /* This is an integer between 1 and 1048561
  273. * and MUST be in multiples of 16. */
  274. par->height = atoi(value);
  275. return 0;
  276. } else if (!strcmp(attr, "delivery-method")) {
  277. /* Possible values are: inline, in_band, out_band/specific_name. */
  278. return AVERROR_PATCHWELCOME;
  279. } else if (!strcmp(attr, "configuration-uri")) {
  280. /* NOTE: configuration-uri is supported only under 2 conditions:
  281. *--after the delivery-method tag
  282. * --with a delivery-method value of out_band */
  283. return AVERROR_PATCHWELCOME;
  284. } else if (!strcmp(attr, "configuration")) {
  285. /* NOTE: configuration is supported only AFTER the delivery-method tag
  286. * The configuration value is a base64 encoded packed header */
  287. uint8_t *decoded_packet = NULL;
  288. int packet_size;
  289. size_t decoded_alloc = strlen(value) / 4 * 3 + 4;
  290. if (decoded_alloc <= INT_MAX) {
  291. decoded_packet = av_malloc(decoded_alloc);
  292. if (decoded_packet) {
  293. packet_size =
  294. av_base64_decode(decoded_packet, value, decoded_alloc);
  295. result = parse_packed_headers
  296. (s, decoded_packet, decoded_packet + packet_size, par,
  297. xiph_data);
  298. } else {
  299. av_log(s, AV_LOG_ERROR,
  300. "Out of memory while decoding SDP configuration.\n");
  301. result = AVERROR(ENOMEM);
  302. }
  303. } else {
  304. av_log(s, AV_LOG_ERROR, "Packet too large\n");
  305. result = AVERROR_INVALIDDATA;
  306. }
  307. av_free(decoded_packet);
  308. }
  309. return result;
  310. }
  311. static int xiph_parse_sdp_line(AVFormatContext *s, int st_index,
  312. PayloadContext *data, const char *line)
  313. {
  314. const char *p;
  315. if (st_index < 0)
  316. return 0;
  317. if (av_strstart(line, "fmtp:", &p)) {
  318. return ff_parse_fmtp(s, s->streams[st_index], data, p,
  319. xiph_parse_fmtp_pair);
  320. }
  321. return 0;
  322. }
  323. RTPDynamicProtocolHandler ff_theora_dynamic_handler = {
  324. .enc_name = "theora",
  325. .codec_type = AVMEDIA_TYPE_VIDEO,
  326. .codec_id = AV_CODEC_ID_THEORA,
  327. .priv_data_size = sizeof(PayloadContext),
  328. .parse_sdp_a_line = xiph_parse_sdp_line,
  329. .close = xiph_close_context,
  330. .parse_packet = xiph_handle_packet,
  331. };
  332. RTPDynamicProtocolHandler ff_vorbis_dynamic_handler = {
  333. .enc_name = "vorbis",
  334. .codec_type = AVMEDIA_TYPE_AUDIO,
  335. .codec_id = AV_CODEC_ID_VORBIS,
  336. .need_parsing = AVSTREAM_PARSE_HEADERS,
  337. .priv_data_size = sizeof(PayloadContext),
  338. .parse_sdp_a_line = xiph_parse_sdp_line,
  339. .close = xiph_close_context,
  340. .parse_packet = xiph_handle_packet,
  341. };