2 * Common code for the RTP depacketization of MPEG-4 formats.
3 * Copyright (c) 2010 Fabrice Bellard
6 * This file is part of Libav.
8 * Libav 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.
13 * Libav 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.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * @brief MPEG-4 / RTP Code
26 * @author Fabrice Bellard
27 * @author Romain Degez
30 #include "libavutil/attributes.h"
31 #include "libavutil/avstring.h"
33 #include "libavcodec/bitstream.h"
35 #include "rtpdec_formats.h"
38 #define MAX_AAC_HBR_FRAME_SIZE 8191
40 /** Structure listing useful vars to parse RTP packet payload */
41 struct PayloadContext {
50 /** mpeg 4 AU headers */
61 int au_headers_allocated;
63 int au_headers_length_bytes;
66 uint8_t buf[FFMAX(RTP_MAX_PACKET_LENGTH, MAX_AAC_HBR_FRAME_SIZE)];
67 int buf_pos, buf_size;
71 typedef struct AttrNameMap {
77 /* All known fmtp parameters and the corresponding RTPAttrTypeEnum */
78 #define ATTR_NAME_TYPE_INT 0
79 #define ATTR_NAME_TYPE_STR 1
80 static const AttrNameMap attr_names[] = {
81 { "SizeLength", ATTR_NAME_TYPE_INT,
82 offsetof(PayloadContext, sizelength) },
83 { "IndexLength", ATTR_NAME_TYPE_INT,
84 offsetof(PayloadContext, indexlength) },
85 { "IndexDeltaLength", ATTR_NAME_TYPE_INT,
86 offsetof(PayloadContext, indexdeltalength) },
87 { "profile-level-id", ATTR_NAME_TYPE_INT,
88 offsetof(PayloadContext, profile_level_id) },
89 { "StreamType", ATTR_NAME_TYPE_INT,
90 offsetof(PayloadContext, streamtype) },
91 { "mode", ATTR_NAME_TYPE_STR,
92 offsetof(PayloadContext, mode) },
96 static void close_context(PayloadContext *data)
98 av_free(data->au_headers);
102 static int parse_fmtp_config(AVCodecParameters *par, const char *value)
104 /* decode the hexa encoded parameter */
105 int len = ff_hex_to_data(NULL, value);
106 av_free(par->extradata);
107 par->extradata = av_mallocz(len + AV_INPUT_BUFFER_PADDING_SIZE);
109 return AVERROR(ENOMEM);
110 par->extradata_size = len;
111 ff_hex_to_data(par->extradata, value);
115 static int rtp_parse_mp4_au(PayloadContext *data, const uint8_t *buf, int len)
117 int au_headers_length, au_header_size, i;
118 BitstreamContext bctx;
121 return AVERROR_INVALIDDATA;
123 /* decode the first 2 bytes where the AUHeader sections are stored
125 au_headers_length = AV_RB16(buf);
127 if (au_headers_length > RTP_MAX_PACKET_LENGTH)
130 data->au_headers_length_bytes = (au_headers_length + 7) / 8;
132 /* skip AU headers length section (2 bytes) */
136 if (len < data->au_headers_length_bytes)
137 return AVERROR_INVALIDDATA;
139 bitstream_init8(&bctx, buf, data->au_headers_length_bytes);
141 /* XXX: Wrong if optional additional sections are present (cts, dts etc...) */
142 au_header_size = data->sizelength + data->indexlength;
143 if (au_header_size <= 0 || (au_headers_length % au_header_size != 0))
146 data->nb_au_headers = au_headers_length / au_header_size;
147 if (!data->au_headers || data->au_headers_allocated < data->nb_au_headers) {
148 av_free(data->au_headers);
149 data->au_headers = av_malloc(sizeof(struct AUHeaders) * data->nb_au_headers);
150 if (!data->au_headers)
151 return AVERROR(ENOMEM);
152 data->au_headers_allocated = data->nb_au_headers;
155 for (i = 0; i < data->nb_au_headers; ++i) {
156 data->au_headers[i].size = bitstream_read(&bctx, data->sizelength);
157 data->au_headers[i].index = bitstream_read(&bctx, data->indexlength);
164 /* Follows RFC 3640 */
165 static int aac_parse_packet(AVFormatContext *ctx, PayloadContext *data,
166 AVStream *st, AVPacket *pkt, uint32_t *timestamp,
167 const uint8_t *buf, int len, uint16_t seq,
173 if (data->cur_au_index > data->nb_au_headers) {
174 av_log(ctx, AV_LOG_ERROR, "Invalid parser state\n");
175 return AVERROR_INVALIDDATA;
177 if (data->buf_size - data->buf_pos < data->au_headers[data->cur_au_index].size) {
178 av_log(ctx, AV_LOG_ERROR, "Invalid AU size\n");
179 return AVERROR_INVALIDDATA;
181 if ((ret = av_new_packet(pkt, data->au_headers[data->cur_au_index].size)) < 0) {
182 av_log(ctx, AV_LOG_ERROR, "Out of memory\n");
185 memcpy(pkt->data, &data->buf[data->buf_pos], data->au_headers[data->cur_au_index].size);
186 data->buf_pos += data->au_headers[data->cur_au_index].size;
187 pkt->stream_index = st->index;
188 data->cur_au_index++;
190 if (data->cur_au_index == data->nb_au_headers) {
198 if (rtp_parse_mp4_au(data, buf, len)) {
199 av_log(ctx, AV_LOG_ERROR, "Error parsing AU headers\n");
203 buf += data->au_headers_length_bytes + 2;
204 len -= data->au_headers_length_bytes + 2;
205 if (data->nb_au_headers == 1 && len < data->au_headers[0].size) {
206 /* Packet is fragmented */
208 if (!data->buf_pos) {
209 if (data->au_headers[0].size > MAX_AAC_HBR_FRAME_SIZE) {
210 av_log(ctx, AV_LOG_ERROR, "Invalid AU size\n");
211 return AVERROR_INVALIDDATA;
214 data->buf_size = data->au_headers[0].size;
215 data->timestamp = *timestamp;
218 if (data->timestamp != *timestamp ||
219 data->au_headers[0].size != data->buf_size ||
220 data->buf_pos + len > MAX_AAC_HBR_FRAME_SIZE) {
223 av_log(ctx, AV_LOG_ERROR, "Invalid packet received\n");
224 return AVERROR_INVALIDDATA;
227 memcpy(&data->buf[data->buf_pos], buf, len);
228 data->buf_pos += len;
230 if (!(flags & RTP_FLAG_MARKER))
231 return AVERROR(EAGAIN);
233 if (data->buf_pos != data->buf_size) {
235 av_log(ctx, AV_LOG_ERROR, "Missed some packets, discarding frame\n");
236 return AVERROR_INVALIDDATA;
240 ret = av_new_packet(pkt, data->buf_size);
242 av_log(ctx, AV_LOG_ERROR, "Out of memory\n");
245 pkt->stream_index = st->index;
247 memcpy(pkt->data, data->buf, data->buf_size);
252 if (len < data->au_headers[0].size) {
253 av_log(ctx, AV_LOG_ERROR, "First AU larger than packet size\n");
254 return AVERROR_INVALIDDATA;
256 if ((ret = av_new_packet(pkt, data->au_headers[0].size)) < 0) {
257 av_log(ctx, AV_LOG_ERROR, "Out of memory\n");
260 memcpy(pkt->data, buf, data->au_headers[0].size);
261 len -= data->au_headers[0].size;
262 buf += data->au_headers[0].size;
263 pkt->stream_index = st->index;
265 if (len > 0 && data->nb_au_headers > 1) {
266 data->buf_size = FFMIN(len, sizeof(data->buf));
267 memcpy(data->buf, buf, data->buf_size);
268 data->cur_au_index = 1;
276 static int parse_fmtp(AVFormatContext *s,
277 AVStream *stream, PayloadContext *data,
278 const char *attr, const char *value)
280 AVCodecParameters *par = stream->codecpar;
283 if (!strcmp(attr, "config")) {
284 res = parse_fmtp_config(par, value);
290 if (par->codec_id == AV_CODEC_ID_AAC) {
291 /* Looking for a known attribute */
292 for (i = 0; attr_names[i].str; ++i) {
293 if (!av_strcasecmp(attr, attr_names[i].str)) {
294 if (attr_names[i].type == ATTR_NAME_TYPE_INT) {
295 int val = atoi(value);
297 av_log(s, AV_LOG_ERROR,
298 "The %s field size is invalid (%d).",
300 return AVERROR_INVALIDDATA;
302 *(int *)((char *)data+
303 attr_names[i].offset) = val;
304 } else if (attr_names[i].type == ATTR_NAME_TYPE_STR) {
305 char *val = av_strdup(value);
307 return AVERROR(ENOMEM);
308 *(char **)((char *)data+
309 attr_names[i].offset) = val;
317 static int parse_sdp_line(AVFormatContext *s, int st_index,
318 PayloadContext *data, const char *line)
325 if (av_strstart(line, "fmtp:", &p))
326 return ff_parse_fmtp(s, s->streams[st_index], data, p, parse_fmtp);
331 RTPDynamicProtocolHandler ff_mp4v_es_dynamic_handler = {
332 .enc_name = "MP4V-ES",
333 .codec_type = AVMEDIA_TYPE_VIDEO,
334 .codec_id = AV_CODEC_ID_MPEG4,
335 .need_parsing = AVSTREAM_PARSE_FULL,
336 .priv_data_size = sizeof(PayloadContext),
337 .parse_sdp_a_line = parse_sdp_line,
340 RTPDynamicProtocolHandler ff_mpeg4_generic_dynamic_handler = {
341 .enc_name = "mpeg4-generic",
342 .codec_type = AVMEDIA_TYPE_AUDIO,
343 .codec_id = AV_CODEC_ID_AAC,
344 .priv_data_size = sizeof(PayloadContext),
345 .parse_sdp_a_line = parse_sdp_line,
346 .close = close_context,
347 .parse_packet = aac_parse_packet,