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