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.

389 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_free_packet(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(const uint8_t * packed_headers,
  196. const uint8_t * packed_headers_end,
  197. AVCodecContext * codec, 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(codec, 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. av_log(codec, AV_LOG_ERROR,
  215. "Unimplemented number of headers: %d packed headers, %d headers\n",
  216. num_packed, num_headers);
  217. return AVERROR_PATCHWELCOME;
  218. }
  219. if (packed_headers_end - packed_headers != length ||
  220. length1 > length || length2 > length - length1) {
  221. av_log(codec, AV_LOG_ERROR,
  222. "Bad packed header lengths (%d,%d,%td,%d)\n", length1,
  223. length2, packed_headers_end - packed_headers, length);
  224. return AVERROR_INVALIDDATA;
  225. }
  226. /* allocate extra space:
  227. * -- length/255 +2 for xiphlacing
  228. * -- one for the '2' marker
  229. * -- FF_INPUT_BUFFER_PADDING_SIZE required */
  230. extradata_alloc = length + length/255 + 3 + FF_INPUT_BUFFER_PADDING_SIZE;
  231. ptr = codec->extradata = av_malloc(extradata_alloc);
  232. if (!ptr) {
  233. av_log(codec, AV_LOG_ERROR, "Out of memory\n");
  234. return AVERROR(ENOMEM);
  235. }
  236. *ptr++ = 2;
  237. ptr += av_xiphlacing(ptr, length1);
  238. ptr += av_xiphlacing(ptr, length2);
  239. memcpy(ptr, packed_headers, length);
  240. ptr += length;
  241. codec->extradata_size = ptr - codec->extradata;
  242. // clear out remaining parts of the buffer
  243. memset(ptr, 0, extradata_alloc - codec->extradata_size);
  244. return 0;
  245. }
  246. static int xiph_parse_fmtp_pair(AVFormatContext *s,
  247. AVStream* stream,
  248. PayloadContext *xiph_data,
  249. const char *attr, const char *value)
  250. {
  251. AVCodecContext *codec = stream->codec;
  252. int result = 0;
  253. if (!strcmp(attr, "sampling")) {
  254. if (!strcmp(value, "YCbCr-4:2:0")) {
  255. codec->pix_fmt = AV_PIX_FMT_YUV420P;
  256. } else if (!strcmp(value, "YCbCr-4:4:2")) {
  257. codec->pix_fmt = AV_PIX_FMT_YUV422P;
  258. } else if (!strcmp(value, "YCbCr-4:4:4")) {
  259. codec->pix_fmt = AV_PIX_FMT_YUV444P;
  260. } else {
  261. av_log(s, AV_LOG_ERROR,
  262. "Unsupported pixel format %s\n", attr);
  263. return AVERROR_INVALIDDATA;
  264. }
  265. } else if (!strcmp(attr, "width")) {
  266. /* This is an integer between 1 and 1048561
  267. * and MUST be in multiples of 16. */
  268. codec->width = atoi(value);
  269. return 0;
  270. } else if (!strcmp(attr, "height")) {
  271. /* This is an integer between 1 and 1048561
  272. * and MUST be in multiples of 16. */
  273. codec->height = atoi(value);
  274. return 0;
  275. } else if (!strcmp(attr, "delivery-method")) {
  276. /* Possible values are: inline, in_band, out_band/specific_name. */
  277. return AVERROR_PATCHWELCOME;
  278. } else if (!strcmp(attr, "configuration-uri")) {
  279. /* NOTE: configuration-uri is supported only under 2 conditions:
  280. *--after the delivery-method tag
  281. * --with a delivery-method value of out_band */
  282. return AVERROR_PATCHWELCOME;
  283. } else if (!strcmp(attr, "configuration")) {
  284. /* NOTE: configuration is supported only AFTER the delivery-method tag
  285. * The configuration value is a base64 encoded packed header */
  286. uint8_t *decoded_packet = NULL;
  287. int packet_size;
  288. size_t decoded_alloc = strlen(value) / 4 * 3 + 4;
  289. if (decoded_alloc <= INT_MAX) {
  290. decoded_packet = av_malloc(decoded_alloc);
  291. if (decoded_packet) {
  292. packet_size =
  293. av_base64_decode(decoded_packet, value, decoded_alloc);
  294. result = parse_packed_headers
  295. (decoded_packet, decoded_packet + packet_size, codec,
  296. xiph_data);
  297. } else {
  298. av_log(s, AV_LOG_ERROR,
  299. "Out of memory while decoding SDP configuration.\n");
  300. result = AVERROR(ENOMEM);
  301. }
  302. } else {
  303. av_log(s, AV_LOG_ERROR, "Packet too large\n");
  304. result = AVERROR_INVALIDDATA;
  305. }
  306. av_free(decoded_packet);
  307. }
  308. return result;
  309. }
  310. static int xiph_parse_sdp_line(AVFormatContext *s, int st_index,
  311. PayloadContext *data, const char *line)
  312. {
  313. const char *p;
  314. if (st_index < 0)
  315. return 0;
  316. if (av_strstart(line, "fmtp:", &p)) {
  317. return ff_parse_fmtp(s, s->streams[st_index], data, p,
  318. xiph_parse_fmtp_pair);
  319. }
  320. return 0;
  321. }
  322. RTPDynamicProtocolHandler ff_theora_dynamic_handler = {
  323. .enc_name = "theora",
  324. .codec_type = AVMEDIA_TYPE_VIDEO,
  325. .codec_id = AV_CODEC_ID_THEORA,
  326. .priv_data_size = sizeof(PayloadContext),
  327. .parse_sdp_a_line = xiph_parse_sdp_line,
  328. .close = xiph_close_context,
  329. .parse_packet = xiph_handle_packet,
  330. };
  331. RTPDynamicProtocolHandler ff_vorbis_dynamic_handler = {
  332. .enc_name = "vorbis",
  333. .codec_type = AVMEDIA_TYPE_AUDIO,
  334. .codec_id = AV_CODEC_ID_VORBIS,
  335. .need_parsing = AVSTREAM_PARSE_HEADERS,
  336. .priv_data_size = sizeof(PayloadContext),
  337. .parse_sdp_a_line = xiph_parse_sdp_line,
  338. .close = xiph_close_context,
  339. .parse_packet = xiph_handle_packet,
  340. };