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.

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