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 PayloadContext *mpa_robust_new_context(void)
38 return av_mallocz(sizeof(PayloadContext));
41 static inline void free_fragment(PayloadContext *data)
45 avio_close_dyn_buf(data->fragment, &p);
47 data->fragment = NULL;
51 static void mpa_robust_free_context(PayloadContext *data)
54 av_free(data->split_buf);
58 static int mpa_robust_parse_rtp_header(AVFormatContext *ctx,
59 const uint8_t *buf, int len,
60 unsigned *adu_size, unsigned *cont)
65 av_log(ctx, AV_LOG_ERROR, "Invalid %d bytes packet\n", len);
66 return AVERROR_INVALIDDATA;
69 *cont = !!(buf[0] & 0x80);
70 if (!(buf[0] & 0x40)) {
72 *adu_size = buf[0] & ~0xc0;
75 *adu_size = AV_RB16(buf) & ~0xc000;
81 static int mpa_robust_parse_packet(AVFormatContext *ctx, PayloadContext *data,
82 AVStream *st, AVPacket *pkt,
83 uint32_t *timestamp, const uint8_t *buf,
84 int len, uint16_t seq, int flags)
86 unsigned adu_size, continuation;
90 buf = &data->split_buf[data->split_pos];
91 len = data->split_buf_size - data->split_pos;
93 header_size = mpa_robust_parse_rtp_header(ctx, buf, len, &adu_size,
95 if (header_size < 0) {
96 av_freep(&data->split_buf);
102 if (continuation || adu_size > len) {
103 av_freep(&data->split_buf);
104 av_log(ctx, AV_LOG_ERROR, "Invalid frame\n");
105 return AVERROR_INVALIDDATA;
108 if (av_new_packet(pkt, adu_size)) {
109 av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
110 return AVERROR(ENOMEM);
113 pkt->stream_index = st->index;
114 memcpy(pkt->data, buf, adu_size);
116 data->split_pos += adu_size;
118 if (data->split_pos == data->split_buf_size) {
119 av_freep(&data->split_buf);
127 header_size = mpa_robust_parse_rtp_header(ctx, buf, len, &adu_size,
135 if (!continuation && adu_size <= len) {
136 /* One or more complete frames */
138 if (av_new_packet(pkt, adu_size)) {
139 av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
140 return AVERROR(ENOMEM);
143 pkt->stream_index = st->index;
144 memcpy(pkt->data, buf, adu_size);
149 data->split_buf_size = len;
150 data->split_buf = av_malloc(data->split_buf_size);
152 if (!data->split_buf) {
153 av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
155 return AVERROR(ENOMEM);
157 memcpy(data->split_buf, buf, data->split_buf_size);
161 } else if (!continuation) { /* && adu_size > len */
165 data->adu_size = adu_size;
166 data->cur_size = len;
167 data->timestamp = *timestamp;
169 err = avio_open_dyn_buf(&data->fragment);
173 avio_write(data->fragment, buf, len);
174 return AVERROR(EAGAIN);
176 /* else continuation == 1 */
178 /* Fragment other than first */
179 if (!data->fragment) {
180 av_log(ctx, AV_LOG_WARNING,
181 "Received packet without a start fragment; dropping.\n");
182 return AVERROR(EAGAIN);
184 if (adu_size = data->adu_size ||
185 data->timestamp != *timestamp) {
187 av_log(ctx, AV_LOG_ERROR, "Invalid packet received\n");
188 return AVERROR_INVALIDDATA;
191 avio_write(data->fragment, buf, len);
192 data->cur_size += len;
194 if (data->cur_size < data->adu_size)
195 return AVERROR(EAGAIN);
197 err = ff_rtp_finalize_packet(pkt, &data->fragment, st->index);
199 av_log(ctx, AV_LOG_ERROR,
200 "Error occurred when getting fragment buffer.\n");
207 RTPDynamicProtocolHandler ff_mpeg_audio_robust_dynamic_handler = {
208 .enc_name = "mpa-robust",
209 .codec_type = AVMEDIA_TYPE_AUDIO,
210 .codec_id = AV_CODEC_ID_MP3ADU,
211 .need_parsing = AVSTREAM_PARSE_HEADERS,
212 .alloc = mpa_robust_new_context,
213 .free = mpa_robust_free_context,
214 .parse_packet = mpa_robust_parse_packet,