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.

204 lines
6.5KB

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