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 "avio_internal.h"
23 #include "rtpdec_formats.h"
25 #include "libavutil/avstring.h"
26 #include "libavcodec/get_bits.h"
28 struct PayloadContext {
35 static void latm_close_context(PayloadContext *data)
37 ffio_free_dyn_buf(&data->dyn_buf);
41 static int latm_parse_packet(AVFormatContext *ctx, PayloadContext *data,
42 AVStream *st, AVPacket *pkt, uint32_t *timestamp,
43 const uint8_t *buf, int len, uint16_t seq,
49 if (!data->dyn_buf || data->timestamp != *timestamp) {
51 ffio_free_dyn_buf(&data->dyn_buf);
53 data->timestamp = *timestamp;
54 if ((ret = avio_open_dyn_buf(&data->dyn_buf)) < 0)
57 avio_write(data->dyn_buf, buf, len);
59 if (!(flags & RTP_FLAG_MARKER))
60 return AVERROR(EAGAIN);
62 data->len = avio_close_dyn_buf(data->dyn_buf, &data->buf);
68 av_log(ctx, AV_LOG_ERROR, "No data available yet\n");
73 while (data->pos < data->len) {
74 uint8_t val = data->buf[data->pos++];
79 if (data->pos + cur_len > data->len) {
80 av_log(ctx, AV_LOG_ERROR, "Malformed LATM packet\n");
84 if ((ret = av_new_packet(pkt, cur_len)) < 0)
86 memcpy(pkt->data, data->buf + data->pos, cur_len);
88 pkt->stream_index = st->index;
89 return data->pos < data->len;
92 static int parse_fmtp_config(AVStream *st, const char *value)
94 int len = ff_hex_to_data(NULL, value), i, ret = 0;
97 int audio_mux_version, same_time_framing, num_programs, num_layers;
99 /* Pad this buffer, too, to avoid out of bounds reads with get_bits below */
100 config = av_mallocz(len + AV_INPUT_BUFFER_PADDING_SIZE);
102 return AVERROR(ENOMEM);
103 ff_hex_to_data(config, value);
104 init_get_bits(&gb, config, len*8);
105 audio_mux_version = get_bits(&gb, 1);
106 same_time_framing = get_bits(&gb, 1);
107 skip_bits(&gb, 6); /* num_sub_frames */
108 num_programs = get_bits(&gb, 4);
109 num_layers = get_bits(&gb, 3);
110 if (audio_mux_version != 0 || same_time_framing != 1 || num_programs != 0 ||
112 av_log(NULL, AV_LOG_WARNING, "Unsupported LATM config (%d,%d,%d,%d)\n",
113 audio_mux_version, same_time_framing,
114 num_programs, num_layers);
115 ret = AVERROR_PATCHWELCOME;
118 av_freep(&st->codec->extradata);
119 st->codec->extradata_size = (get_bits_left(&gb) + 7)/8;
120 st->codec->extradata = av_mallocz(st->codec->extradata_size +
121 AV_INPUT_BUFFER_PADDING_SIZE);
122 if (!st->codec->extradata) {
123 ret = AVERROR(ENOMEM);
126 for (i = 0; i < st->codec->extradata_size; i++)
127 st->codec->extradata[i] = get_bits(&gb, 8);
134 static int parse_fmtp(AVFormatContext *s,
135 AVStream *stream, PayloadContext *data,
136 const char *attr, const char *value)
140 if (!strcmp(attr, "config")) {
141 res = parse_fmtp_config(stream, value);
144 } else if (!strcmp(attr, "cpresent")) {
145 int cpresent = atoi(value);
147 avpriv_request_sample(s,
148 "RTP MP4A-LATM with in-band configuration");
154 static int latm_parse_sdp_line(AVFormatContext *s, int st_index,
155 PayloadContext *data, const char *line)
162 if (av_strstart(line, "fmtp:", &p))
163 return ff_parse_fmtp(s, s->streams[st_index], data, p, parse_fmtp);
168 RTPDynamicProtocolHandler ff_mp4a_latm_dynamic_handler = {
169 .enc_name = "MP4A-LATM",
170 .codec_type = AVMEDIA_TYPE_AUDIO,
171 .codec_id = AV_CODEC_ID_AAC,
172 .priv_data_size = sizeof(PayloadContext),
173 .parse_sdp_a_line = latm_parse_sdp_line,
174 .close = latm_close_context,
175 .parse_packet = latm_parse_packet,