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.

213 lines
6.7KB

  1. /*
  2. * RTP AMR Depacketizer, RFC 3267
  3. * Copyright (c) 2010 Martin Storsjo
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/channel_layout.h"
  22. #include "avformat.h"
  23. #include "rtpdec_formats.h"
  24. #include "libavutil/avstring.h"
  25. static const uint8_t frame_sizes_nb[16] = {
  26. 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0
  27. };
  28. static const uint8_t frame_sizes_wb[16] = {
  29. 17, 23, 32, 36, 40, 46, 50, 58, 60, 5, 5, 0, 0, 0, 0, 0
  30. };
  31. struct PayloadContext {
  32. int octet_align;
  33. int crc;
  34. int interleaving;
  35. int channels;
  36. };
  37. static PayloadContext *amr_new_context(void)
  38. {
  39. PayloadContext *data = av_mallocz(sizeof(PayloadContext));
  40. if(!data) return data;
  41. data->channels = 1;
  42. return data;
  43. }
  44. static void amr_free_context(PayloadContext *data)
  45. {
  46. av_free(data);
  47. }
  48. static int amr_handle_packet(AVFormatContext *ctx,
  49. PayloadContext *data,
  50. AVStream *st,
  51. AVPacket * pkt,
  52. uint32_t * timestamp,
  53. const uint8_t * buf,
  54. int len, int flags)
  55. {
  56. const uint8_t *frame_sizes = NULL;
  57. int frames;
  58. int i;
  59. const uint8_t *speech_data;
  60. uint8_t *ptr;
  61. if (st->codec->codec_id == AV_CODEC_ID_AMR_NB) {
  62. frame_sizes = frame_sizes_nb;
  63. } else if (st->codec->codec_id == AV_CODEC_ID_AMR_WB) {
  64. frame_sizes = frame_sizes_wb;
  65. } else {
  66. av_log(ctx, AV_LOG_ERROR, "Bad codec ID\n");
  67. return AVERROR_INVALIDDATA;
  68. }
  69. if (st->codec->channels != 1) {
  70. av_log(ctx, AV_LOG_ERROR, "Only mono AMR is supported\n");
  71. return AVERROR_INVALIDDATA;
  72. }
  73. st->codec->channel_layout = AV_CH_LAYOUT_MONO;
  74. /* The AMR RTP packet consists of one header byte, followed
  75. * by one TOC byte for each AMR frame in the packet, followed
  76. * by the speech data for all the AMR frames.
  77. *
  78. * The header byte contains only a codec mode request, for
  79. * requesting what kind of AMR data the sender wants to
  80. * receive. Not used at the moment.
  81. */
  82. /* Count the number of frames in the packet. The highest bit
  83. * is set in a TOC byte if there are more frames following.
  84. */
  85. for (frames = 1; frames < len && (buf[frames] & 0x80); frames++) ;
  86. if (1 + frames >= len) {
  87. /* We hit the end of the packet while counting frames. */
  88. av_log(ctx, AV_LOG_ERROR, "No speech data found\n");
  89. return AVERROR_INVALIDDATA;
  90. }
  91. speech_data = buf + 1 + frames;
  92. /* Everything except the codec mode request byte should be output. */
  93. if (av_new_packet(pkt, len - 1)) {
  94. av_log(ctx, AV_LOG_ERROR, "Out of memory\n");
  95. return AVERROR(ENOMEM);
  96. }
  97. pkt->stream_index = st->index;
  98. ptr = pkt->data;
  99. for (i = 0; i < frames; i++) {
  100. uint8_t toc = buf[1 + i];
  101. int frame_size = frame_sizes[(toc >> 3) & 0x0f];
  102. if (speech_data + frame_size > buf + len) {
  103. /* Too little speech data */
  104. av_log(ctx, AV_LOG_WARNING, "Too little speech data in the RTP packet\n");
  105. /* Set the unwritten part of the packet to zero. */
  106. memset(ptr, 0, pkt->data + pkt->size - ptr);
  107. pkt->size = ptr - pkt->data;
  108. return 0;
  109. }
  110. /* Extract the AMR frame mode from the TOC byte */
  111. *ptr++ = toc & 0x7C;
  112. /* Copy the speech data */
  113. memcpy(ptr, speech_data, frame_size);
  114. speech_data += frame_size;
  115. ptr += frame_size;
  116. }
  117. if (speech_data < buf + len) {
  118. av_log(ctx, AV_LOG_WARNING, "Too much speech data in the RTP packet?\n");
  119. /* Set the unwritten part of the packet to zero. */
  120. memset(ptr, 0, pkt->data + pkt->size - ptr);
  121. pkt->size = ptr - pkt->data;
  122. }
  123. return 0;
  124. }
  125. static int amr_parse_fmtp(AVStream *stream, PayloadContext *data,
  126. char *attr, char *value)
  127. {
  128. /* Some AMR SDP configurations contain "octet-align", without
  129. * the trailing =1. Therefore, if the value is empty,
  130. * interpret it as "1".
  131. */
  132. if (!strcmp(value, "")) {
  133. av_log(NULL, AV_LOG_WARNING, "AMR fmtp attribute %s had "
  134. "nonstandard empty value\n", attr);
  135. strcpy(value, "1");
  136. }
  137. if (!strcmp(attr, "octet-align"))
  138. data->octet_align = atoi(value);
  139. else if (!strcmp(attr, "crc"))
  140. data->crc = atoi(value);
  141. else if (!strcmp(attr, "interleaving"))
  142. data->interleaving = atoi(value);
  143. else if (!strcmp(attr, "channels"))
  144. data->channels = atoi(value);
  145. return 0;
  146. }
  147. static int amr_parse_sdp_line(AVFormatContext *s, int st_index,
  148. PayloadContext *data, const char *line)
  149. {
  150. const char *p;
  151. int ret;
  152. if (st_index < 0)
  153. return 0;
  154. /* Parse an fmtp line this one:
  155. * a=fmtp:97 octet-align=1; interleaving=0
  156. * That is, a normal fmtp: line followed by semicolon & space
  157. * separated key/value pairs.
  158. */
  159. if (av_strstart(line, "fmtp:", &p)) {
  160. ret = ff_parse_fmtp(s->streams[st_index], data, p, amr_parse_fmtp);
  161. if (!data->octet_align || data->crc ||
  162. data->interleaving || data->channels != 1) {
  163. av_log(s, AV_LOG_ERROR, "Unsupported RTP/AMR configuration!\n");
  164. return -1;
  165. }
  166. return ret;
  167. }
  168. return 0;
  169. }
  170. RTPDynamicProtocolHandler ff_amr_nb_dynamic_handler = {
  171. .enc_name = "AMR",
  172. .codec_type = AVMEDIA_TYPE_AUDIO,
  173. .codec_id = AV_CODEC_ID_AMR_NB,
  174. .parse_sdp_a_line = amr_parse_sdp_line,
  175. .alloc = amr_new_context,
  176. .free = amr_free_context,
  177. .parse_packet = amr_handle_packet,
  178. };
  179. RTPDynamicProtocolHandler ff_amr_wb_dynamic_handler = {
  180. .enc_name = "AMR-WB",
  181. .codec_type = AVMEDIA_TYPE_AUDIO,
  182. .codec_id = AV_CODEC_ID_AMR_WB,
  183. .parse_sdp_a_line = amr_parse_sdp_line,
  184. .alloc = amr_new_context,
  185. .free = amr_free_context,
  186. .parse_packet = amr_handle_packet,
  187. };