2 * RTP parser for loss tolerant payload format for MP3 audio (RFC 5219)
3 * Copyright (c) 2015 Gilles Chanteperdrix <gch@xenomai.org>
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/attributes.h"
23 #include "libavutil/intreadwrite.h"
25 #include "rtpdec_formats.h"
27 struct PayloadContext {
32 int split_pos, split_buf_size, split_pkts;
33 AVIOContext *fragment;
36 static inline void free_fragment(PayloadContext *data)
40 avio_close_dyn_buf(data->fragment, &p);
42 data->fragment = NULL;
46 static void mpa_robust_free_context(PayloadContext *data)
49 av_free(data->split_buf);
53 static int mpa_robust_parse_rtp_header(AVFormatContext *ctx,
54 const uint8_t *buf, int len,
55 unsigned *adu_size, unsigned *cont)
60 av_log(ctx, AV_LOG_ERROR, "Invalid %d bytes packet\n", len);
61 return AVERROR_INVALIDDATA;
64 *cont = !!(buf[0] & 0x80);
65 if (!(buf[0] & 0x40)) {
67 *adu_size = buf[0] & ~0xc0;
70 *adu_size = AV_RB16(buf) & ~0xc000;
76 static int mpa_robust_parse_packet(AVFormatContext *ctx, PayloadContext *data,
77 AVStream *st, AVPacket *pkt,
78 uint32_t *timestamp, const uint8_t *buf,
79 int len, uint16_t seq, int flags)
81 unsigned adu_size, continuation;
85 buf = &data->split_buf[data->split_pos];
86 len = data->split_buf_size - data->split_pos;
88 header_size = mpa_robust_parse_rtp_header(ctx, buf, len, &adu_size,
90 if (header_size < 0) {
91 av_freep(&data->split_buf);
97 if (continuation || adu_size > len) {
98 av_freep(&data->split_buf);
99 av_log(ctx, AV_LOG_ERROR, "Invalid frame\n");
100 return AVERROR_INVALIDDATA;
103 if (av_new_packet(pkt, adu_size)) {
104 av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
105 return AVERROR(ENOMEM);
108 pkt->stream_index = st->index;
109 memcpy(pkt->data, buf, adu_size);
111 data->split_pos += adu_size;
113 if (data->split_pos == data->split_buf_size) {
114 av_freep(&data->split_buf);
122 header_size = mpa_robust_parse_rtp_header(ctx, buf, len, &adu_size,
130 if (!continuation && adu_size <= len) {
131 /* One or more complete frames */
133 if (av_new_packet(pkt, adu_size)) {
134 av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
135 return AVERROR(ENOMEM);
138 pkt->stream_index = st->index;
139 memcpy(pkt->data, buf, adu_size);
144 data->split_buf_size = len;
145 data->split_buf = av_malloc(data->split_buf_size);
147 if (!data->split_buf) {
148 av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
150 return AVERROR(ENOMEM);
152 memcpy(data->split_buf, buf, data->split_buf_size);
156 } else if (!continuation) { /* && adu_size > len */
160 data->adu_size = adu_size;
161 data->cur_size = len;
162 data->timestamp = *timestamp;
164 err = avio_open_dyn_buf(&data->fragment);
168 avio_write(data->fragment, buf, len);
169 return AVERROR(EAGAIN);
171 /* else continuation == 1 */
173 /* Fragment other than first */
174 if (!data->fragment) {
175 av_log(ctx, AV_LOG_WARNING,
176 "Received packet without a start fragment; dropping.\n");
177 return AVERROR(EAGAIN);
179 if (adu_size = data->adu_size ||
180 data->timestamp != *timestamp) {
182 av_log(ctx, AV_LOG_ERROR, "Invalid packet received\n");
183 return AVERROR_INVALIDDATA;
186 avio_write(data->fragment, buf, len);
187 data->cur_size += len;
189 if (data->cur_size < data->adu_size)
190 return AVERROR(EAGAIN);
192 err = ff_rtp_finalize_packet(pkt, &data->fragment, st->index);
194 av_log(ctx, AV_LOG_ERROR,
195 "Error occurred when getting fragment buffer.\n");
202 RTPDynamicProtocolHandler ff_mpeg_audio_robust_dynamic_handler = {
203 .enc_name = "mpa-robust",
204 .codec_type = AVMEDIA_TYPE_AUDIO,
205 .codec_id = AV_CODEC_ID_MP3ADU,
206 .need_parsing = AVSTREAM_PARSE_HEADERS,
207 .priv_data_size = sizeof(PayloadContext),
208 .free = mpa_robust_free_context,
209 .parse_packet = mpa_robust_parse_packet,