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.

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