Browse code

rtpdec: Add a dynamic payload handler for the x-Purevoice format, RFC 2658

This fixes roundup issue 2390.

Originally committed as revision 25889 to svn://svn.ffmpeg.org/ffmpeg/trunk

Martin Storsjö authored on 2010/12/06 04:37:45
Showing 6 changed files
... ...
@@ -62,6 +62,7 @@ version <next>:
62 62
 - Mobotix .mxg demuxer
63 63
 - frei0r source added
64 64
 - hqdn3d filter added
65
+- RTP depacketization of QCELP
65 66
 
66 67
 
67 68
 version 0.6:
... ...
@@ -239,6 +239,7 @@ OBJS-$(CONFIG_RTPDEC)                    += rdt.o         \
239 239
                                             rtpdec_h264.o \
240 240
                                             rtpdec_latm.o \
241 241
                                             rtpdec_mpeg4.o \
242
+                                            rtpdec_qcelp.o \
242 243
                                             rtpdec_qdm2.o \
243 244
                                             rtpdec_qt.o   \
244 245
                                             rtpdec_svq3.o \
... ...
@@ -22,8 +22,8 @@
22 22
 #define AVFORMAT_AVFORMAT_H
23 23
 
24 24
 #define LIBAVFORMAT_VERSION_MAJOR 52
25
-#define LIBAVFORMAT_VERSION_MINOR 87
26
-#define LIBAVFORMAT_VERSION_MICRO  1
25
+#define LIBAVFORMAT_VERSION_MINOR 88
26
+#define LIBAVFORMAT_VERSION_MICRO  0
27 27
 
28 28
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
29 29
                                                LIBAVFORMAT_VERSION_MINOR, \
... ...
@@ -67,6 +67,7 @@ void av_register_rtp_dynamic_payload_handlers(void)
67 67
     ff_register_dynamic_payload_handler(&ff_svq3_dynamic_handler);
68 68
     ff_register_dynamic_payload_handler(&ff_mp4a_latm_dynamic_handler);
69 69
     ff_register_dynamic_payload_handler(&ff_vp8_dynamic_handler);
70
+    ff_register_dynamic_payload_handler(&ff_qcelp_dynamic_handler);
70 71
 
71 72
     ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfv_handler);
72 73
     ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfa_handler);
... ...
@@ -41,6 +41,7 @@ extern RTPDynamicProtocolHandler ff_mp4v_es_dynamic_handler;
41 41
 extern RTPDynamicProtocolHandler ff_mpeg4_generic_dynamic_handler;
42 42
 extern RTPDynamicProtocolHandler ff_ms_rtp_asf_pfa_handler;
43 43
 extern RTPDynamicProtocolHandler ff_ms_rtp_asf_pfv_handler;
44
+extern RTPDynamicProtocolHandler ff_qcelp_dynamic_handler;
44 45
 extern RTPDynamicProtocolHandler ff_qdm2_dynamic_handler;
45 46
 extern RTPDynamicProtocolHandler ff_qt_rtp_aud_handler;
46 47
 extern RTPDynamicProtocolHandler ff_qt_rtp_vid_handler;
