rtpdec_xiph.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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/avstring.h"
  29. #include "libavutil/base64.h"
  30. #include "libavcodec/bytestream.h"
  31. #include <assert.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. assert(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. av_init_packet(pkt);
  179. pkt->size = avio_close_dyn_buf(data->fragment, &pkt->data);
  180. if (pkt->size < 0) {
  181. av_log(ctx, AV_LOG_ERROR,
  182. "Error occurred when getting fragment buffer.");
  183. return pkt->size;
  184. }
  185. pkt->stream_index = st->index;
  186. pkt->destruct = av_destruct_packet;
  187. data->fragment = NULL;
  188. return 0;
  189. }
  190. }
  191. return AVERROR(EAGAIN);
  192. }
  193. /**
  194. * Length encoding described in RFC5215 section 3.1.1.
  195. */
  196. static int get_base128(const uint8_t ** buf, const uint8_t * buf_end)
  197. {
  198. int n = 0;
  199. for (; *buf < buf_end; ++*buf) {
  200. n <<= 7;
  201. n += **buf & 0x7f;
  202. if (!(**buf & 0x80)) {
  203. ++*buf;
  204. return n;
  205. }
  206. }
  207. return 0;
  208. }
  209. /**
  210. * Based off parse_packed_headers in Vorbis RTP
  211. */
  212. static unsigned int
  213. parse_packed_headers(const uint8_t * packed_headers,
  214. const uint8_t * packed_headers_end,
  215. AVCodecContext * codec, PayloadContext * xiph_data)
  216. {
  217. unsigned num_packed, num_headers, length, length1, length2, extradata_alloc;
  218. uint8_t *ptr;
  219. if (packed_headers_end - packed_headers < 9) {
  220. av_log(codec, AV_LOG_ERROR,
  221. "Invalid %td byte packed header.",
  222. packed_headers_end - packed_headers);
  223. return AVERROR_INVALIDDATA;
  224. }
  225. num_packed = bytestream_get_be32(&packed_headers);
  226. xiph_data->ident = bytestream_get_be24(&packed_headers);
  227. length = bytestream_get_be16(&packed_headers);
  228. num_headers = get_base128(&packed_headers, packed_headers_end);
  229. length1 = get_base128(&packed_headers, packed_headers_end);
  230. length2 = get_base128(&packed_headers, packed_headers_end);
  231. if (num_packed != 1 || num_headers > 3) {
  232. av_log(codec, AV_LOG_ERROR,
  233. "Unimplemented number of headers: %d packed headers, %d headers\n",
  234. num_packed, num_headers);
  235. return AVERROR_PATCHWELCOME;
  236. }
  237. if (packed_headers_end - packed_headers != length ||
  238. length1 > length || length2 > length - length1) {
  239. av_log(codec, AV_LOG_ERROR,
  240. "Bad packed header lengths (%d,%d,%td,%d)\n", length1,
  241. length2, packed_headers_end - packed_headers, length);
  242. return AVERROR_INVALIDDATA;
  243. }
  244. /* allocate extra space:
  245. * -- length/255 +2 for xiphlacing
  246. * -- one for the '2' marker
  247. * -- FF_INPUT_BUFFER_PADDING_SIZE required */
  248. extradata_alloc = length + length/255 + 3 + FF_INPUT_BUFFER_PADDING_SIZE;
  249. ptr = codec->extradata = av_malloc(extradata_alloc);
  250. if (!ptr) {
  251. av_log(codec, AV_LOG_ERROR, "Out of memory\n");
  252. return AVERROR(ENOMEM);
  253. }
  254. *ptr++ = 2;
  255. ptr += av_xiphlacing(ptr, length1);
  256. ptr += av_xiphlacing(ptr, length2);
  257. memcpy(ptr, packed_headers, length);
  258. ptr += length;
  259. codec->extradata_size = ptr - codec->extradata;
  260. // clear out remaining parts of the buffer
  261. memset(ptr, 0, extradata_alloc - codec->extradata_size);
  262. return 0;
  263. }
  264. static int xiph_parse_fmtp_pair(AVStream* stream,
  265. PayloadContext *xiph_data,
  266. char *attr, char *value)
  267. {
  268. AVCodecContext *codec = stream->codec;
  269. int result = 0;
  270. if (!strcmp(attr, "sampling")) {
  271. if (!strcmp(value, "YCbCr-4:2:0")) {
  272. codec->pix_fmt = PIX_FMT_YUV420P;
  273. } else if (!strcmp(value, "YCbCr-4:4:2")) {
  274. codec->pix_fmt = PIX_FMT_YUV422P;
  275. } else if (!strcmp(value, "YCbCr-4:4:4")) {
  276. codec->pix_fmt = PIX_FMT_YUV444P;
  277. } else {
  278. av_log(codec, AV_LOG_ERROR,
  279. "Unsupported pixel format %s\n", attr);
  280. return AVERROR_INVALIDDATA;
  281. }
  282. } else if (!strcmp(attr, "width")) {
  283. /* This is an integer between 1 and 1048561
  284. * and MUST be in multiples of 16. */
  285. codec->width = atoi(value);
  286. return 0;
  287. } else if (!strcmp(attr, "height")) {
  288. /* This is an integer between 1 and 1048561
  289. * and MUST be in multiples of 16. */
  290. codec->height = atoi(value);
  291. return 0;
  292. } else if (!strcmp(attr, "delivery-method")) {
  293. /* Possible values are: inline, in_band, out_band/specific_name. */
  294. return AVERROR_PATCHWELCOME;
  295. } else if (!strcmp(attr, "configuration-uri")) {
  296. /* NOTE: configuration-uri is supported only under 2 conditions:
  297. *--after the delivery-method tag
  298. * --with a delivery-method value of out_band */
  299. return AVERROR_PATCHWELCOME;
  300. } else if (!strcmp(attr, "configuration")) {
  301. /* NOTE: configuration is supported only AFTER the delivery-method tag
  302. * The configuration value is a base64 encoded packed header */
  303. uint8_t *decoded_packet = NULL;
  304. int packet_size;
  305. size_t decoded_alloc = strlen(value) / 4 * 3 + 4;
  306. if (decoded_alloc <= INT_MAX) {
  307. decoded_packet = av_malloc(decoded_alloc);
  308. if (decoded_packet) {
  309. packet_size =
  310. av_base64_decode(decoded_packet, value, decoded_alloc);
  311. result = parse_packed_headers
  312. (decoded_packet, decoded_packet + packet_size, codec,
  313. xiph_data);
  314. } else {
  315. av_log(codec, AV_LOG_ERROR,
  316. "Out of memory while decoding SDP configuration.\n");
  317. result = AVERROR(ENOMEM);
  318. }
  319. } else {
  320. av_log(codec, AV_LOG_ERROR, "Packet too large\n");
  321. result = AVERROR_INVALIDDATA;
  322. }
  323. av_free(decoded_packet);
  324. }
  325. return result;
  326. }
  327. static int xiph_parse_sdp_line(AVFormatContext *s, int st_index,
  328. PayloadContext *data, const char *line)
  329. {
  330. const char *p;
  331. if (av_strstart(line, "fmtp:", &p)) {
  332. return ff_parse_fmtp(s->streams[st_index], data, p,
  333. xiph_parse_fmtp_pair);
  334. }
  335. return 0;
  336. }
  337. RTPDynamicProtocolHandler ff_theora_dynamic_handler = {
  338. .enc_name = "theora",
  339. .codec_type = AVMEDIA_TYPE_VIDEO,
  340. .codec_id = CODEC_ID_THEORA,
  341. .parse_sdp_a_line = xiph_parse_sdp_line,
  342. .alloc = xiph_new_context,
  343. .free = xiph_free_context,
  344. .parse_packet = xiph_handle_packet
  345. };
  346. RTPDynamicProtocolHandler ff_vorbis_dynamic_handler = {
  347. .enc_name = "vorbis",
  348. .codec_type = AVMEDIA_TYPE_AUDIO,
  349. .codec_id = CODEC_ID_VORBIS,
  350. .parse_sdp_a_line = xiph_parse_sdp_line,
  351. .alloc = xiph_new_context,
  352. .free = xiph_free_context,
  353. .parse_packet = xiph_handle_packet
  354. };