OSDN Git Service

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