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