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.

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