rtp_vorbis.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * RTP Vorbis Protocol (RFC5215)
  3. * Copyright (c) 2009 Colin McQuillan
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file libavformat/rtp_vorbis.c
  23. * @brief Vorbis / RTP Code (RFC 5215)
  24. * @author Colin McQuillan <m.niloc@gmail.com>
  25. */
  26. #include "libavutil/base64.h"
  27. #include "libavutil/avstring.h"
  28. #include "libavcodec/bytestream.h"
  29. #include <assert.h>
  30. #include "rtpdec.h"
  31. #include "rtp_vorbis.h"
  32. /**
  33. * RTP/Vorbis specific private data.
  34. */
  35. struct PayloadContext {
  36. unsigned ident; ///< 24-bit stream configuration identifier
  37. };
  38. /**
  39. * Length encoding described in RFC5215 section 3.1.1.
  40. */
  41. static int get_base128(const uint8_t ** buf, const uint8_t * buf_end)
  42. {
  43. int n = 0;
  44. for (; *buf < buf_end; ++*buf) {
  45. n <<= 7;
  46. n += **buf & 0x7f;
  47. if (!(**buf & 0x80)) {
  48. ++*buf;
  49. return n;
  50. }
  51. }
  52. return 0;
  53. }
  54. /**
  55. * Out-of-band headers, described in RFC 5251 section 3.2.1
  56. */
  57. static unsigned int
  58. parse_packed_headers(const uint8_t * packed_headers,
  59. const uint8_t * packed_headers_end,
  60. AVCodecContext * codec, PayloadContext * vorbis_data)
  61. {
  62. unsigned num_packed, num_headers, length, length1, length2;
  63. uint8_t *ptr;
  64. num_packed = bytestream_get_be32(&packed_headers);
  65. vorbis_data->ident = bytestream_get_be24(&packed_headers);
  66. length = bytestream_get_be16(&packed_headers);
  67. num_headers = get_base128(&packed_headers, packed_headers_end);
  68. length1 = get_base128(&packed_headers, packed_headers_end);
  69. length2 = get_base128(&packed_headers, packed_headers_end);
  70. if (num_packed != 1 || num_headers > 3) {
  71. av_log(codec, AV_LOG_ERROR,
  72. "Unimplemented number of headers: %d packed headers, %d headers\n",
  73. num_packed, num_headers);
  74. return AVERROR_PATCHWELCOME;
  75. }
  76. if (packed_headers_end - packed_headers != length ||
  77. length1 > length || length2 > length - length1) {
  78. av_log(codec, AV_LOG_ERROR,
  79. "Bad packed header lengths (%d,%d,%d,%d)\n", length1,
  80. length2, packed_headers_end - packed_headers, length);
  81. return AVERROR_INVALIDDATA;
  82. }
  83. ptr = codec->extradata = av_mallocz(length + length / 255 + 64);
  84. if (!ptr) {
  85. av_log(codec, AV_LOG_ERROR, "Out of memory");
  86. return AVERROR_NOMEM;
  87. }
  88. *ptr++ = 2;
  89. ptr += av_xiphlacing(ptr, length1);
  90. ptr += av_xiphlacing(ptr, length2);
  91. memcpy(ptr, packed_headers, length);
  92. ptr += length;
  93. codec->extradata_size = ptr - codec->extradata;
  94. return 0;
  95. }
  96. int
  97. ff_vorbis_parse_fmtp_config(AVCodecContext * codec,
  98. void *vorbis_data, char *attr, char *value)
  99. {
  100. int result = 0;
  101. assert(codec->codec_id == CODEC_ID_VORBIS);
  102. assert(vorbis_data);
  103. // The configuration value is a base64 encoded packed header
  104. if (!strcmp(attr, "configuration")) {
  105. uint8_t *decoded_packet = NULL;
  106. int packet_size;
  107. size_t decoded_alloc = strlen(value) / 4 * 3 + 4;
  108. if (decoded_alloc <= INT_MAX) {
  109. decoded_packet = av_malloc(decoded_alloc);
  110. if (decoded_packet) {
  111. packet_size =
  112. av_base64_decode(decoded_packet, value, decoded_alloc);
  113. result = parse_packed_headers
  114. (decoded_packet, decoded_packet + packet_size, codec,
  115. vorbis_data);
  116. } else {
  117. av_log(codec, AV_LOG_ERROR,
  118. "Out of memory while decoding SDP configuration.\n");
  119. result = AVERROR_NOMEM;
  120. }
  121. } else {
  122. av_log(codec, AV_LOG_ERROR, "Packet too large\n");
  123. result = AVERROR_INVALIDDATA;
  124. }
  125. av_free(decoded_packet);
  126. }
  127. return result;
  128. }
  129. static PayloadContext *vorbis_new_extradata(void)
  130. {
  131. return av_mallocz(sizeof(PayloadContext));
  132. }
  133. static void vorbis_free_extradata(PayloadContext * data)
  134. {
  135. av_free(data);
  136. }
  137. /**
  138. * Handle payload as described in RFC 5215 section 2.2
  139. */
  140. static int
  141. vorbis_handle_packet(AVFormatContext * ctx,
  142. PayloadContext * data,
  143. AVStream * st,
  144. AVPacket * pkt,
  145. uint32_t * timestamp,
  146. const uint8_t * buf, int len, int flags)
  147. {
  148. int ident, fragmented, vdt, num_pkts, pkt_len;
  149. if (len < 6) {
  150. av_log(ctx, AV_LOG_ERROR, "Invalid %d byte packet\n", len);
  151. return AVERROR_INVALIDDATA;
  152. }
  153. ident = AV_RB24(buf);
  154. fragmented = buf[3] >> 6;
  155. vdt = (buf[3] >> 4) & 3;
  156. num_pkts = buf[3] & 7;
  157. pkt_len = AV_RB16(buf + 4);
  158. if (pkt_len > len - 6) {
  159. av_log(ctx, AV_LOG_ERROR,
  160. "Invalid packet length %d in %d byte packet\n", pkt_len,
  161. len);
  162. return AVERROR_INVALIDDATA;
  163. }
  164. if (ident != data->ident) {
  165. av_log(ctx, AV_LOG_ERROR,
  166. "Unimplemented Vorbis SDP configuration change detected\n");
  167. return AVERROR_PATCHWELCOME;
  168. }
  169. if (fragmented != 0 || vdt != 0 || num_pkts != 1) {
  170. av_log(ctx, AV_LOG_ERROR,
  171. "Unimplemented RTP Vorbis packet settings (%d,%d,%d)\n",
  172. fragmented, vdt, num_pkts);
  173. return AVERROR_PATCHWELCOME;
  174. }
  175. if (av_new_packet(pkt, pkt_len)) {
  176. av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
  177. return AVERROR_NOMEM;
  178. }
  179. memcpy(pkt->data, buf + 6, pkt_len);
  180. pkt->stream_index = st->index;
  181. return 0;
  182. }
  183. RTPDynamicProtocolHandler ff_vorbis_dynamic_handler = {
  184. "vorbis",
  185. CODEC_TYPE_AUDIO,
  186. CODEC_ID_VORBIS,
  187. NULL,
  188. vorbis_new_extradata,
  189. vorbis_free_extradata,
  190. vorbis_handle_packet
  191. };