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.

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