rtpdec_qdm2.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. * @file
  23. * @brief RTP support for the QDM2 payload (todo: wiki)
  24. * @author Ronald S. Bultje <rbultje@ronald.bitfreak.net>
  25. */
  26. #include <string.h>
  27. #include "libavutil/intreadwrite.h"
  28. #include "libavcodec/avcodec.h"
  29. #include "rtp.h"
  30. #include "rtpdec.h"
  31. #include "rtpdec_formats.h"
  32. struct PayloadContext {
  33. /** values read from the config header, used as packet headers */
  34. //@{
  35. int block_type; ///< superblock type, value 2 .. 8
  36. int block_size; ///< from extradata, used as pkt length
  37. int subpkts_per_block; ///< max. nr. of subpackets to add per output buffer
  38. //@}
  39. /** Temporary storage for superblock restoring, per packet ID (0x80 total) */
  40. //@{
  41. uint16_t len[0x80]; ///< how much the temporary buffer is filled
  42. uint8_t buf[0x80][0x800]; ///< the temporary storage buffer
  43. unsigned int cache; ///< number of data packets that we have cached right now
  44. unsigned int n_pkts; ///< number of RTP packets received since last packet output / config
  45. uint32_t timestamp; ///< timestamp of next-to-be-returned packet
  46. //@}
  47. };
  48. /**
  49. * Parse configuration (basically the codec-specific extradata) from
  50. * an RTP config subpacket (starts with 0xff).
  51. *
  52. * Layout of the config subpacket (in bytes):
  53. * 1: 0xFF <- config ID
  54. * then an array {
  55. * 1: size <- of the current item
  56. * 1: item type <- 0 .. 4
  57. * size-2: data <- data depends on the item type
  58. * }
  59. *
  60. * Item 0 implies the end of the config subpacket, and has no data.
  61. * Item 1 implies a stream configuration without extradata.
  62. * Item 2 max. nr. of subpackets per superblock
  63. * Item 3 superblock type for the stream
  64. * Item 4 implies a stream configuration with extradata (size >= 0x1c).
  65. *
  66. * @return <0 on error, otherwise the number of bytes parsed from the
  67. * input buffer.
  68. */
  69. static int qdm2_parse_config(PayloadContext *qdm, AVStream *st,
  70. const uint8_t *buf, const uint8_t *end)
  71. {
  72. const uint8_t *p = buf;
  73. while (end - p >= 2) {
  74. unsigned int item_len = p[0], config_item = p[1];
  75. if (item_len < 2 || end - p < item_len || config_item > 4)
  76. return AVERROR_INVALIDDATA;
  77. switch (config_item) {
  78. case 0: /* end of config block */
  79. return p - buf + item_len;
  80. case 1: /* stream without extradata */
  81. /* FIXME: set default qdm->block_size */
  82. break;
  83. case 2: /**< subpackets per block */
  84. if (item_len < 3)
  85. return AVERROR_INVALIDDATA;
  86. qdm->subpkts_per_block = p[2];
  87. break;
  88. case 3: /* superblock type */
  89. if (item_len < 4)
  90. return AVERROR_INVALIDDATA;
  91. qdm->block_type = AV_RB16(p + 2);
  92. break;
  93. case 4: /* stream with extradata */
  94. if (item_len < 30)
  95. return AVERROR_INVALIDDATA;
  96. av_freep(&st->codec->extradata);
  97. st->codec->extradata_size = 26 + item_len;
  98. if (!(st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE))) {
  99. st->codec->extradata_size = 0;
  100. return AVERROR(ENOMEM);
  101. }
  102. AV_WB32(st->codec->extradata, 12);
  103. memcpy(st->codec->extradata + 4, "frma", 4);
  104. memcpy(st->codec->extradata + 8, "QDM2", 4);
  105. AV_WB32(st->codec->extradata + 12, 6 + item_len);
  106. memcpy(st->codec->extradata + 16, "QDCA", 4);
  107. memcpy(st->codec->extradata + 20, p + 2, item_len - 2);
  108. AV_WB32(st->codec->extradata + 18 + item_len, 8);
  109. AV_WB32(st->codec->extradata + 22 + item_len, 0);
  110. qdm->block_size = AV_RB32(p + 26);
  111. break;
  112. }
  113. p += item_len;
  114. }
  115. return AVERROR(EAGAIN); /* not enough data */
  116. }
  117. /**
  118. * Parse a single subpacket. We store this subpacket in an intermediate
  119. * buffer (position depends on the ID (byte[0]). When called, at least
  120. * 4 bytes are available for reading (see qdm2_parse_packet()).
  121. *
  122. * Layout of a single subpacket (RTP packets commonly contain multiple
  123. * such subpackets) - length in bytes:
  124. * 1: ordering ID <- 0 .. 0x7F
  125. * 1: subpacket type <- 0 .. 0x7F; value & 0x80 means subpacket length = 2 bytes, else 1 byte
  126. * 1/2: subpacket length <- length of the data following the flags/length fields
  127. * if (subpacket type & 0x7F) == 0x7F
  128. * 1: subpacket type, higher bits
  129. * size: subpacket data
  130. *
  131. * The subpackets come in randomly, and should be encapsulated into 1
  132. * or more superblocks (containing qdm->subpkts_per_block subpackets
  133. * each) per RTP packet, in order of ascending "ordering ID", see
  134. * qdm2_restore_block().
  135. *
  136. * @return <0 on error, otherwise the number of bytes parsed from the
  137. * input buffer.
  138. */
  139. static int qdm2_parse_subpacket(PayloadContext *qdm, AVStream *st,
  140. const uint8_t *buf, const uint8_t *end)
  141. {
  142. const uint8_t *p = buf;
  143. unsigned int id, len, type, to_copy;
  144. /* parse header so we know the size of the header/data */
  145. id = *p++;
  146. type = *p++;
  147. if (type & 0x80) {
  148. len = AV_RB16(p);
  149. p += 2;
  150. type &= 0x7F;
  151. } else
  152. len = *p++;
  153. if (end - p < len + (type == 0x7F) || id >= 0x80)
  154. return AVERROR_INVALIDDATA;
  155. if (type == 0x7F)
  156. type |= *p++ << 8;
  157. /* copy data into a temporary buffer */
  158. to_copy = FFMIN(len + (p - &buf[1]), 0x800 - qdm->len[id]);
  159. memcpy(&qdm->buf[id][qdm->len[id]], buf + 1, to_copy);
  160. qdm->len[id] += to_copy;
  161. return p + len - buf;
  162. }
  163. /**
  164. * Add a superblock header around a set of subpackets.
  165. *
  166. * @return <0 on error, else 0.
  167. */
  168. static int qdm2_restore_block(PayloadContext *qdm, AVStream *st, AVPacket *pkt)
  169. {
  170. int to_copy, n, res, include_csum;
  171. uint8_t *p, *csum_pos = NULL;
  172. /* create packet to hold subpkts into a superblock */
  173. assert(qdm->cache > 0);
  174. for (n = 0; n < 0x80; n++)
  175. if (qdm->len[n] > 0)
  176. break;
  177. assert(n < 0x80);
  178. if ((res = av_new_packet(pkt, qdm->block_size)) < 0)
  179. return res;
  180. memset(pkt->data, 0, pkt->size);
  181. pkt->stream_index = st->index;
  182. p = pkt->data;
  183. /* superblock header */
  184. if (qdm->len[n] > 0xff) {
  185. *p++ = qdm->block_type | 0x80;
  186. AV_WB16(p, qdm->len[n]);
  187. p += 2;
  188. } else {
  189. *p++ = qdm->block_type;
  190. *p++ = qdm->len[n];
  191. }
  192. if ((include_csum = (qdm->block_type == 2 || qdm->block_type == 4))) {
  193. csum_pos = p;
  194. p += 2;
  195. }
  196. /* subpacket data */
  197. to_copy = FFMIN(qdm->len[n], pkt->size - (p - pkt->data));
  198. memcpy(p, qdm->buf[n], to_copy);
  199. qdm->len[n] = 0;
  200. /* checksum header */
  201. if (include_csum) {
  202. unsigned int total = 0;
  203. uint8_t *q;
  204. for (q = pkt->data; q < &pkt->data[qdm->block_size]; q++)
  205. total += *q;
  206. AV_WB16(csum_pos, (uint16_t) total);
  207. }
  208. return 0;
  209. }
  210. /** return 0 on packet, no more left, 1 on packet, -1 on partial packet... */
  211. static int qdm2_parse_packet(AVFormatContext *s, PayloadContext *qdm,
  212. AVStream *st, AVPacket *pkt,
  213. uint32_t *timestamp,
  214. const uint8_t *buf, int len, int flags)
  215. {
  216. int res = AVERROR_INVALIDDATA, n;
  217. const uint8_t *end = buf + len, *p = buf;
  218. if (len > 0) {
  219. if (len < 2)
  220. return AVERROR_INVALIDDATA;
  221. /* configuration block */
  222. if (*p == 0xff) {
  223. if (qdm->n_pkts > 0) {
  224. av_log(s, AV_LOG_WARNING,
  225. "Out of sequence config - dropping queue\n");
  226. qdm->n_pkts = 0;
  227. memset(qdm->len, 0, sizeof(qdm->len));
  228. }
  229. if ((res = qdm2_parse_config(qdm, st, ++p, end)) < 0)
  230. return res;
  231. p += res;
  232. /* We set codec_id to CODEC_ID_NONE initially to
  233. * delay decoder initialization since extradata is
  234. * carried within the RTP stream, not SDP. Here,
  235. * by setting codec_id to CODEC_ID_QDM2, we are signalling
  236. * to the decoder that it is OK to initialize. */
  237. st->codec->codec_id = CODEC_ID_QDM2;
  238. }
  239. if (st->codec->codec_id == CODEC_ID_NONE)
  240. return AVERROR(EAGAIN);
  241. /* subpackets */
  242. while (end - p >= 4) {
  243. if ((res = qdm2_parse_subpacket(qdm, st, p, end)) < 0)
  244. return res;
  245. p += res;
  246. }
  247. qdm->timestamp = *timestamp;
  248. if (++qdm->n_pkts < qdm->subpkts_per_block)
  249. return AVERROR(EAGAIN);
  250. qdm->cache = 0;
  251. for (n = 0; n < 0x80; n++)
  252. if (qdm->len[n] > 0)
  253. qdm->cache++;
  254. }
  255. /* output the subpackets into freshly created superblock structures */
  256. if (!qdm->cache || (res = qdm2_restore_block(qdm, st, pkt)) < 0)
  257. return res;
  258. if (--qdm->cache == 0)
  259. qdm->n_pkts = 0;
  260. *timestamp = qdm->timestamp;
  261. qdm->timestamp = RTP_NOTS_VALUE;
  262. return (qdm->cache > 0) ? 1 : 0;
  263. }
  264. static PayloadContext *qdm2_extradata_new(void)
  265. {
  266. return av_mallocz(sizeof(PayloadContext));
  267. }
  268. static void qdm2_extradata_free(PayloadContext *qdm)
  269. {
  270. av_free(qdm);
  271. }
  272. RTPDynamicProtocolHandler ff_qdm2_dynamic_handler = {
  273. .enc_name = "X-QDM",
  274. .codec_type = AVMEDIA_TYPE_AUDIO,
  275. .codec_id = CODEC_ID_NONE,
  276. .alloc = qdm2_extradata_new,
  277. .free = qdm2_extradata_free,
  278. .parse_packet = qdm2_parse_packet,
  279. };