47 48
new file mode 100644
... ...
@@ -0,0 +1,228 @@
0
+/**
1
+ * RTP Depacketization of QCELP/PureVoice, RFC 2658
2
+ * Copyright (c) 2010 Martin Storsjo
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 "rtpdec_formats.h"
22
+
23
+static const uint8_t frame_sizes[] = {
24
+    1, 4, 8, 17, 35
25
+};
26
+
27
+typedef struct {
28
+    int pos;
29
+    int size;
30
+    /* The largest frame is 35 bytes, only 10 frames are allowed per
31
+     * packet, and we return the first one immediately, so allocate
32
+     * space for 9 frames */
33
+    uint8_t data[35*9];
34
+} InterleavePacket;
35
+
36
+struct PayloadContext {
37
+    int interleave_size;
38
+    int interleave_index;
39
+    InterleavePacket group[6];
40
+    int group_finished;
41
+
42
+    /* The maximum packet size, 10 frames of 35 bytes each, and one
43
+     * packet header byte. */
44
+    uint8_t  next_data[1 + 35*10];
45
+    int      next_size;
46
+    uint32_t next_timestamp;
47
+};
48
+
49
+static PayloadContext *qcelp_new_context(void)
50
+{
51
+    return av_mallocz(sizeof(PayloadContext));
52
+}
53
+
54
+static void qcelp_free_context(PayloadContext *data)
55
+{
56
+    av_free(data);
57
+}
58
+
59
+static int return_stored_frame(AVFormatContext *ctx, PayloadContext *data,
60
+                               AVStream *st, AVPacket *pkt, uint32_t *timestamp,
61
+                               const uint8_t *buf, int len);
62
+
63
+static int store_packet(AVFormatContext *ctx, PayloadContext *data,
64
+                        AVStream *st, AVPacket *pkt, uint32_t *timestamp,
65
+                        const uint8_t *buf, int len)
66
+{
67
+    int interleave_size, interleave_index;
68
+    int frame_size, ret;
69
+    InterleavePacket* ip;
70
+
71
+    if (len < 2)
72
+        return AVERROR_INVALIDDATA;
73
+
74
+    interleave_size  = buf[0] >> 3 & 7;
75
+    interleave_index = buf[0]      & 7;
76
+
77
+    if (interleave_size > 5) {
78
+        av_log(ctx, AV_LOG_ERROR, "Invalid interleave size %d\n",
79
+                                   interleave_size);
80
+        return AVERROR_INVALIDDATA;
81
+    }
82
+    if (interleave_index > interleave_size) {
83
+        av_log(ctx, AV_LOG_ERROR, "Invalid interleave index %d/%d\n",
84
+                                   interleave_index, interleave_size);
85
+        return AVERROR_INVALIDDATA;
86
+    }
87
+    if (interleave_size != data->interleave_size) {
88
+        int i;
89
+        /* First packet, or changed interleave size */
90
+        data->interleave_size = interleave_size;
91
+        data->interleave_index = 0;
92
+        for (i = 0; i < 6; i++)
93
+            data->group[i].size = 0;
94
+    }
95
+
96
+    if (interleave_index < data->interleave_index) {
97
+        /* Wrapped around - missed the last packet of the previous group. */
98
+        if (data->group_finished) {
99
+            /* No more data in the packets in this interleaving group, just
100
+             * start processing the next one */
101
+            data->interleave_index = 0;
102
+        } else {
103
+            /* Stash away the current packet, emit everything we have of the
104
+             * previous group. */
105
+            for (; data->interleave_index <= interleave_size;
106
+                 data->interleave_index++)
107
+                data->group[data->interleave_index].size = 0;
108
+
109
+            if (len > sizeof(data->next_data))
110
+                return AVERROR_INVALIDDATA;
111
+            memcpy(data->next_data, buf, len);
112
+            data->next_size = len;
113
+            data->next_timestamp = *timestamp;
114
+            *timestamp = RTP_NOTS_VALUE;
115
+
116
+            data->interleave_index = 0;
117
+            return return_stored_frame(ctx, data, st, pkt, timestamp, buf, len);
118
+        }
119
+    }
120
+    if (interleave_index > data->interleave_index) {
121
+        /* We missed a packet */
122
+        for (; data->interleave_index < interleave_index;
123
+             data->interleave_index++)
124
+            data->group[data->interleave_index].size = 0;
125
+    }
126
+    data->interleave_index = interleave_index;
127
+
128
+    if (buf[1] >= FF_ARRAY_ELEMS(frame_sizes))
129
+        return AVERROR_INVALIDDATA;
130
+    frame_size = frame_sizes[buf[1]];
131
+    if (1 + frame_size > len)
132
+        return AVERROR_INVALIDDATA;
133
+
134
+    if (len - 1 - frame_size > sizeof(data->group[0].data))
135
+        return AVERROR_INVALIDDATA;
136
+
137
+    if ((ret = av_new_packet(pkt, frame_size)) < 0)
138
+        return ret;
139
+    memcpy(pkt->data, &buf[1], frame_size);
140
+    pkt->stream_index = st->index;
141
+
142
+    ip = &data->group[data->interleave_index];
143
+    ip->size = len - 1 - frame_size;
144
+    ip->pos = 0;
145
+    memcpy(ip->data, &buf[1 + frame_size], ip->size);
146
+    /* Each packet must contain the same number of frames according to the
147
+     * RFC. If there's no data left in this packet, there shouldn't be any
148
+     * in any of the other frames in the interleaving group either. */
149
+    data->group_finished = ip->size == 0;
150
+
151
+    if (interleave_index == interleave_size) {
152
+        data->interleave_index = 0;
153
+        return !data->group_finished;
154
+    } else {
155
+        data->interleave_index++;
156
+        return 0;
157
+    }
158
+}
159
+
160
+static int return_stored_frame(AVFormatContext *ctx, PayloadContext *data,
161
+                               AVStream *st, AVPacket *pkt, uint32_t *timestamp,
162
+                               const uint8_t *buf, int len)
163
+{
164
+    InterleavePacket* ip = &data->group[data->interleave_index];
165
+    int frame_size, ret;
166
+
167
+    if (data->group_finished && data->interleave_index == 0) {
168
+        *timestamp = data->next_timestamp;
169
+        ret = store_packet(ctx, data, st, pkt, timestamp, data->next_data,
170
+                           data->next_size);
171
+        data->next_size = 0;
172
+        return ret;
173
+    }
174
+
175
+    if (ip->size == 0) {
176
+        /* No stored data for this interleave block, output an empty packet */
177
+        if ((ret = av_new_packet(pkt, 1)) < 0)
178
+            return ret;
179
+        pkt->data[0] = 0; // Blank - could also be 14, Erasure
180
+    } else {
181
+        if (ip->pos >= ip->size)
182
+            return AVERROR_INVALIDDATA;
183
+        if (ip->data[ip->pos] >= FF_ARRAY_ELEMS(frame_sizes))
184
+            return AVERROR_INVALIDDATA;
185
+        frame_size = frame_sizes[ip->data[ip->pos]];
186
+        if (ip->pos + frame_size > ip->size)
187
+            return AVERROR_INVALIDDATA;
188
+
189
+        if ((ret = av_new_packet(pkt, frame_size)) < 0)
190
+            return ret;
191
+        memcpy(pkt->data, &ip->data[ip->pos], frame_size);
192
+
193
+        ip->pos += frame_size;
194
+        data->group_finished = ip->pos >= ip->size;
195
+    }
196
+    pkt->stream_index = st->index;
197
+
198
+    if (data->interleave_index == data->interleave_size) {
199
+        data->interleave_index = 0;
200
+        if (!data->group_finished)
201
+            return 1;
202
+        else
203
+            return data->next_size > 0;
204
+    } else {
205
+        data->interleave_index++;
206
+        return 1;
207
+    }
208
+}
209
+
210
+static int qcelp_parse_packet(AVFormatContext *ctx, PayloadContext *data,
211
+                              AVStream *st, AVPacket *pkt, uint32_t *timestamp,
212
+                              const uint8_t *buf, int len, int flags)
213
+{
214
+    if (buf)
215
+        return store_packet(ctx, data, st, pkt, timestamp, buf, len);
216
+    else
217
+        return return_stored_frame(ctx, data, st, pkt, timestamp, buf, len);
218
+}
219
+
220
+RTPDynamicProtocolHandler ff_qcelp_dynamic_handler = {
221
+    .enc_name           = "x-Purevoice",
222
+    .codec_type         = AVMEDIA_TYPE_AUDIO,
223
+    .codec_id           = CODEC_ID_QCELP,
224
+    .open               = qcelp_new_context,
225
+    .close              = qcelp_free_context,
226
+    .parse_packet       = qcelp_parse_packet
227
+};