rtpdec_qdm2.c
Go to the documentation of this file.
1 /*
2  * QDesign Music 2 (QDM2) payload for RTP
3  * Copyright (c) 2010 Ronald S. Bultje
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
28 #include <string.h>
29 #include "libavutil/intreadwrite.h"
30 #include "libavcodec/avcodec.h"
31 #include "rtp.h"
32 #include "rtpdec.h"
33 #include "rtpdec_formats.h"
34 
35 struct PayloadContext {
38  int block_type;
39  int block_size;
41 
42 
45  uint16_t len[0x80];
46  uint8_t buf[0x80][0x800];
47 
48  unsigned int cache;
49  unsigned int n_pkts;
50  uint32_t timestamp;
51 
52 };
53 
76  const uint8_t *buf, const uint8_t *end)
77 {
78  const uint8_t *p = buf;
79 
80  while (end - p >= 2) {
81  unsigned int item_len = p[0], config_item = p[1];
82 
83  if (item_len < 2 || end - p < item_len || config_item > 4)
84  return AVERROR_INVALIDDATA;
85 
86  switch (config_item) {
87  case 0: /* end of config block */
88  return p - buf + item_len;
89  case 1: /* stream without extradata */
90  /* FIXME: set default qdm->block_size */
91  break;
92  case 2:
93  if (item_len < 3)
94  return AVERROR_INVALIDDATA;
95  qdm->subpkts_per_block = p[2];
96  break;
97  case 3: /* superblock type */
98  if (item_len < 4)
99  return AVERROR_INVALIDDATA;
100  qdm->block_type = AV_RB16(p + 2);
101  break;
102  case 4: /* stream with extradata */
103  if (item_len < 30)
104  return AVERROR_INVALIDDATA;
105  av_freep(&st->codec->extradata);
106  st->codec->extradata_size = 26 + item_len;
108  st->codec->extradata_size = 0;
109  return AVERROR(ENOMEM);
110  }
111  AV_WB32(st->codec->extradata, 12);
112  memcpy(st->codec->extradata + 4, "frma", 4);
113  memcpy(st->codec->extradata + 8, "QDM2", 4);
114  AV_WB32(st->codec->extradata + 12, 6 + item_len);
115  memcpy(st->codec->extradata + 16, "QDCA", 4);
116  memcpy(st->codec->extradata + 20, p + 2, item_len - 2);
117  AV_WB32(st->codec->extradata + 18 + item_len, 8);
118  AV_WB32(st->codec->extradata + 22 + item_len, 0);
119 
120  qdm->block_size = AV_RB32(p + 26);
121  break;
122  }
123 
124  p += item_len;
125  }
126 
127  return AVERROR(EAGAIN); /* not enough data */
128 }
129 
153  const uint8_t *buf, const uint8_t *end)
154 {
155  const uint8_t *p = buf;
156  unsigned int id, len, type, to_copy;
157 
158  /* parse header so we know the size of the header/data */
159  id = *p++;
160  type = *p++;
161  if (type & 0x80) {
162  len = AV_RB16(p);
163  p += 2;
164  type &= 0x7F;
165  } else
166  len = *p++;
167 
168  if (end - p < len + (type == 0x7F) || id >= 0x80)
169  return AVERROR_INVALIDDATA;
170  if (type == 0x7F)
171  type |= *p++ << 8;
172 
173  /* copy data into a temporary buffer */
174  to_copy = FFMIN(len + (p - &buf[1]), 0x800 - qdm->len[id]);
175  memcpy(&qdm->buf[id][qdm->len[id]], buf + 1, to_copy);
176  qdm->len[id] += to_copy;
177 
178  return p + len - buf;
179 }
180 
187 {
188  int to_copy, n, res, include_csum;
189  uint8_t *p, *csum_pos = NULL;
190 
191  /* create packet to hold subpkts into a superblock */
192  assert(qdm->cache > 0);
193  for (n = 0; n < 0x80; n++)
194  if (qdm->len[n] > 0)
195  break;
196  assert(n < 0x80);
197 
198  if ((res = av_new_packet(pkt, qdm->block_size)) < 0)
199  return res;
200  memset(pkt->data, 0, pkt->size);
201  pkt->stream_index = st->index;
202  p = pkt->data;
203 
204  /* superblock header */
205  if (qdm->len[n] > 0xff) {
206  *p++ = qdm->block_type | 0x80;
207  AV_WB16(p, qdm->len[n]);
208  p += 2;
209  } else {
210  *p++ = qdm->block_type;
211  *p++ = qdm->len[n];
212  }
213  if ((include_csum = (qdm->block_type == 2 || qdm->block_type == 4))) {
214  csum_pos = p;
215  p += 2;
216  }
217 
218  /* subpacket data */
219  to_copy = FFMIN(qdm->len[n], pkt->size - (p - pkt->data));
220  memcpy(p, qdm->buf[n], to_copy);
221  qdm->len[n] = 0;
222 
223  /* checksum header */
224  if (include_csum) {
225  unsigned int total = 0;
226  uint8_t *q;
227 
228  for (q = pkt->data; q < &pkt->data[qdm->block_size]; q++)
229  total += *q;
230  AV_WB16(csum_pos, (uint16_t) total);
231  }
232 
233  return 0;
234 }
235 
238  AVStream *st, AVPacket *pkt,
239  uint32_t *timestamp,
240  const uint8_t *buf, int len, int flags)
241 {
242  int res = AVERROR_INVALIDDATA, n;
243  const uint8_t *end = buf + len, *p = buf;
244 
245  if (len > 0) {
246  if (len < 2)
247  return AVERROR_INVALIDDATA;
248 
249  /* configuration block */
250  if (*p == 0xff) {
251  if (qdm->n_pkts > 0) {
253  "Out of sequence config - dropping queue\n");
254  qdm->n_pkts = 0;
255  memset(qdm->len, 0, sizeof(qdm->len));
256  }
257 
258  if ((res = qdm2_parse_config(qdm, st, ++p, end)) < 0)
259  return res;
260  p += res;
261 
262  /* We set codec_id to CODEC_ID_NONE initially to
263  * delay decoder initialization since extradata is
264  * carried within the RTP stream, not SDP. Here,
265  * by setting codec_id to CODEC_ID_QDM2, we are signalling
266  * to the decoder that it is OK to initialize. */
268  }
269  if (st->codec->codec_id == CODEC_ID_NONE)
270  return AVERROR(EAGAIN);
271 
272  /* subpackets */
273  while (end - p >= 4) {
274  if ((res = qdm2_parse_subpacket(qdm, st, p, end)) < 0)
275  return res;
276  p += res;
277  }
278 
279  qdm->timestamp = *timestamp;
280  if (++qdm->n_pkts < qdm->subpkts_per_block)
281  return AVERROR(EAGAIN);
282  qdm->cache = 0;
283  for (n = 0; n < 0x80; n++)
284  if (qdm->len[n] > 0)
285  qdm->cache++;
286  }
287 
288  /* output the subpackets into freshly created superblock structures */
289  if (!qdm->cache || (res = qdm2_restore_block(qdm, st, pkt)) < 0)
290  return res;
291  if (--qdm->cache == 0)
292  qdm->n_pkts = 0;
293 
294  *timestamp = qdm->timestamp;
295  qdm->timestamp = RTP_NOTS_VALUE;
296 
297  return (qdm->cache > 0) ? 1 : 0;
298 }
299 
301 {
302  return av_mallocz(sizeof(PayloadContext));
303 }
304 
306 {
307  av_free(qdm);
308 }
309 
311  .enc_name = "X-QDM",
312  .codec_type = AVMEDIA_TYPE_AUDIO,
313  .codec_id = CODEC_ID_NONE,
314  .alloc = qdm2_extradata_new,
315  .free = qdm2_extradata_free,
316  .parse_packet = qdm2_parse_packet,
317 };