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 FFmpeg.
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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 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);
52 static int mpa_robust_parse_rtp_header(AVFormatContext *ctx,
53 const uint8_t *buf, int len,
54 unsigned *adu_size, unsigned *cont)
59 av_log(ctx, AV_LOG_ERROR, "Invalid %d bytes packet\n", len);
60 return AVERROR_INVALIDDATA;
63 *cont = !!(buf[0] & 0x80);
64 if (!(buf[0] & 0x40)) {
66 *adu_size = buf[0] & ~0xc0;
69 *adu_size = AV_RB16(buf) & ~0xc000;
75 static int mpa_robust_parse_packet(AVFormatContext *ctx, PayloadContext *data,
76 AVStream *st, AVPacket *pkt,
77 uint32_t *timestamp, const uint8_t *buf,
78 int len, uint16_t seq, int flags)
80 unsigned adu_size, continuation;
84 buf = &data->split_buf[data->split_pos];
85 len = data->split_buf_size - data->split_pos;
87 header_size = mpa_robust_parse_rtp_header(ctx, buf, len, &adu_size,
89 if (header_size < 0) {
90 av_freep(&data->split_buf);
96 if (continuation || adu_size > len) {
97 av_freep(&data->split_buf);
98 av_log(ctx, AV_LOG_ERROR, "Invalid frame\n");
99 return AVERROR_INVALIDDATA;
102 if (av_new_packet(pkt, adu_size)) {
103 av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
104 return AVERROR(ENOMEM);
107 pkt->stream_index = st->index;
108 memcpy(pkt->data, buf, adu_size);
110 data->split_pos += adu_size;
112 if (data->split_pos == data->split_buf_size) {
113 av_freep(&data->split_buf);
121 header_size = mpa_robust_parse_rtp_header(ctx, buf, len, &adu_size,
129 if (!continuation && adu_size <= len) {
130 /* One or more complete frames */
132 if (av_new_packet(pkt, adu_size)) {
133 av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
134 return AVERROR(ENOMEM);
137 pkt->stream_index = st->index;
138 memcpy(pkt->data, buf, adu_size);
143 data->split_buf_size = len;
144 data->split_buf = av_malloc(data->split_buf_size);
146 if (!data->split_buf) {
147 av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
149 return AVERROR(ENOMEM);
151 memcpy(data->split_buf, buf, data->split_buf_size);
155 } else if (!continuation) { /* && adu_size > len */
159 data->adu_size = adu_size;
160 data->cur_size = len;
161 data->timestamp = *timestamp;
163 err = avio_open_dyn_buf(&data->fragment);
167 avio_write(data->fragment, buf, len);
168 return AVERROR(EAGAIN);
170 /* else continuation == 1 */
172 /* Fragment other than first */
173 if (!data->fragment) {
174 av_log(ctx, AV_LOG_WARNING,
175 "Received packet without a start fragment; dropping.\n");
176 return AVERROR(EAGAIN);
178 if (adu_size = data->adu_size ||
179 data->timestamp != *timestamp) {
181 av_log(ctx, AV_LOG_ERROR, "Invalid packet received\n");
182 return AVERROR_INVALIDDATA;
185 avio_write(data->fragment, buf, len);
186 data->cur_size += len;
188 if (data->cur_size < data->adu_size)
189 return AVERROR(EAGAIN);
191 err = ff_rtp_finalize_packet(pkt, &data->fragment, st->index);
193 av_log(ctx, AV_LOG_ERROR,
194 "Error occurred when getting fragment buffer.\n");
201 RTPDynamicProtocolHandler ff_mpeg_audio_robust_dynamic_handler = {
202 .enc_name = "mpa-robust",
203 .codec_type = AVMEDIA_TYPE_AUDIO,
204 .codec_id = AV_CODEC_ID_MP3ADU,
205 .need_parsing = AVSTREAM_PARSE_HEADERS,
206 .priv_data_size = sizeof(PayloadContext),
207 .free = mpa_robust_free_context,
208 .parse_packet = mpa_robust_parse_packet,