• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavformat/rtpdec_amr.c

Go to the documentation of this file.
00001 /*
00002  * RTP AMR Depacketizer, RFC 3267
00003  * Copyright (c) 2010 Martin Storsjo
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 #include "avformat.h"
00023 #include "rtpdec_formats.h"
00024 #include "libavutil/avstring.h"
00025 
00026 static const uint8_t frame_sizes_nb[16] = {
00027     12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0
00028 };
00029 static const uint8_t frame_sizes_wb[16] = {
00030     17, 23, 32, 36, 40, 46, 50, 58, 60, 5, 5, 0, 0, 0, 0, 0
00031 };
00032 
00033 struct PayloadContext {
00034     int octet_align;
00035     int crc;
00036     int interleaving;
00037     int channels;
00038 };
00039 
00040 static PayloadContext *amr_new_context(void)
00041 {
00042     PayloadContext *data = av_mallocz(sizeof(PayloadContext));
00043     if(!data) return data;
00044     data->channels = 1;
00045     return data;
00046 }
00047 
00048 static void amr_free_context(PayloadContext *data)
00049 {
00050     av_free(data);
00051 }
00052 
00053 static int amr_handle_packet(AVFormatContext *ctx,
00054                              PayloadContext *data,
00055                              AVStream *st,
00056                              AVPacket * pkt,
00057                              uint32_t * timestamp,
00058                              const uint8_t * buf,
00059                              int len, int flags)
00060 {
00061     const uint8_t *frame_sizes = NULL;
00062     int frames;
00063     int i;
00064     const uint8_t *speech_data;
00065     uint8_t *ptr;
00066 
00067     if (st->codec->codec_id == CODEC_ID_AMR_NB) {
00068         frame_sizes = frame_sizes_nb;
00069     } else if (st->codec->codec_id == CODEC_ID_AMR_WB) {
00070         frame_sizes = frame_sizes_wb;
00071     } else {
00072         av_log(ctx, AV_LOG_ERROR, "Bad codec ID\n");
00073         return AVERROR_INVALIDDATA;
00074     }
00075 
00076     if (st->codec->channels != 1) {
00077         av_log(ctx, AV_LOG_ERROR, "Only mono AMR is supported\n");
00078         return AVERROR_INVALIDDATA;
00079     }
00080 
00081     /* The AMR RTP packet consists of one header byte, followed
00082      * by one TOC byte for each AMR frame in the packet, followed
00083      * by the speech data for all the AMR frames.
00084      *
00085      * The header byte contains only a codec mode request, for
00086      * requesting what kind of AMR data the sender wants to
00087      * receive. Not used at the moment.
00088      */
00089 
00090     /* Count the number of frames in the packet. The highest bit
00091      * is set in a TOC byte if there are more frames following.
00092      */
00093     for (frames = 1; frames < len && (buf[frames] & 0x80); frames++) ;
00094 
00095     if (1 + frames >= len) {
00096         /* We hit the end of the packet while counting frames. */
00097         av_log(ctx, AV_LOG_ERROR, "No speech data found\n");
00098         return AVERROR_INVALIDDATA;
00099     }
00100 
00101     speech_data = buf + 1 + frames;
00102 
00103     /* Everything except the codec mode request byte should be output. */
00104     if (av_new_packet(pkt, len - 1)) {
00105         av_log(ctx, AV_LOG_ERROR, "Out of memory\n");
00106         return AVERROR(ENOMEM);
00107     }
00108     pkt->stream_index = st->index;
00109     ptr = pkt->data;
00110 
00111     for (i = 0; i < frames; i++) {
00112         uint8_t toc = buf[1 + i];
00113         int frame_size = frame_sizes[(toc >> 3) & 0x0f];
00114 
00115         if (speech_data + frame_size > buf + len) {
00116             /* Too little speech data */
00117             av_log(ctx, AV_LOG_WARNING, "Too little speech data in the RTP packet\n");
00118             /* Set the unwritten part of the packet to zero. */
00119             memset(ptr, 0, pkt->data + pkt->size - ptr);
00120             pkt->size = ptr - pkt->data;
00121             return 0;
00122         }
00123 
00124         /* Extract the AMR frame mode from the TOC byte */
00125         *ptr++ = toc & 0x7C;
00126 
00127         /* Copy the speech data */
00128         memcpy(ptr, speech_data, frame_size);
00129         speech_data += frame_size;
00130         ptr += frame_size;
00131     }
00132 
00133     if (speech_data < buf + len) {
00134         av_log(ctx, AV_LOG_WARNING, "Too much speech data in the RTP packet?\n");
00135         /* Set the unwritten part of the packet to zero. */
00136         memset(ptr, 0, pkt->data + pkt->size - ptr);
00137         pkt->size = ptr - pkt->data;
00138     }
00139 
00140     return 0;
00141 }
00142 
00143 static int amr_parse_fmtp(AVStream *stream, PayloadContext *data,
00144                           char *attr, char *value)
00145 {
00146     /* Some AMR SDP configurations contain "octet-align", without
00147      * the trailing =1. Therefore, if the value is empty,
00148      * interpret it as "1".
00149      */
00150     if (!strcmp(value, "")) {
00151         av_log(NULL, AV_LOG_WARNING, "AMR fmtp attribute %s had "
00152                                      "nonstandard empty value\n", attr);
00153         strcpy(value, "1");
00154     }
00155     if (!strcmp(attr, "octet-align"))
00156         data->octet_align = atoi(value);
00157     else if (!strcmp(attr, "crc"))
00158         data->crc = atoi(value);
00159     else if (!strcmp(attr, "interleaving"))
00160         data->interleaving = atoi(value);
00161     else if (!strcmp(attr, "channels"))
00162         data->channels = atoi(value);
00163     return 0;
00164 }
00165 
00166 static int amr_parse_sdp_line(AVFormatContext *s, int st_index,
00167                               PayloadContext *data, const char *line)
00168 {
00169     const char *p;
00170     int ret;
00171 
00172     /* Parse an fmtp line this one:
00173      * a=fmtp:97 octet-align=1; interleaving=0
00174      * That is, a normal fmtp: line followed by semicolon & space
00175      * separated key/value pairs.
00176      */
00177     if (av_strstart(line, "fmtp:", &p)) {
00178         ret = ff_parse_fmtp(s->streams[st_index], data, p, amr_parse_fmtp);
00179         if (!data->octet_align || data->crc ||
00180             data->interleaving || data->channels != 1) {
00181             av_log(s, AV_LOG_ERROR, "Unsupported RTP/AMR configuration!\n");
00182             return -1;
00183         }
00184         return ret;
00185     }
00186     return 0;
00187 }
00188 
00189 RTPDynamicProtocolHandler ff_amr_nb_dynamic_handler = {
00190     .enc_name         = "AMR",
00191     .codec_type       = AVMEDIA_TYPE_AUDIO,
00192     .codec_id         = CODEC_ID_AMR_NB,
00193     .parse_sdp_a_line = amr_parse_sdp_line,
00194     .alloc            = amr_new_context,
00195     .free             = amr_free_context,
00196     .parse_packet     = amr_handle_packet,
00197 };
00198 
00199 RTPDynamicProtocolHandler ff_amr_wb_dynamic_handler = {
00200     .enc_name         = "AMR-WB",
00201     .codec_type       = AVMEDIA_TYPE_AUDIO,
00202     .codec_id         = CODEC_ID_AMR_WB,
00203     .parse_sdp_a_line = amr_parse_sdp_line,
00204     .alloc            = amr_new_context,
00205     .free             = amr_free_context,
00206     .parse_packet     = amr_handle_packet,
00207 };
00208 
Generated on Sat Mar 17 2012 12:57:55 for Libav by doxygen 1.7.1