rtpdec_amr.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 "avformat.h"
  22. #include "rtpdec_formats.h"
  23. #include "libavutil/avstring.h"
  24. static const uint8_t frame_sizes_nb[16] = {
  25. 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0
  26. };
  27. static const uint8_t frame_sizes_wb[16] = {
  28. 17, 23, 32, 36, 40, 46, 50, 58, 60, 5, 5, 0, 0, 0, 0, 0
  29. };
  30. struct PayloadContext {
  31. int octet_align;
  32. int crc;
  33. int interleaving;
  34. int channels;
  35. };
  36. static PayloadContext *amr_new_context(void)
  37. {
  38. PayloadContext *data = av_mallocz(sizeof(PayloadContext));
  39. if(!data) return data;
  40. data->channels = 1;
  41. return data;
  42. }
  43. static void amr_free_context(PayloadContext *data)
  44. {
  45. av_free(data);
  46. }
  47. static int amr_handle_packet(AVFormatContext *ctx,
  48. PayloadContext *data,
  49. AVStream *st,
  50. AVPacket * pkt,
  51. uint32_t * timestamp,
  52. const uint8_t * buf,
  53. int len, int flags)
  54. {
  55. const uint8_t *frame_sizes = NULL;
  56. int frames;
  57. int i;
  58. const uint8_t *speech_data;
  59. uint8_t *ptr;
  60. if (st->codec->codec_id == AV_CODEC_ID_AMR_NB) {
  61. frame_sizes = frame_sizes_nb;
  62. } else if (st->codec->codec_id == AV_CODEC_ID_AMR_WB) {
  63. frame_sizes = frame_sizes_wb;
  64. } else {
  65. av_log(ctx, AV_LOG_ERROR, "Bad codec ID\n");
  66. return AVERROR_INVALIDDATA;
  67. }
  68. if (st->codec->channels != 1) {
  69. av_log(ctx, AV_LOG_ERROR, "Only mono AMR is supported\n");
  70. return AVERROR_INVALIDDATA;
  71. }
  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(AVStream *stream, PayloadContext *data,
  124. char *attr, char *value)
  125. {
  126. /* Some AMR SDP configurations contain "octet-align", without
  127. * the trailing =1. Therefore, if the value is empty,
  128. * interpret it as "1".
  129. */
  130. if (!strcmp(value, "")) {
  131. av_log(NULL, AV_LOG_WARNING, "AMR fmtp attribute %s had "
  132. "nonstandard empty value\n", attr);
  133. strcpy(value, "1");
  134. }
  135. if (!strcmp(attr, "octet-align"))
  136. data->octet_align = atoi(value);
  137. else if (!strcmp(attr, "crc"))
  138. data->crc = atoi(value);
  139. else if (!strcmp(attr, "interleaving"))
  140. data->interleaving = atoi(value);
  141. else if (!strcmp(attr, "channels"))
  142. data->channels = atoi(value);
  143. return 0;
  144. }
  145. static int amr_parse_sdp_line(AVFormatContext *s, int st_index,
  146. PayloadContext *data, const char *line)
  147. {
  148. const char *p;
  149. int ret;
  150. if (st_index < 0)
  151. return 0;
  152. /* Parse an fmtp line this one:
  153. * a=fmtp:97 octet-align=1; interleaving=0
  154. * That is, a normal fmtp: line followed by semicolon & space
  155. * separated key/value pairs.
  156. */
  157. if (av_strstart(line, "fmtp:", &p)) {
  158. ret = ff_parse_fmtp(s->streams[st_index], data, p, amr_parse_fmtp);
  159. if (!data->octet_align || data->crc ||
  160. data->interleaving || data->channels != 1) {
  161. av_log(s, AV_LOG_ERROR, "Unsupported RTP/AMR configuration!\n");
  162. return -1;
  163. }
  164. return ret;
  165. }
  166. return 0;
  167. }
  168. RTPDynamicProtocolHandler ff_amr_nb_dynamic_handler = {
  169. .enc_name = "AMR",
  170. .codec_type = AVMEDIA_TYPE_AUDIO,
  171. .codec_id = AV_CODEC_ID_AMR_NB,
  172. .parse_sdp_a_line = amr_parse_sdp_line,
  173. .alloc = amr_new_context,
  174. .free = amr_free_context,
  175. .parse_packet = amr_handle_packet,
  176. };
  177. RTPDynamicProtocolHandler ff_amr_wb_dynamic_handler = {
  178. .enc_name = "AMR-WB",
  179. .codec_type = AVMEDIA_TYPE_AUDIO,
  180. .codec_id = AV_CODEC_ID_AMR_WB,
  181. .parse_sdp_a_line = amr_parse_sdp_line,
  182. .alloc = amr_new_context,
  183. .free = amr_free_context,
  184. .parse_packet = amr_handle_packet,
  185. };