Browse code

Merge commit '96084251e57d1738fde02a2b0d37ca609d9efd71'

* commit '96084251e57d1738fde02a2b0d37ca609d9efd71':
libavformat: add robust MPEG audio depacketization (RFC 5219)

Conflicts:
Changelog
libavformat/version.h

See: 22470510d1f9441e848bbe107c7963b6d492b47f
Merged-by: Michael Niedermayer <michaelni@gmx.at>

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