00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #include <string.h>
00029 #include "libavutil/avassert.h"
00030 #include "libavutil/intreadwrite.h"
00031 #include "libavcodec/avcodec.h"
00032 #include "rtp.h"
00033 #include "rtpdec.h"
00034 #include "rtpdec_formats.h"
00035
00036 struct PayloadContext {
00039 int block_type;
00040 int block_size;
00041 int subpkts_per_block;
00042
00043
00046 uint16_t len[0x80];
00047 uint8_t buf[0x80][0x800];
00048
00049 unsigned int cache;
00050 unsigned int n_pkts;
00051 uint32_t timestamp;
00052
00053 };
00054
00076 static int qdm2_parse_config(PayloadContext *qdm, AVStream *st,
00077 const uint8_t *buf, const uint8_t *end)
00078 {
00079 const uint8_t *p = buf;
00080
00081 while (end - p >= 2) {
00082 unsigned int item_len = p[0], config_item = p[1];
00083
00084 if (item_len < 2 || end - p < item_len || config_item > 4)
00085 return AVERROR_INVALIDDATA;
00086
00087 switch (config_item) {
00088 case 0:
00089 return p - buf + item_len;
00090 case 1:
00091
00092 break;
00093 case 2:
00094 if (item_len < 3)
00095 return AVERROR_INVALIDDATA;
00096 qdm->subpkts_per_block = p[2];
00097 break;
00098 case 3:
00099 if (item_len < 4)
00100 return AVERROR_INVALIDDATA;
00101 qdm->block_type = AV_RB16(p + 2);
00102 break;
00103 case 4:
00104 if (item_len < 30)
00105 return AVERROR_INVALIDDATA;
00106 av_freep(&st->codec->extradata);
00107 st->codec->extradata_size = 26 + item_len;
00108 if (!(st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE))) {
00109 st->codec->extradata_size = 0;
00110 return AVERROR(ENOMEM);
00111 }
00112 AV_WB32(st->codec->extradata, 12);
00113 memcpy(st->codec->extradata + 4, "frma", 4);
00114 memcpy(st->codec->extradata + 8, "QDM2", 4);
00115 AV_WB32(st->codec->extradata + 12, 6 + item_len);
00116 memcpy(st->codec->extradata + 16, "QDCA", 4);
00117 memcpy(st->codec->extradata + 20, p + 2, item_len - 2);
00118 AV_WB32(st->codec->extradata + 18 + item_len, 8);
00119 AV_WB32(st->codec->extradata + 22 + item_len, 0);
00120
00121 qdm->block_size = AV_RB32(p + 26);
00122 break;
00123 }
00124
00125 p += item_len;
00126 }
00127
00128 return AVERROR(EAGAIN);
00129 }
00130
00153 static int qdm2_parse_subpacket(PayloadContext *qdm, AVStream *st,
00154 const uint8_t *buf, const uint8_t *end)
00155 {
00156 const uint8_t *p = buf;
00157 unsigned int id, len, type, to_copy;
00158
00159
00160 id = *p++;
00161 type = *p++;
00162 if (type & 0x80) {
00163 len = AV_RB16(p);
00164 p += 2;
00165 type &= 0x7F;
00166 } else
00167 len = *p++;
00168
00169 if (end - p < len + (type == 0x7F) || id >= 0x80)
00170 return AVERROR_INVALIDDATA;
00171 if (type == 0x7F)
00172 type |= *p++ << 8;
00173
00174
00175 to_copy = FFMIN(len + (p - &buf[1]), 0x800 - qdm->len[id]);
00176 memcpy(&qdm->buf[id][qdm->len[id]], buf + 1, to_copy);
00177 qdm->len[id] += to_copy;
00178
00179 return p + len - buf;
00180 }
00181
00187 static int qdm2_restore_block(PayloadContext *qdm, AVStream *st, AVPacket *pkt)
00188 {
00189 int to_copy, n, res, include_csum;
00190 uint8_t *p, *csum_pos = NULL;
00191
00192
00193 assert(qdm->cache > 0);
00194 for (n = 0; n < 0x80; n++)
00195 if (qdm->len[n] > 0)
00196 break;
00197 av_assert0(n < 0x80);
00198
00199 if ((res = av_new_packet(pkt, qdm->block_size)) < 0)
00200 return res;
00201 memset(pkt->data, 0, pkt->size);
00202 pkt->stream_index = st->index;
00203 p = pkt->data;
00204
00205
00206 if (qdm->len[n] > 0xff) {
00207 *p++ = qdm->block_type | 0x80;
00208 AV_WB16(p, qdm->len[n]);
00209 p += 2;
00210 } else {
00211 *p++ = qdm->block_type;
00212 *p++ = qdm->len[n];
00213 }
00214 if ((include_csum = (qdm->block_type == 2 || qdm->block_type == 4))) {
00215 csum_pos = p;
00216 p += 2;
00217 }
00218
00219
00220 to_copy = FFMIN(qdm->len[n], pkt->size - (p - pkt->data));
00221 memcpy(p, qdm->buf[n], to_copy);
00222 qdm->len[n] = 0;
00223
00224
00225 if (include_csum) {
00226 unsigned int total = 0;
00227 uint8_t *q;
00228
00229 for (q = pkt->data; q < &pkt->data[qdm->block_size]; q++)
00230 total += *q;
00231 AV_WB16(csum_pos, (uint16_t) total);
00232 }
00233
00234 return 0;
00235 }
00236
00238 static int qdm2_parse_packet(AVFormatContext *s, PayloadContext *qdm,
00239 AVStream *st, AVPacket *pkt,
00240 uint32_t *timestamp,
00241 const uint8_t *buf, int len, int flags)
00242 {
00243 int res = AVERROR_INVALIDDATA, n;
00244 const uint8_t *end = buf + len, *p = buf;
00245
00246 if (len > 0) {
00247 if (len < 2)
00248 return AVERROR_INVALIDDATA;
00249
00250
00251 if (*p == 0xff) {
00252 if (qdm->n_pkts > 0) {
00253 av_log(s, AV_LOG_WARNING,
00254 "Out of sequence config - dropping queue\n");
00255 qdm->n_pkts = 0;
00256 memset(qdm->len, 0, sizeof(qdm->len));
00257 }
00258
00259 if ((res = qdm2_parse_config(qdm, st, ++p, end)) < 0)
00260 return res;
00261 p += res;
00262
00263
00264
00265
00266
00267
00268 st->codec->codec_id = AV_CODEC_ID_QDM2;
00269 }
00270 if (st->codec->codec_id == AV_CODEC_ID_NONE)
00271 return AVERROR(EAGAIN);
00272
00273
00274 while (end - p >= 4) {
00275 if ((res = qdm2_parse_subpacket(qdm, st, p, end)) < 0)
00276 return res;
00277 p += res;
00278 }
00279
00280 qdm->timestamp = *timestamp;
00281 if (++qdm->n_pkts < qdm->subpkts_per_block)
00282 return AVERROR(EAGAIN);
00283 qdm->cache = 0;
00284 for (n = 0; n < 0x80; n++)
00285 if (qdm->len[n] > 0)
00286 qdm->cache++;
00287 }
00288
00289
00290 if (!qdm->cache || (res = qdm2_restore_block(qdm, st, pkt)) < 0)
00291 return res;
00292 if (--qdm->cache == 0)
00293 qdm->n_pkts = 0;
00294
00295 *timestamp = qdm->timestamp;
00296 qdm->timestamp = RTP_NOTS_VALUE;
00297
00298 return (qdm->cache > 0) ? 1 : 0;
00299 }
00300
00301 static PayloadContext *qdm2_extradata_new(void)
00302 {
00303 return av_mallocz(sizeof(PayloadContext));
00304 }
00305
00306 static void qdm2_extradata_free(PayloadContext *qdm)
00307 {
00308 av_free(qdm);
00309 }
00310
00311 RTPDynamicProtocolHandler ff_qdm2_dynamic_handler = {
00312 .enc_name = "X-QDM",
00313 .codec_type = AVMEDIA_TYPE_AUDIO,
00314 .codec_id = AV_CODEC_ID_NONE,
00315 .alloc = qdm2_extradata_new,
00316 .free = qdm2_extradata_free,
00317 .parse_packet = qdm2_parse_packet,
00318 };