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.

387 lines
12KB

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