rtpdec: Move AAC depacketization code in rtpdec to a proper payload handler
[libav.git] / libavformat / rtpdec_mpeg4.c
1 /**
2 * Common code for the RTP depacketization of MPEG-4 formats.
3 * Copyright (c) 2010 Fabrice Bellard
4 * Romain Degez
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
25 * @brief MPEG4 / RTP Code
26 * @author Fabrice Bellard
27 * @author Romain Degez
28 */
29
30 #include "rtpdec_mpeg4.h"
31 #include "internal.h"
32 #include "libavutil/avstring.h"
33 #include "libavcodec/get_bits.h"
34 #include <strings.h>
35
36 #include "rtsp.h" //XXX remove this dependency
37
38 /* return the length and optionally the data */
39 static int hex_to_data(uint8_t *data, const char *p)
40 {
41 int c, len, v;
42
43 len = 0;
44 v = 1;
45 for (;;) {
46 p += strspn(p, SPACE_CHARS);
47 if (*p == '\0')
48 break;
49 c = toupper((unsigned char) *p++);
50 if (c >= '0' && c <= '9')
51 c = c - '0';
52 else if (c >= 'A' && c <= 'F')
53 c = c - 'A' + 10;
54 else
55 break;
56 v = (v << 4) | c;
57 if (v & 0x100) {
58 if (data)
59 data[len] = v;
60 len++;
61 v = 1;
62 }
63 }
64 return len;
65 }
66
67 typedef struct {
68 const char *str;
69 uint16_t type;
70 uint32_t offset;
71 } AttrNameMap;
72
73 /* All known fmtp parameters and the corresponding RTPAttrTypeEnum */
74 #define ATTR_NAME_TYPE_INT 0
75 #define ATTR_NAME_TYPE_STR 1
76 static const AttrNameMap attr_names[]=
77 {
78 { "SizeLength", ATTR_NAME_TYPE_INT,
79 offsetof(RTPPayloadData, sizelength) },
80 { "IndexLength", ATTR_NAME_TYPE_INT,
81 offsetof(RTPPayloadData, indexlength) },
82 { "IndexDeltaLength", ATTR_NAME_TYPE_INT,
83 offsetof(RTPPayloadData, indexdeltalength) },
84 { "profile-level-id", ATTR_NAME_TYPE_INT,
85 offsetof(RTPPayloadData, profile_level_id) },
86 { "StreamType", ATTR_NAME_TYPE_INT,
87 offsetof(RTPPayloadData, streamtype) },
88 { "mode", ATTR_NAME_TYPE_STR,
89 offsetof(RTPPayloadData, mode) },
90 { NULL, -1, -1 },
91 };
92
93 static int parse_fmtp_config(AVCodecContext * codec, char *value)
94 {
95 /* decode the hexa encoded parameter */
96 int len = hex_to_data(NULL, value);
97 if (codec->extradata)
98 av_free(codec->extradata);
99 codec->extradata = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
100 if (!codec->extradata)
101 return AVERROR(ENOMEM);
102 codec->extradata_size = len;
103 hex_to_data(codec->extradata, value);
104 return 0;
105 }
106
107 static int rtp_parse_mp4_au(RTSPStream *rtsp_st, const uint8_t *buf)
108 {
109 int au_headers_length, au_header_size, i;
110 GetBitContext getbitcontext;
111 RTPPayloadData *infos;
112
113 infos = &rtsp_st->rtp_payload_data;
114 if (infos == NULL)
115 return -1;
116
117 /* decode the first 2 bytes where the AUHeader sections are stored
118 length in bits */
119 au_headers_length = AV_RB16(buf);
120
121 if (au_headers_length > RTP_MAX_PACKET_LENGTH)
122 return -1;
123
124 infos->au_headers_length_bytes = (au_headers_length + 7) / 8;
125
126 /* skip AU headers length section (2 bytes) */
127 buf += 2;
128
129 init_get_bits(&getbitcontext, buf, infos->au_headers_length_bytes * 8);
130
131 /* XXX: Wrong if optionnal additional sections are present (cts, dts etc...) */
132 au_header_size = infos->sizelength + infos->indexlength;
133 if (au_header_size <= 0 || (au_headers_length % au_header_size != 0))
134 return -1;
135
136 infos->nb_au_headers = au_headers_length / au_header_size;
137 if (!infos->au_headers || infos->au_headers_allocated < infos->nb_au_headers) {
138 av_free(infos->au_headers);
139 infos->au_headers = av_malloc(sizeof(struct AUHeaders) * infos->nb_au_headers);
140 infos->au_headers_allocated = infos->nb_au_headers;
141 }
142
143 /* XXX: We handle multiple AU Section as only one (need to fix this for interleaving)
144 In my test, the FAAD decoder does not behave correctly when sending each AU one by one
145 but does when sending the whole as one big packet... */
146 infos->au_headers[0].size = 0;
147 infos->au_headers[0].index = 0;
148 for (i = 0; i < infos->nb_au_headers; ++i) {
149 infos->au_headers[0].size += get_bits_long(&getbitcontext, infos->sizelength);
150 infos->au_headers[0].index = get_bits_long(&getbitcontext, infos->indexlength);
151 }
152
153 infos->nb_au_headers = 1;
154
155 return 0;
156 }
157
158
159 /* Follows RFC 3640 */
160 static int aac_parse_packet(AVFormatContext *ctx,
161 PayloadContext *data,
162 AVStream *st,
163 AVPacket *pkt,
164 uint32_t *timestamp,
165 const uint8_t *buf, int len, int flags)
166 {
167 RTSPStream *rtsp_st = st->priv_data;
168 RTPPayloadData *infos;
169
170 if (rtp_parse_mp4_au(rtsp_st, buf))
171 return -1;
172
173 infos = &rtsp_st->rtp_payload_data;
174 if (infos == NULL)
175 return -1;
176 buf += infos->au_headers_length_bytes + 2;
177 len -= infos->au_headers_length_bytes + 2;
178
179 /* XXX: Fixme we only handle the case where rtp_parse_mp4_au define
180 one au_header */
181 av_new_packet(pkt, infos->au_headers[0].size);
182 memcpy(pkt->data, buf, infos->au_headers[0].size);
183
184 pkt->stream_index = st->index;
185 return 0;
186 }
187
188 static int parse_sdp_line(AVFormatContext *s, int st_index,
189 PayloadContext *data, const char *line)
190 {
191 const char *p;
192 char value[4096], attr[25];
193 int res = 0, i;
194 AVStream *st = s->streams[st_index];
195 RTSPStream *rtsp_st = st->priv_data;
196 AVCodecContext* codec = st->codec;
197 RTPPayloadData *rtp_payload_data = &rtsp_st->rtp_payload_data;
198
199 if (av_strstart(line, "fmtp:", &p)) {
200 // remove protocol identifier
201 while (*p && *p == ' ') p++; // strip spaces
202 while (*p && *p != ' ') p++; // eat protocol identifier
203 while (*p && *p == ' ') p++; // strip trailing spaces
204
205 while (ff_rtsp_next_attr_and_value(&p,
206 attr, sizeof(attr),
207 value, sizeof(value))) {
208 if (!strcmp(attr, "config")) {
209 res = parse_fmtp_config(codec, value);
210
211 if (res < 0)
212 return res;
213 }
214
215 if (codec->codec_id == CODEC_ID_AAC) {
216 /* Looking for a known attribute */
217 for (i = 0; attr_names[i].str; ++i) {
218 if (!strcasecmp(attr, attr_names[i].str)) {
219 if (attr_names[i].type == ATTR_NAME_TYPE_INT) {
220 *(int *)((char *)rtp_payload_data +
221 attr_names[i].offset) = atoi(value);
222 } else if (attr_names[i].type == ATTR_NAME_TYPE_STR)
223 *(char **)((char *)rtp_payload_data +
224 attr_names[i].offset) = av_strdup(value);
225 }
226 }
227 }
228 }
229 }
230
231 return 0;
232
233 }
234
235 RTPDynamicProtocolHandler ff_mp4v_es_dynamic_handler = {
236 .enc_name = "MP4V-ES",
237 .codec_type = AVMEDIA_TYPE_VIDEO,
238 .codec_id = CODEC_ID_MPEG4,
239 .parse_sdp_a_line = parse_sdp_line,
240 .open = NULL,
241 .close = NULL,
242 .parse_packet = NULL
243 };
244
245 RTPDynamicProtocolHandler ff_mpeg4_generic_dynamic_handler = {
246 .enc_name = "mpeg4-generic",
247 .codec_type = AVMEDIA_TYPE_AUDIO,
248 .codec_id = CODEC_ID_AAC,
249 .parse_sdp_a_line = parse_sdp_line,
250 .open = NULL,
251 .close = NULL,
252 .parse_packet = aac_parse_packet
253 };