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.

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