123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- #include <string.h>
- #include "libavutil/intreadwrite.h"
- #include "libavcodec/avcodec.h"
- #include "rtp.h"
- #include "rtpdec.h"
- #include "rtpdec_qdm2.h"
- struct PayloadContext {
-
-
- int block_type;
- int block_size;
- int subpkts_per_block;
-
-
-
- uint16_t len[0x80];
- uint8_t buf[0x80][0x800];
- unsigned int cache;
- unsigned int n_pkts;
- uint32_t timestamp;
-
- };
- static int qdm2_parse_config(PayloadContext *qdm, AVStream *st,
- const uint8_t *buf, const uint8_t *end)
- {
- const uint8_t *p = buf;
- while (end - p >= 2) {
- unsigned int item_len = p[0], config_item = p[1];
- if (item_len < 2 || end - p < item_len || config_item > 4)
- return AVERROR_INVALIDDATA;
- switch (config_item) {
- case 0:
- return p - buf + item_len;
- case 1:
-
- break;
- case 2:
- if (item_len < 3)
- return AVERROR_INVALIDDATA;
- qdm->subpkts_per_block = p[2];
- break;
- case 3:
- if (item_len < 4)
- return AVERROR_INVALIDDATA;
- qdm->block_type = AV_RB16(p + 2);
- break;
- case 4:
- if (item_len < 30)
- return AVERROR_INVALIDDATA;
- av_freep(&st->codec->extradata);
- st->codec->extradata_size = 26 + item_len;
- if (!(st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE))) {
- st->codec->extradata_size = 0;
- return AVERROR(ENOMEM);
- }
- AV_WB32(st->codec->extradata, 12);
- memcpy(st->codec->extradata + 4, "frma", 4);
- memcpy(st->codec->extradata + 8, "QDM2", 4);
- AV_WB32(st->codec->extradata + 12, 6 + item_len);
- memcpy(st->codec->extradata + 16, "QDCA", 4);
- memcpy(st->codec->extradata + 20, p + 2, item_len - 2);
- AV_WB32(st->codec->extradata + 18 + item_len, 8);
- AV_WB32(st->codec->extradata + 22 + item_len, 0);
- qdm->block_size = AV_RB32(p + 26);
- break;
- }
- p += item_len;
- }
- return AVERROR(EAGAIN);
- }
- static int qdm2_parse_subpacket(PayloadContext *qdm, AVStream *st,
- const uint8_t *buf, const uint8_t *end)
- {
- const uint8_t *p = buf;
- unsigned int id, len, type, to_copy;
-
- id = *p++;
- type = *p++;
- if (type & 0x80) {
- len = AV_RB16(p);
- p += 2;
- type &= 0x7F;
- } else
- len = *p++;
- if (end - p < len + (type == 0x7F) || id >= 0x80)
- return AVERROR_INVALIDDATA;
- if (type == 0x7F)
- type |= *p++ << 8;
-
- to_copy = FFMIN(len + (p - &buf[1]), 0x800 - qdm->len[id]);
- memcpy(&qdm->buf[id][qdm->len[id]], buf + 1, to_copy);
- qdm->len[id] += to_copy;
- return p + len - buf;
- }
- static int qdm2_restore_block(PayloadContext *qdm, AVStream *st, AVPacket *pkt)
- {
- int to_copy, n, res, include_csum;
- uint8_t *p, *csum_pos = NULL;
-
- assert(qdm->cache > 0);
- for (n = 0; n < 0x80; n++)
- if (qdm->len[n] > 0)
- break;
- assert(n < 0x80);
- if ((res = av_new_packet(pkt, qdm->block_size)) < 0)
- return res;
- memset(pkt->data, 0, pkt->size);
- pkt->stream_index = st->index;
- p = pkt->data;
-
- if (qdm->len[n] > 0xff) {
- *p++ = qdm->block_type | 0x80;
- AV_WB16(p, qdm->len[n]);
- p += 2;
- } else {
- *p++ = qdm->block_type;
- *p++ = qdm->len[n];
- }
- if ((include_csum = (qdm->block_type == 2 || qdm->block_type == 4))) {
- csum_pos = p;
- p += 2;
- }
-
- to_copy = FFMIN(qdm->len[n], pkt->size - (p - pkt->data));
- memcpy(p, qdm->buf[n], to_copy);
- qdm->len[n] = 0;
-
- if (include_csum) {
- unsigned int total = 0;
- uint8_t *q;
- for (q = pkt->data; q < &pkt->data[qdm->block_size]; q++)
- total += *q;
- AV_WB16(csum_pos, (uint16_t) total);
- }
- return 0;
- }
- static int qdm2_parse_packet(AVFormatContext *s, PayloadContext *qdm,
- AVStream *st, AVPacket *pkt,
- uint32_t *timestamp,
- const uint8_t *buf, int len, int flags)
- {
- int res = AVERROR_INVALIDDATA, n;
- const uint8_t *end = buf + len, *p = buf;
- if (len > 0) {
- if (len < 2)
- return AVERROR_INVALIDDATA;
-
- if (*p == 0xff) {
- if (qdm->n_pkts > 0) {
- av_log(s, AV_LOG_WARNING,
- "Out of sequence config - dropping queue\n");
- qdm->n_pkts = 0;
- memset(qdm->len, 0, sizeof(qdm->len));
- }
- if ((res = qdm2_parse_config(qdm, st, ++p, end)) < 0)
- return res;
- p += res;
-
- st->codec->codec_id = CODEC_ID_QDM2;
- }
-
- while (end - p >= 4) {
- if ((res = qdm2_parse_subpacket(qdm, st, p, end)) < 0)
- return res;
- p += res;
- }
- qdm->timestamp = *timestamp;
- if (++qdm->n_pkts < qdm->subpkts_per_block)
- return AVERROR(EAGAIN);
- qdm->cache = 0;
- for (n = 0; n < 0x80; n++)
- if (qdm->len[n] > 0)
- qdm->cache++;
- }
-
- if (!qdm->cache || (res = qdm2_restore_block(qdm, st, pkt)) < 0)
- return res;
- if (--qdm->cache == 0)
- qdm->n_pkts = 0;
- *timestamp = qdm->timestamp;
- qdm->timestamp = RTP_NOTS_VALUE;
- return (qdm->cache > 0) ? 1 : 0;
- }
- static PayloadContext *qdm2_extradata_new(void)
- {
- return av_mallocz(sizeof(PayloadContext));
- }
- static void qdm2_extradata_free(PayloadContext *qdm)
- {
- av_free(qdm);
- }
- RTPDynamicProtocolHandler ff_qdm2_dynamic_handler = {
- "X-QDM",
- CODEC_TYPE_AUDIO,
- CODEC_ID_NONE,
- NULL,
- qdm2_extradata_new,
- qdm2_extradata_free,
- qdm2_parse_packet,
- };
|