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.

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