1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
/*
* Common code for the RTP depacketization of MPEG-1/2 formats.
* Copyright (c) 2002 Fabrice Bellard
*
* RTP parser for loss tolerant payload format for MP3 audio (RFC 5219)
* Copyright (c) 2015 Gilles Chanteperdrix <gch@xenomai.org>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "libavutil/attributes.h"
#include "libavutil/intreadwrite.h"
#include "rtpdec_formats.h"
#define RTP_MPA_PAYLOAD_HEADER_SIZE 1
static av_cold int mpeg_init(AVFormatContext *ctx, int st_index, PayloadContext *data)
{
if (st_index < 0)
return 0;
ctx->streams[st_index]->need_parsing = AVSTREAM_PARSE_FULL;
return 0;
}
static int mpeg_parse_packet(AVFormatContext *ctx, PayloadContext *data,
AVStream *st, AVPacket *pkt, uint32_t *timestamp,
const uint8_t *buf, int len, uint16_t seq,
int flags)
{
unsigned int h;
if (len <= 4)
return AVERROR_INVALIDDATA;
h = AV_RB32(buf);
buf += 4;
len -= 4;
if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && h & (1 << 26)) {
/* MPEG-2 */
if (len <= 4)
return AVERROR_INVALIDDATA;
buf += 4;
len -= 4;
}
if (av_new_packet(pkt, len) < 0)
return AVERROR(ENOMEM);
memcpy(pkt->data, buf, len);
pkt->stream_index = st->index;
return 0;
}
RTPDynamicProtocolHandler ff_mpeg_audio_dynamic_handler = {
.codec_type = AVMEDIA_TYPE_AUDIO,
.codec_id = AV_CODEC_ID_MP3,
.init = mpeg_init,
.parse_packet = mpeg_parse_packet,
.static_payload_id = 14,
};
RTPDynamicProtocolHandler ff_mpeg_video_dynamic_handler = {
.codec_type = AVMEDIA_TYPE_VIDEO,
.codec_id = AV_CODEC_ID_MPEG2VIDEO,
.init = mpeg_init,
.parse_packet = mpeg_parse_packet,
.static_payload_id = 32,
};
/* MPA-ROBUST, RFC 5219 */
struct PayloadContext {
unsigned adu_size;
unsigned cur_size;
uint32_t timestamp;
uint8_t *split_buf;
int split_pos, split_buf_size, split_pkts;
AVIOContext *fragment;
};
static av_cold int mpa_robust_init(AVFormatContext *ctx, int st_index,
PayloadContext *data)
{
if (st_index < 0)
return 0;
ctx->streams[st_index]->need_parsing = AVSTREAM_PARSE_HEADERS;
return 0;
}
static PayloadContext *mpa_robust_new_context(void)
{
return av_mallocz(sizeof(PayloadContext));
}
static inline void free_fragment_if_needed(PayloadContext *data)
{
if (data->fragment) {
uint8_t *p;
avio_close_dyn_buf(data->fragment, &p);
av_free(p);
data->fragment = NULL;
}
}
static void mpa_robust_free_context(PayloadContext *data)
{
free_fragment_if_needed(data);
av_free(data);
}
static int mpa_robust_parse_rtp_header(AVFormatContext *ctx,
const uint8_t *buf, int len,
unsigned *adu_size, unsigned *cont)
{
unsigned header_size;
if (len < RTP_MPA_PAYLOAD_HEADER_SIZE + 1) {
av_log(ctx, AV_LOG_ERROR, "Invalid %d bytes packet\n", len);
return AVERROR_INVALIDDATA;
}
*cont = !!(buf[0] & 0x80);
if (!(buf[0] & 0x40)) {
header_size = 1;
*adu_size = buf[0] & ~0xc0;
} else {
header_size = 2;
*adu_size = AV_RB16(buf) & ~0xc000;
}
return header_size;
}
static int mpa_robust_parse_packet(AVFormatContext *ctx, PayloadContext *data,
AVStream *st, AVPacket *pkt,
uint32_t *timestamp, const uint8_t *buf,
int len, uint16_t seq, int flags)
{
unsigned adu_size, continuation;
int err, header_size;
if (!buf) {
buf = &data->split_buf[data->split_pos];
len = data->split_buf_size - data->split_pos;
header_size = mpa_robust_parse_rtp_header(ctx, buf, len, &adu_size,
&continuation);
if (header_size < 0) {
av_freep(&data->split_buf);
return header_size;
}
buf += header_size;
len -= header_size;
if (continuation || adu_size > len) {
av_freep(&data->split_buf);
av_log(ctx, AV_LOG_ERROR, "Invalid frame\n");
return AVERROR_INVALIDDATA;
}
if (av_new_packet(pkt, adu_size)) {
av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
return AVERROR(ENOMEM);
}
pkt->stream_index = st->index;
memcpy(pkt->data, buf, adu_size);
data->split_pos = (buf - data->split_buf) + adu_size;
if (data->split_pos == data->split_buf_size) {
av_freep(&data->split_buf);
return 0;
}
return 1;
}
header_size = mpa_robust_parse_rtp_header(ctx, buf, len, &adu_size,
&continuation);
if (header_size < 0)
return header_size;
buf += header_size;
len -= header_size;
if (!continuation && adu_size <= len) {
/* One or more complete frames */
if (av_new_packet(pkt, adu_size)) {
av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
return AVERROR(ENOMEM);
}
pkt->stream_index = st->index;
memcpy(pkt->data, buf, adu_size);
buf += adu_size;
len -= adu_size;
if (len) {
data->split_buf_size = len;
data->split_buf = av_malloc(data->split_buf_size);
data->split_pos = 0;
if (!data->split_buf) {
av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
av_free_packet(pkt);
return AVERROR(ENOMEM);
}
memcpy(data->split_buf, buf, data->split_buf_size);
return 1;
}
return 0;
} else if (!continuation) { /* && adu_size > len */
/* First fragment */
free_fragment_if_needed(data);
data->adu_size = adu_size;
data->cur_size = len;
data->timestamp = *timestamp;
err = avio_open_dyn_buf(&data->fragment);
if (err < 0)
return err;
avio_write(data->fragment, buf, len);
return AVERROR(EAGAIN);
}
/* else continuation == 1 */
/* Fragment other than first */
if (!data->fragment) {
av_log(ctx, AV_LOG_WARNING,
"Received packet without a start fragment; dropping.\n");
return AVERROR(EAGAIN);
}
if (adu_size = data->adu_size ||
data->timestamp != *timestamp) {
free_fragment_if_needed(data);
av_log(ctx, AV_LOG_ERROR, "Invalid packet received\n");
return AVERROR_INVALIDDATA;
}
avio_write(data->fragment, buf, len);
data->cur_size += len;
if (data->cur_size < data->adu_size)
return AVERROR(EAGAIN);
err = ff_rtp_finalize_packet(pkt, &data->fragment, st->index);
if (err < 0) {
av_log(ctx, AV_LOG_ERROR,
"Error occurred when getting fragment buffer.");
return err;
}
return 0;
}
RTPDynamicProtocolHandler ff_mpeg_audio_robust_dynamic_handler = {
.codec_type = AVMEDIA_TYPE_AUDIO,
.codec_id = AV_CODEC_ID_MP3ADU,
.init = mpa_robust_init,
.alloc = mpa_robust_new_context,
.free = mpa_robust_free_context,
.parse_packet = mpa_robust_parse_packet,
.enc_name = "mpa-robust",
};
|