You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

320 lines
11KB

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