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 av_cold int mpa_robust_init(AVFormatContext *ctx, int st_index,
41 ctx->streams[st_index]->need_parsing = AVSTREAM_PARSE_HEADERS;
45 static PayloadContext *mpa_robust_new_context(void)
47 return av_mallocz(sizeof(PayloadContext));
50 static inline void free_fragment(PayloadContext *data)
54 avio_close_dyn_buf(data->fragment, &p);
56 data->fragment = NULL;
60 static void mpa_robust_free_context(PayloadContext *data)
63 av_free(data->split_buf);
67 static int mpa_robust_parse_rtp_header(AVFormatContext *ctx,
68 const uint8_t *buf, int len,
69 unsigned *adu_size, unsigned *cont)
74 av_log(ctx, AV_LOG_ERROR, "Invalid %d bytes packet\n", len);
75 return AVERROR_INVALIDDATA;
78 *cont = !!(buf[0] & 0x80);
79 if (!(buf[0] & 0x40)) {
81 *adu_size = buf[0] & ~0xc0;
84 *adu_size = AV_RB16(buf) & ~0xc000;
90 static int mpa_robust_parse_packet(AVFormatContext *ctx, PayloadContext *data,
91 AVStream *st, AVPacket *pkt,
92 uint32_t *timestamp, const uint8_t *buf,
93 int len, uint16_t seq, int flags)
95 unsigned adu_size, continuation;
99 buf = &data->split_buf[data->split_pos];
100 len = data->split_buf_size - data->split_pos;
102 header_size = mpa_robust_parse_rtp_header(ctx, buf, len, &adu_size,
104 if (header_size < 0) {
105 av_freep(&data->split_buf);
111 if (continuation || adu_size > len) {
112 av_freep(&data->split_buf);
113 av_log(ctx, AV_LOG_ERROR, "Invalid frame\n");
114 return AVERROR_INVALIDDATA;
117 if (av_new_packet(pkt, adu_size)) {
118 av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
119 return AVERROR(ENOMEM);
122 pkt->stream_index = st->index;
123 memcpy(pkt->data, buf, adu_size);
125 data->split_pos += adu_size;
127 if (data->split_pos == data->split_buf_size) {
128 av_freep(&data->split_buf);
136 header_size = mpa_robust_parse_rtp_header(ctx, buf, len, &adu_size,
144 if (!continuation && adu_size <= len) {
145 /* One or more complete frames */
147 if (av_new_packet(pkt, adu_size)) {
148 av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
149 return AVERROR(ENOMEM);
152 pkt->stream_index = st->index;
153 memcpy(pkt->data, buf, adu_size);
158 data->split_buf_size = len;
159 data->split_buf = av_malloc(data->split_buf_size);
161 if (!data->split_buf) {
162 av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
164 return AVERROR(ENOMEM);
166 memcpy(data->split_buf, buf, data->split_buf_size);
170 } else if (!continuation) { /* && adu_size > len */
174 data->adu_size = adu_size;
175 data->cur_size = len;
176 data->timestamp = *timestamp;
178 err = avio_open_dyn_buf(&data->fragment);
182 avio_write(data->fragment, buf, len);
183 return AVERROR(EAGAIN);
185 /* else continuation == 1 */
187 /* Fragment other than first */
188 if (!data->fragment) {
189 av_log(ctx, AV_LOG_WARNING,
190 "Received packet without a start fragment; dropping.\n");
191 return AVERROR(EAGAIN);
193 if (adu_size = data->adu_size ||
194 data->timestamp != *timestamp) {
196 av_log(ctx, AV_LOG_ERROR, "Invalid packet received\n");
197 return AVERROR_INVALIDDATA;
200 avio_write(data->fragment, buf, len);
201 data->cur_size += len;
203 if (data->cur_size < data->adu_size)
204 return AVERROR(EAGAIN);
206 err = ff_rtp_finalize_packet(pkt, &data->fragment, st->index);
208 av_log(ctx, AV_LOG_ERROR,
209 "Error occurred when getting fragment buffer.\n");
216 RTPDynamicProtocolHandler ff_mpeg_audio_robust_dynamic_handler = {
217 .codec_type = AVMEDIA_TYPE_AUDIO,
218 .codec_id = AV_CODEC_ID_MP3ADU,
219 .init = mpa_robust_init,
220 .alloc = mpa_robust_new_context,
221 .free = mpa_robust_free_context,
222 .parse_packet = mpa_robust_parse_packet,
223 .enc_name = "mpa-robust",