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.

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