| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * QDesign Music 2 (QDM2) payload for RTP | ||
| 3 | * Copyright (c) 2010 Ronald S. Bultje | ||
| 4 | * | ||
| 5 | * This file is part of FFmpeg. | ||
| 6 | * | ||
| 7 | * FFmpeg is free software; you can redistribute it and/or | ||
| 8 | * modify it under the terms of the GNU Lesser General Public | ||
| 9 | * License as published by the Free Software Foundation; either | ||
| 10 | * version 2.1 of the License, or (at your option) any later version. | ||
| 11 | * | ||
| 12 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * Lesser General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU Lesser General Public | ||
| 18 | * License along with FFmpeg; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | |||
| 22 | /** | ||
| 23 | * @file | ||
| 24 | * @brief RTP support for the QDM2 payload (todo: wiki) | ||
| 25 | * @author Ronald S. Bultje <rbultje@ronald.bitfreak.net> | ||
| 26 | */ | ||
| 27 | |||
| 28 | #include <string.h> | ||
| 29 | #include "libavutil/avassert.h" | ||
| 30 | #include "libavutil/intreadwrite.h" | ||
| 31 | #include "internal.h" | ||
| 32 | #include "rtp.h" | ||
| 33 | #include "rtpdec.h" | ||
| 34 | #include "rtpdec_formats.h" | ||
| 35 | |||
| 36 | struct PayloadContext { | ||
| 37 | /** values read from the config header, used as packet headers */ | ||
| 38 | //@{ | ||
| 39 | int block_type; ///< superblock type, value 2 .. 8 | ||
| 40 | int block_size; ///< from extradata, used as pkt length | ||
| 41 | int subpkts_per_block; ///< max. nr. of subpackets to add per output buffer | ||
| 42 | //@} | ||
| 43 | |||
| 44 | /** Temporary storage for superblock restoring, per packet ID (0x80 total) */ | ||
| 45 | //@{ | ||
| 46 | uint16_t len[0x80]; ///< how much the temporary buffer is filled | ||
| 47 | uint8_t buf[0x80][0x800]; ///< the temporary storage buffer | ||
| 48 | |||
| 49 | unsigned int cache; ///< number of data packets that we have cached right now | ||
| 50 | unsigned int n_pkts; ///< number of RTP packets received since last packet output / config | ||
| 51 | uint32_t timestamp; ///< timestamp of next-to-be-returned packet | ||
| 52 | //@} | ||
| 53 | }; | ||
| 54 | |||
| 55 | /** | ||
| 56 | * Parse configuration (basically the codec-specific extradata) from | ||
| 57 | * an RTP config subpacket (starts with 0xff). | ||
| 58 | * | ||
| 59 | * Layout of the config subpacket (in bytes): | ||
| 60 | * 1: 0xFF <- config ID | ||
| 61 | * then an array { | ||
| 62 | * 1: size <- of the current item | ||
| 63 | * 1: item type <- 0 .. 4 | ||
| 64 | * size-2: data <- data depends on the item type | ||
| 65 | * } | ||
| 66 | * | ||
| 67 | * Item 0 implies the end of the config subpacket, and has no data. | ||
| 68 | * Item 1 implies a stream configuration without extradata. | ||
| 69 | * Item 2 max. nr. of subpackets per superblock | ||
| 70 | * Item 3 superblock type for the stream | ||
| 71 | * Item 4 implies a stream configuration with extradata (size >= 0x1c). | ||
| 72 | * | ||
| 73 | * @return <0 on error, otherwise the number of bytes parsed from the | ||
| 74 | * input buffer. | ||
| 75 | */ | ||
| 76 | ✗ | static int qdm2_parse_config(PayloadContext *qdm, AVStream *st, | |
| 77 | const uint8_t *buf, const uint8_t *end) | ||
| 78 | { | ||
| 79 | ✗ | const uint8_t *p = buf; | |
| 80 | int ret; | ||
| 81 | |||
| 82 | ✗ | while (end - p >= 2) { | |
| 83 | ✗ | unsigned int item_len = p[0], config_item = p[1]; | |
| 84 | |||
| 85 | ✗ | if (item_len < 2 || end - p < item_len || config_item > 4) | |
| 86 | ✗ | return AVERROR_INVALIDDATA; | |
| 87 | |||
| 88 | ✗ | switch (config_item) { | |
| 89 | ✗ | case 0: /* end of config block */ | |
| 90 | ✗ | return p - buf + item_len; | |
| 91 | ✗ | case 1: /* stream without extradata */ | |
| 92 | /* FIXME: set default qdm->block_size */ | ||
| 93 | ✗ | break; | |
| 94 | ✗ | case 2: /**< subpackets per block */ | |
| 95 | ✗ | if (item_len < 3) | |
| 96 | ✗ | return AVERROR_INVALIDDATA; | |
| 97 | ✗ | qdm->subpkts_per_block = p[2]; | |
| 98 | ✗ | break; | |
| 99 | ✗ | case 3: /* superblock type */ | |
| 100 | ✗ | if (item_len < 4) | |
| 101 | ✗ | return AVERROR_INVALIDDATA; | |
| 102 | ✗ | qdm->block_type = AV_RB16(p + 2); | |
| 103 | ✗ | break; | |
| 104 | ✗ | case 4: /* stream with extradata */ | |
| 105 | ✗ | if (item_len < 30) | |
| 106 | ✗ | return AVERROR_INVALIDDATA; | |
| 107 | |||
| 108 | ✗ | ret = ff_alloc_extradata(st->codecpar, 26 + item_len); | |
| 109 | ✗ | if (ret < 0) { | |
| 110 | ✗ | return ret; | |
| 111 | } | ||
| 112 | ✗ | AV_WB32(st->codecpar->extradata, 12); | |
| 113 | ✗ | memcpy(st->codecpar->extradata + 4, "frma", 4); | |
| 114 | ✗ | memcpy(st->codecpar->extradata + 8, "QDM2", 4); | |
| 115 | ✗ | AV_WB32(st->codecpar->extradata + 12, 6 + item_len); | |
| 116 | ✗ | memcpy(st->codecpar->extradata + 16, "QDCA", 4); | |
| 117 | ✗ | memcpy(st->codecpar->extradata + 20, p + 2, item_len - 2); | |
| 118 | ✗ | AV_WB32(st->codecpar->extradata + 18 + item_len, 8); | |
| 119 | ✗ | AV_WB32(st->codecpar->extradata + 22 + item_len, 0); | |
| 120 | |||
| 121 | ✗ | qdm->block_size = AV_RB32(p + 26); | |
| 122 | ✗ | break; | |
| 123 | } | ||
| 124 | |||
| 125 | ✗ | p += item_len; | |
| 126 | } | ||
| 127 | |||
| 128 | ✗ | return AVERROR(EAGAIN); /* not enough data */ | |
| 129 | } | ||
| 130 | |||
| 131 | /** | ||
| 132 | * Parse a single subpacket. We store this subpacket in an intermediate | ||
| 133 | * buffer (position depends on the ID (byte[0]). When called, at least | ||
| 134 | * 4 bytes are available for reading (see qdm2_parse_packet()). | ||
| 135 | * | ||
| 136 | * Layout of a single subpacket (RTP packets commonly contain multiple | ||
| 137 | * such subpackets) - length in bytes: | ||
| 138 | * 1: ordering ID <- 0 .. 0x7F | ||
| 139 | * 1: subpacket type <- 0 .. 0x7F; value & 0x80 means subpacket length = 2 bytes, else 1 byte | ||
| 140 | * 1/2: subpacket length <- length of the data following the flags/length fields | ||
| 141 | * if (subpacket type & 0x7F) == 0x7F | ||
| 142 | * 1: subpacket type, higher bits | ||
| 143 | * size: subpacket data | ||
| 144 | * | ||
| 145 | * The subpackets come in randomly, and should be encapsulated into 1 | ||
| 146 | * or more superblocks (containing qdm->subpkts_per_block subpackets | ||
| 147 | * each) per RTP packet, in order of ascending "ordering ID", see | ||
| 148 | * qdm2_restore_block(). | ||
| 149 | * | ||
| 150 | * @return <0 on error, otherwise the number of bytes parsed from the | ||
| 151 | * input buffer. | ||
| 152 | */ | ||
| 153 | ✗ | static int qdm2_parse_subpacket(PayloadContext *qdm, AVStream *st, | |
| 154 | const uint8_t *buf, const uint8_t *end) | ||
| 155 | { | ||
| 156 | ✗ | const uint8_t *p = buf; | |
| 157 | unsigned int id, len, type, to_copy; | ||
| 158 | |||
| 159 | /* parse header so we know the size of the header/data */ | ||
| 160 | ✗ | id = *p++; | |
| 161 | ✗ | type = *p++; | |
| 162 | ✗ | if (type & 0x80) { | |
| 163 | ✗ | len = AV_RB16(p); | |
| 164 | ✗ | p += 2; | |
| 165 | ✗ | type &= 0x7F; | |
| 166 | } else | ||
| 167 | ✗ | len = *p++; | |
| 168 | |||
| 169 | ✗ | if (end - p < len + (type == 0x7F) || id >= 0x80) | |
| 170 | ✗ | return AVERROR_INVALIDDATA; | |
| 171 | ✗ | if (type == 0x7F) | |
| 172 | ✗ | type |= *p++ << 8; | |
| 173 | |||
| 174 | /* copy data into a temporary buffer */ | ||
| 175 | ✗ | to_copy = FFMIN(len + (p - &buf[1]), 0x800 - qdm->len[id]); | |
| 176 | ✗ | memcpy(&qdm->buf[id][qdm->len[id]], buf + 1, to_copy); | |
| 177 | ✗ | qdm->len[id] += to_copy; | |
| 178 | |||
| 179 | ✗ | return p + len - buf; | |
| 180 | } | ||
| 181 | |||
| 182 | /** | ||
| 183 | * Add a superblock header around a set of subpackets. | ||
| 184 | * | ||
| 185 | * @return <0 on error, else 0. | ||
| 186 | */ | ||
| 187 | ✗ | static int qdm2_restore_block(PayloadContext *qdm, AVStream *st, AVPacket *pkt) | |
| 188 | { | ||
| 189 | int to_copy, n, res; | ||
| 190 | ✗ | uint8_t *p, *csum_pos = NULL; | |
| 191 | ✗ | int include_csum = qdm->block_type == 2 || qdm->block_type == 4; | |
| 192 | |||
| 193 | /* create packet to hold subpkts into a superblock */ | ||
| 194 | ✗ | av_assert0(qdm->cache > 0); | |
| 195 | ✗ | for (n = 0; n < 0x80; n++) | |
| 196 | ✗ | if (qdm->len[n] > 0) | |
| 197 | ✗ | break; | |
| 198 | ✗ | av_assert0(n < 0x80); | |
| 199 | |||
| 200 | ✗ | int min_size = 2 + (qdm->len[n] > 0xff) + 2*include_csum; | |
| 201 | |||
| 202 | ✗ | if (qdm->block_size < min_size) | |
| 203 | ✗ | return AVERROR_INVALIDDATA; | |
| 204 | |||
| 205 | ✗ | if ((res = av_new_packet(pkt, qdm->block_size)) < 0) | |
| 206 | ✗ | return res; | |
| 207 | ✗ | memset(pkt->data, 0, pkt->size); | |
| 208 | ✗ | pkt->stream_index = st->index; | |
| 209 | ✗ | p = pkt->data; | |
| 210 | |||
| 211 | /* superblock header */ | ||
| 212 | ✗ | if (qdm->len[n] > 0xff) { | |
| 213 | ✗ | *p++ = qdm->block_type | 0x80; | |
| 214 | ✗ | AV_WB16(p, qdm->len[n]); | |
| 215 | ✗ | p += 2; | |
| 216 | } else { | ||
| 217 | ✗ | *p++ = qdm->block_type; | |
| 218 | ✗ | *p++ = qdm->len[n]; | |
| 219 | } | ||
| 220 | ✗ | if (include_csum) { | |
| 221 | ✗ | csum_pos = p; | |
| 222 | ✗ | p += 2; | |
| 223 | } | ||
| 224 | |||
| 225 | /* subpacket data */ | ||
| 226 | ✗ | to_copy = FFMIN(qdm->len[n], pkt->size - (p - pkt->data)); | |
| 227 | ✗ | memcpy(p, qdm->buf[n], to_copy); | |
| 228 | ✗ | qdm->len[n] = 0; | |
| 229 | |||
| 230 | /* checksum header */ | ||
| 231 | ✗ | if (include_csum) { | |
| 232 | ✗ | unsigned int total = 0; | |
| 233 | uint8_t *q; | ||
| 234 | |||
| 235 | ✗ | for (q = pkt->data; q < &pkt->data[qdm->block_size]; q++) | |
| 236 | ✗ | total += *q; | |
| 237 | ✗ | AV_WB16(csum_pos, (uint16_t) total); | |
| 238 | } | ||
| 239 | |||
| 240 | ✗ | return 0; | |
| 241 | } | ||
| 242 | |||
| 243 | /** return 0 on packet, no more left, 1 on packet, -1 on partial packet... */ | ||
| 244 | ✗ | static int qdm2_parse_packet(AVFormatContext *s, PayloadContext *qdm, | |
| 245 | AVStream *st, AVPacket *pkt, | ||
| 246 | uint32_t *timestamp, | ||
| 247 | const uint8_t *buf, int len, uint16_t seq, | ||
| 248 | int flags) | ||
| 249 | { | ||
| 250 | ✗ | int res = AVERROR_INVALIDDATA, n; | |
| 251 | ✗ | const uint8_t *end = buf + len, *p = buf; | |
| 252 | |||
| 253 | ✗ | if (len > 0) { | |
| 254 | ✗ | if (len < 2) | |
| 255 | ✗ | return AVERROR_INVALIDDATA; | |
| 256 | |||
| 257 | /* configuration block */ | ||
| 258 | ✗ | if (*p == 0xff) { | |
| 259 | ✗ | if (qdm->n_pkts > 0) { | |
| 260 | ✗ | av_log(s, AV_LOG_WARNING, | |
| 261 | "Out of sequence config - dropping queue\n"); | ||
| 262 | ✗ | qdm->n_pkts = 0; | |
| 263 | ✗ | memset(qdm->len, 0, sizeof(qdm->len)); | |
| 264 | } | ||
| 265 | |||
| 266 | ✗ | if ((res = qdm2_parse_config(qdm, st, ++p, end)) < 0) | |
| 267 | ✗ | return res; | |
| 268 | ✗ | p += res; | |
| 269 | |||
| 270 | /* We set codec_id to AV_CODEC_ID_NONE initially to | ||
| 271 | * delay decoder initialization since extradata is | ||
| 272 | * carried within the RTP stream, not SDP. Here, | ||
| 273 | * by setting codec_id to AV_CODEC_ID_QDM2, we are signalling | ||
| 274 | * to the decoder that it is OK to initialize. */ | ||
| 275 | ✗ | st->codecpar->codec_id = AV_CODEC_ID_QDM2; | |
| 276 | } | ||
| 277 | ✗ | if (st->codecpar->codec_id == AV_CODEC_ID_NONE) | |
| 278 | ✗ | return AVERROR(EAGAIN); | |
| 279 | |||
| 280 | /* subpackets */ | ||
| 281 | ✗ | while (end - p >= 4) { | |
| 282 | ✗ | if ((res = qdm2_parse_subpacket(qdm, st, p, end)) < 0) | |
| 283 | ✗ | return res; | |
| 284 | ✗ | p += res; | |
| 285 | } | ||
| 286 | |||
| 287 | ✗ | qdm->timestamp = *timestamp; | |
| 288 | ✗ | if (++qdm->n_pkts < qdm->subpkts_per_block) | |
| 289 | ✗ | return AVERROR(EAGAIN); | |
| 290 | ✗ | qdm->cache = 0; | |
| 291 | ✗ | for (n = 0; n < 0x80; n++) | |
| 292 | ✗ | if (qdm->len[n] > 0) | |
| 293 | ✗ | qdm->cache++; | |
| 294 | } | ||
| 295 | |||
| 296 | /* output the subpackets into freshly created superblock structures */ | ||
| 297 | ✗ | if (!qdm->cache || (res = qdm2_restore_block(qdm, st, pkt)) < 0) | |
| 298 | ✗ | return res; | |
| 299 | ✗ | if (--qdm->cache == 0) | |
| 300 | ✗ | qdm->n_pkts = 0; | |
| 301 | |||
| 302 | ✗ | *timestamp = qdm->timestamp; | |
| 303 | ✗ | qdm->timestamp = RTP_NOTS_VALUE; | |
| 304 | |||
| 305 | ✗ | return (qdm->cache > 0) ? 1 : 0; | |
| 306 | } | ||
| 307 | |||
| 308 | const RTPDynamicProtocolHandler ff_qdm2_dynamic_handler = { | ||
| 309 | .enc_name = "X-QDM", | ||
| 310 | .codec_type = AVMEDIA_TYPE_AUDIO, | ||
| 311 | .codec_id = AV_CODEC_ID_NONE, | ||
| 312 | .priv_data_size = sizeof(PayloadContext), | ||
| 313 | .parse_packet = qdm2_parse_packet, | ||
| 314 | }; | ||
| 315 |