2 * Common code for the RTP depacketization of MPEG-1/2 formats.
3 * Copyright (c) 2002 Fabrice Bellard
5 * RTP parser for loss tolerant payload format for MP3 audio (RFC 5219)
6 * Copyright (c) 2015 Gilles Chanteperdrix <gch@xenomai.org>
8 * This file is part of FFmpeg.
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "libavutil/attributes.h"
26 #include "libavutil/intreadwrite.h"
27 #include "rtpdec_formats.h"
29 #define RTP_MPA_PAYLOAD_HEADER_SIZE 1
31 static av_cold int mpeg_init(AVFormatContext *ctx, int st_index, PayloadContext *data)
35 ctx->streams[st_index]->need_parsing = AVSTREAM_PARSE_FULL;
39 static int mpeg_parse_packet(AVFormatContext *ctx, PayloadContext *data,
40 AVStream *st, AVPacket *pkt, uint32_t *timestamp,
41 const uint8_t *buf, int len, uint16_t seq,
46 return AVERROR_INVALIDDATA;
50 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && h & (1 << 26)) {
53 return AVERROR_INVALIDDATA;
57 if (av_new_packet(pkt, len) < 0)
58 return AVERROR(ENOMEM);
59 memcpy(pkt->data, buf, len);
60 pkt->stream_index = st->index;
64 RTPDynamicProtocolHandler ff_mpeg_audio_dynamic_handler = {
65 .codec_type = AVMEDIA_TYPE_AUDIO,
66 .codec_id = AV_CODEC_ID_MP3,
68 .parse_packet = mpeg_parse_packet,
69 .static_payload_id = 14,
72 RTPDynamicProtocolHandler ff_mpeg_video_dynamic_handler = {
73 .codec_type = AVMEDIA_TYPE_VIDEO,
74 .codec_id = AV_CODEC_ID_MPEG2VIDEO,
76 .parse_packet = mpeg_parse_packet,
77 .static_payload_id = 32,
80 /* MPA-ROBUST, RFC 5219 */
81 struct PayloadContext {
86 int split_pos, split_buf_size, split_pkts;
87 AVIOContext *fragment;
90 static av_cold int mpa_robust_init(AVFormatContext *ctx, int st_index,
95 ctx->streams[st_index]->need_parsing = AVSTREAM_PARSE_HEADERS;
99 static PayloadContext *mpa_robust_new_context(void)
101 return av_mallocz(sizeof(PayloadContext));
104 static inline void free_fragment_if_needed(PayloadContext *data)
106 if (data->fragment) {
108 avio_close_dyn_buf(data->fragment, &p);
110 data->fragment = NULL;
114 static void mpa_robust_free_context(PayloadContext *data)
116 free_fragment_if_needed(data);
120 static int mpa_robust_parse_rtp_header(AVFormatContext *ctx,
121 const uint8_t *buf, int len,
122 unsigned *adu_size, unsigned *cont)
124 unsigned header_size;
126 if (len < RTP_MPA_PAYLOAD_HEADER_SIZE + 1) {
127 av_log(ctx, AV_LOG_ERROR, "Invalid %d bytes packet\n", len);
128 return AVERROR_INVALIDDATA;
131 *cont = !!(buf[0] & 0x80);
132 if (!(buf[0] & 0x40)) {
134 *adu_size = buf[0] & ~0xc0;
137 *adu_size = AV_RB16(buf) & ~0xc000;
143 static int mpa_robust_parse_packet(AVFormatContext *ctx, PayloadContext *data,
144 AVStream *st, AVPacket *pkt,
145 uint32_t *timestamp, const uint8_t *buf,
146 int len, uint16_t seq, int flags)
148 unsigned adu_size, continuation;
149 int err, header_size;
152 buf = &data->split_buf[data->split_pos];
153 len = data->split_buf_size - data->split_pos;
155 header_size = mpa_robust_parse_rtp_header(ctx, buf, len, &adu_size,
157 if (header_size < 0) {
158 av_freep(&data->split_buf);
164 if (continuation || adu_size > len) {
165 av_freep(&data->split_buf);
166 av_log(ctx, AV_LOG_ERROR, "Invalid frame\n");
167 return AVERROR_INVALIDDATA;
170 if (av_new_packet(pkt, adu_size)) {
171 av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
172 return AVERROR(ENOMEM);
175 pkt->stream_index = st->index;
176 memcpy(pkt->data, buf, adu_size);
178 data->split_pos = (buf - data->split_buf) + adu_size;
180 if (data->split_pos == data->split_buf_size) {
181 av_freep(&data->split_buf);
189 header_size = mpa_robust_parse_rtp_header(ctx, buf, len, &adu_size,
197 if (!continuation && adu_size <= len) {
198 /* One or more complete frames */
200 if (av_new_packet(pkt, adu_size)) {
201 av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
202 return AVERROR(ENOMEM);
205 pkt->stream_index = st->index;
206 memcpy(pkt->data, buf, adu_size);
211 data->split_buf_size = len;
212 data->split_buf = av_malloc(data->split_buf_size);
214 if (!data->split_buf) {
215 av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
217 return AVERROR(ENOMEM);
219 memcpy(data->split_buf, buf, data->split_buf_size);
223 } else if (!continuation) { /* && adu_size > len */
225 free_fragment_if_needed(data);
227 data->adu_size = adu_size;
228 data->cur_size = len;
229 data->timestamp = *timestamp;
231 err = avio_open_dyn_buf(&data->fragment);
235 avio_write(data->fragment, buf, len);
236 return AVERROR(EAGAIN);
238 /* else continuation == 1 */
240 /* Fragment other than first */
241 if (!data->fragment) {
242 av_log(ctx, AV_LOG_WARNING,
243 "Received packet without a start fragment; dropping.\n");
244 return AVERROR(EAGAIN);
246 if (adu_size = data->adu_size ||
247 data->timestamp != *timestamp) {
248 free_fragment_if_needed(data);
249 av_log(ctx, AV_LOG_ERROR, "Invalid packet received\n");
250 return AVERROR_INVALIDDATA;
253 avio_write(data->fragment, buf, len);
254 data->cur_size += len;
256 if (data->cur_size < data->adu_size)
257 return AVERROR(EAGAIN);
259 err = ff_rtp_finalize_packet(pkt, &data->fragment, st->index);
261 av_log(ctx, AV_LOG_ERROR,
262 "Error occurred when getting fragment buffer.");
269 RTPDynamicProtocolHandler ff_mpeg_audio_robust_dynamic_handler = {
270 .codec_type = AVMEDIA_TYPE_AUDIO,
271 .codec_id = AV_CODEC_ID_MP3ADU,
272 .init = mpa_robust_init,
273 .alloc = mpa_robust_new_context,
274 .free = mpa_robust_free_context,
275 .parse_packet = mpa_robust_parse_packet,
276 .enc_name = "mpa-robust",