rtpdec_amr.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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_amr.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. static int amr_handle_packet(AVFormatContext *ctx,
  31. PayloadContext *data,
  32. AVStream *st,
  33. AVPacket * pkt,
  34. uint32_t * timestamp,
  35. const uint8_t * buf,
  36. int len, int flags)
  37. {
  38. const uint8_t *frame_sizes = NULL;
  39. int frames;
  40. int i;
  41. const uint8_t *speech_data;
  42. uint8_t *ptr;
  43. if (st->codec->codec_id == CODEC_ID_AMR_NB) {
  44. frame_sizes = frame_sizes_nb;
  45. } else if (st->codec->codec_id == CODEC_ID_AMR_WB) {
  46. frame_sizes = frame_sizes_wb;
  47. } else {
  48. av_log(ctx, AV_LOG_ERROR, "Bad codec ID\n");
  49. return AVERROR_INVALIDDATA;
  50. }
  51. if (st->codec->channels != 1) {
  52. av_log(ctx, AV_LOG_ERROR, "Only mono AMR is supported\n");
  53. return AVERROR_INVALIDDATA;
  54. }
  55. /* The AMR RTP packet consists of one header byte, followed
  56. * by one TOC byte for each AMR frame in the packet, followed
  57. * by the speech data for all the AMR frames.
  58. *
  59. * The header byte contains only a codec mode request, for
  60. * requesting what kind of AMR data the sender wants to
  61. * receive. Not used at the moment.
  62. */
  63. /* Count the number of frames in the packet. The highest bit
  64. * is set in a TOC byte if there are more frames following.
  65. */
  66. for (frames = 1; frames < len && (buf[frames] & 0x80); frames++) ;
  67. if (1 + frames >= len) {
  68. /* We hit the end of the packet while counting frames. */
  69. av_log(ctx, AV_LOG_ERROR, "No speech data found\n");
  70. return AVERROR_INVALIDDATA;
  71. }
  72. speech_data = buf + 1 + frames;
  73. /* Everything except the codec mode request byte should be output. */
  74. if (av_new_packet(pkt, len - 1)) {
  75. av_log(ctx, AV_LOG_ERROR, "Out of memory\n");
  76. return AVERROR_NOMEM;
  77. }
  78. pkt->stream_index = st->index;
  79. ptr = pkt->data;
  80. for (i = 0; i < frames; i++) {
  81. uint8_t toc = buf[1 + i];
  82. int frame_size = frame_sizes[(toc >> 3) & 0x0f];
  83. if (speech_data + frame_size > buf + len) {
  84. /* Too little speech data */
  85. av_log(ctx, AV_LOG_WARNING, "Too little speech data in the RTP packet\n");
  86. /* Set the unwritten part of the packet to zero. */
  87. memset(ptr, 0, pkt->data + pkt->size - ptr);
  88. pkt->size = ptr - pkt->data;
  89. return 0;
  90. }
  91. /* Extract the AMR frame mode from the TOC byte */
  92. *ptr++ = toc & 0x7C;
  93. /* Copy the speech data */
  94. memcpy(ptr, speech_data, frame_size);
  95. speech_data += frame_size;
  96. ptr += frame_size;
  97. }
  98. if (speech_data < buf + len) {
  99. av_log(ctx, AV_LOG_WARNING, "Too much speech data in the RTP packet?\n");
  100. /* Set the unwritten part of the packet to zero. */
  101. memset(ptr, 0, pkt->data + pkt->size - ptr);
  102. pkt->size = ptr - pkt->data;
  103. }
  104. return 0;
  105. }
  106. static int amr_parse_sdp_line(AVFormatContext *s, int st_index,
  107. PayloadContext *data, const char *line)
  108. {
  109. const char *p;
  110. char attr[25], value[25];
  111. /* Parse an fmtp line this one:
  112. * a=fmtp:97 octet-align=1; interleaving=0
  113. * That is, a normal fmtp: line followed by semicolon & space
  114. * separated key/value pairs.
  115. */
  116. if (av_strstart(line, "fmtp:", &p)) {
  117. int octet_align = 0;
  118. int crc = 0;
  119. int interleaving = 0;
  120. int channels = 1;
  121. while (*p && *p == ' ') p++; /* strip spaces */
  122. while (*p && *p != ' ') p++; /* eat protocol identifier */
  123. while (*p && *p == ' ') p++; /* strip trailing spaces */
  124. while (ff_rtsp_next_attr_and_value(&p, attr, sizeof(attr), value, sizeof(value))) {
  125. if (!strcmp(attr, "octet-align"))
  126. octet_align = atoi(value);
  127. else if (!strcmp(attr, "crc"))
  128. crc = atoi(value);
  129. else if (!strcmp(attr, "interleaving"))
  130. interleaving = atoi(value);
  131. else if (!strcmp(attr, "channels"))
  132. channels = atoi(value);
  133. }
  134. if (!octet_align || crc || interleaving || channels != 1) {
  135. av_log(s, AV_LOG_ERROR, "Unsupported RTP/AMR configuration!\n");
  136. return -1;
  137. }
  138. }
  139. return 0;
  140. }
  141. RTPDynamicProtocolHandler ff_amr_nb_dynamic_handler = {
  142. .enc_name = "AMR",
  143. .codec_type = CODEC_TYPE_AUDIO,
  144. .codec_id = CODEC_ID_AMR_NB,
  145. .parse_sdp_a_line = amr_parse_sdp_line,
  146. .parse_packet = amr_handle_packet,
  147. };
  148. RTPDynamicProtocolHandler ff_amr_wb_dynamic_handler = {
  149. .enc_name = "AMR-WB",
  150. .codec_type = CODEC_TYPE_AUDIO,
  151. .codec_id = CODEC_ID_AMR_WB,
  152. .parse_sdp_a_line = amr_parse_sdp_line,
  153. .parse_packet = amr_handle_packet,
  154. };