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