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.

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