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