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.

210 lines
6.6KB

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