2 * RTP Depacketization of MP4A-LATM, RFC 3016
3 * Copyright (c) 2010 Martin Storsjo
5 * This file is part of Libav.
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.
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.
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
22 #include "libavutil/avstring.h"
24 #include "libavcodec/bitstream.h"
26 #include "avio_internal.h"
27 #include "rtpdec_formats.h"
30 struct PayloadContext {
37 static void latm_close_context(PayloadContext *data)
39 ffio_free_dyn_buf(&data->dyn_buf);
43 static int latm_parse_packet(AVFormatContext *ctx, PayloadContext *data,
44 AVStream *st, AVPacket *pkt, uint32_t *timestamp,
45 const uint8_t *buf, int len, uint16_t seq,
51 if (!data->dyn_buf || data->timestamp != *timestamp) {
53 ffio_free_dyn_buf(&data->dyn_buf);
55 data->timestamp = *timestamp;
56 if ((ret = avio_open_dyn_buf(&data->dyn_buf)) < 0)
59 avio_write(data->dyn_buf, buf, len);
61 if (!(flags & RTP_FLAG_MARKER))
62 return AVERROR(EAGAIN);
64 data->len = avio_close_dyn_buf(data->dyn_buf, &data->buf);
70 av_log(ctx, AV_LOG_ERROR, "No data available yet\n");
75 while (data->pos < data->len) {
76 uint8_t val = data->buf[data->pos++];
81 if (data->pos + cur_len > data->len) {
82 av_log(ctx, AV_LOG_ERROR, "Malformed LATM packet\n");
86 if ((ret = av_new_packet(pkt, cur_len)) < 0)
88 memcpy(pkt->data, data->buf + data->pos, cur_len);
90 pkt->stream_index = st->index;
91 return data->pos < data->len;
94 static int parse_fmtp_config(AVStream *st, const char *value)
96 int len = ff_hex_to_data(NULL, value), i, ret = 0;
99 int audio_mux_version, same_time_framing, num_programs, num_layers;
101 /* Pad this buffer, too, to avoid out of bounds reads with get_bits below */
102 config = av_mallocz(len + AV_INPUT_BUFFER_PADDING_SIZE);
104 return AVERROR(ENOMEM);
105 ff_hex_to_data(config, value);
106 bitstream_init8(&bc, config, len);
107 audio_mux_version = bitstream_read(&bc, 1);
108 same_time_framing = bitstream_read(&bc, 1);
109 bitstream_skip(&bc, 6); /* num_sub_frames */
110 num_programs = bitstream_read(&bc, 4);
111 num_layers = bitstream_read(&bc, 3);
112 if (audio_mux_version != 0 || same_time_framing != 1 || num_programs != 0 ||
114 avpriv_report_missing_feature(NULL, "LATM config (%d,%d,%d,%d)",
115 audio_mux_version, same_time_framing,
116 num_programs, num_layers);
117 ret = AVERROR_PATCHWELCOME;
120 av_freep(&st->codecpar->extradata);
121 st->codecpar->extradata_size = (bitstream_bits_left(&bc) + 7) / 8;
122 st->codecpar->extradata = av_mallocz(st->codecpar->extradata_size +
123 AV_INPUT_BUFFER_PADDING_SIZE);
124 if (!st->codecpar->extradata) {
125 ret = AVERROR(ENOMEM);
128 for (i = 0; i < st->codecpar->extradata_size; i++)
129 st->codecpar->extradata[i] = bitstream_read(&bc, 8);
136 static int parse_fmtp(AVFormatContext *s,
137 AVStream *stream, PayloadContext *data,
138 const char *attr, const char *value)
142 if (!strcmp(attr, "config")) {
143 res = parse_fmtp_config(stream, value);
146 } else if (!strcmp(attr, "cpresent")) {
147 int cpresent = atoi(value);
149 avpriv_request_sample(s,
150 "RTP MP4A-LATM with in-band configuration");
156 static int latm_parse_sdp_line(AVFormatContext *s, int st_index,
157 PayloadContext *data, const char *line)
164 if (av_strstart(line, "fmtp:", &p))
165 return ff_parse_fmtp(s, s->streams[st_index], data, p, parse_fmtp);
170 RTPDynamicProtocolHandler ff_mp4a_latm_dynamic_handler = {
171 .enc_name = "MP4A-LATM",
172 .codec_type = AVMEDIA_TYPE_AUDIO,
173 .codec_id = AV_CODEC_ID_AAC,
174 .priv_data_size = sizeof(PayloadContext),
175 .parse_sdp_a_line = latm_parse_sdp_line,
176 .close = latm_close_context,
177 .parse_packet = latm_parse_packet,