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.

556 lines
17KB

  1. /*
  2. * Shorten decoder
  3. * Copyright (c) 2005 Jeff Muizelaar
  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. /**
  22. * @file
  23. * Shorten decoder
  24. * @author Jeff Muizelaar
  25. *
  26. */
  27. #include <limits.h>
  28. #include "avcodec.h"
  29. #include "get_bits.h"
  30. #include "golomb.h"
  31. #define MAX_CHANNELS 8
  32. #define MAX_BLOCKSIZE 65535
  33. #define OUT_BUFFER_SIZE 16384
  34. #define ULONGSIZE 2
  35. #define WAVE_FORMAT_PCM 0x0001
  36. #define DEFAULT_BLOCK_SIZE 256
  37. #define TYPESIZE 4
  38. #define CHANSIZE 0
  39. #define LPCQSIZE 2
  40. #define ENERGYSIZE 3
  41. #define BITSHIFTSIZE 2
  42. #define TYPE_S16HL 3
  43. #define TYPE_S16LH 5
  44. #define NWRAP 3
  45. #define NSKIPSIZE 1
  46. #define LPCQUANT 5
  47. #define V2LPCQOFFSET (1 << LPCQUANT)
  48. #define FNSIZE 2
  49. #define FN_DIFF0 0
  50. #define FN_DIFF1 1
  51. #define FN_DIFF2 2
  52. #define FN_DIFF3 3
  53. #define FN_QUIT 4
  54. #define FN_BLOCKSIZE 5
  55. #define FN_BITSHIFT 6
  56. #define FN_QLPC 7
  57. #define FN_ZERO 8
  58. #define FN_VERBATIM 9
  59. #define VERBATIM_CKSIZE_SIZE 5
  60. #define VERBATIM_BYTE_SIZE 8
  61. #define CANONICAL_HEADER_SIZE 44
  62. typedef struct ShortenContext {
  63. AVCodecContext *avctx;
  64. GetBitContext gb;
  65. int min_framesize, max_framesize;
  66. int channels;
  67. int32_t *decoded[MAX_CHANNELS];
  68. int32_t *offset[MAX_CHANNELS];
  69. int *coeffs;
  70. uint8_t *bitstream;
  71. int bitstream_size;
  72. int bitstream_index;
  73. unsigned int allocated_bitstream_size;
  74. int header_size;
  75. uint8_t header[OUT_BUFFER_SIZE];
  76. int version;
  77. int cur_chan;
  78. int bitshift;
  79. int nmean;
  80. int internal_ftype;
  81. int nwrap;
  82. int blocksize;
  83. int bitindex;
  84. int32_t lpcqoffset;
  85. } ShortenContext;
  86. static av_cold int shorten_decode_init(AVCodecContext * avctx)
  87. {
  88. ShortenContext *s = avctx->priv_data;
  89. s->avctx = avctx;
  90. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  91. return 0;
  92. }
  93. static int allocate_buffers(ShortenContext *s)
  94. {
  95. int i, chan;
  96. int *coeffs;
  97. for (chan=0; chan<s->channels; chan++) {
  98. if(FFMAX(1, s->nmean) >= UINT_MAX/sizeof(int32_t)){
  99. av_log(s->avctx, AV_LOG_ERROR, "nmean too large\n");
  100. return -1;
  101. }
  102. if(s->blocksize + s->nwrap >= UINT_MAX/sizeof(int32_t) || s->blocksize + s->nwrap <= (unsigned)s->nwrap){
  103. av_log(s->avctx, AV_LOG_ERROR, "s->blocksize + s->nwrap too large\n");
  104. return -1;
  105. }
  106. s->offset[chan] = av_realloc(s->offset[chan], sizeof(int32_t)*FFMAX(1, s->nmean));
  107. s->decoded[chan] = av_realloc(s->decoded[chan], sizeof(int32_t)*(s->blocksize + s->nwrap));
  108. for (i=0; i<s->nwrap; i++)
  109. s->decoded[chan][i] = 0;
  110. s->decoded[chan] += s->nwrap;
  111. }
  112. coeffs = av_realloc(s->coeffs, s->nwrap * sizeof(*s->coeffs));
  113. if (!coeffs)
  114. return AVERROR(ENOMEM);
  115. s->coeffs = coeffs;
  116. return 0;
  117. }
  118. static inline unsigned int get_uint(ShortenContext *s, int k)
  119. {
  120. if (s->version != 0)
  121. k = get_ur_golomb_shorten(&s->gb, ULONGSIZE);
  122. return get_ur_golomb_shorten(&s->gb, k);
  123. }
  124. static void fix_bitshift(ShortenContext *s, int32_t *buffer)
  125. {
  126. int i;
  127. if (s->bitshift != 0)
  128. for (i = 0; i < s->blocksize; i++)
  129. buffer[i] <<= s->bitshift;
  130. }
  131. static void init_offset(ShortenContext *s)
  132. {
  133. int32_t mean = 0;
  134. int chan, i;
  135. int nblock = FFMAX(1, s->nmean);
  136. /* initialise offset */
  137. switch (s->internal_ftype)
  138. {
  139. case TYPE_S16HL:
  140. case TYPE_S16LH:
  141. mean = 0;
  142. break;
  143. default:
  144. av_log(s->avctx, AV_LOG_ERROR, "unknown audio type");
  145. abort();
  146. }
  147. for (chan = 0; chan < s->channels; chan++)
  148. for (i = 0; i < nblock; i++)
  149. s->offset[chan][i] = mean;
  150. }
  151. static inline int get_le32(GetBitContext *gb)
  152. {
  153. return av_bswap32(get_bits_long(gb, 32));
  154. }
  155. static inline short get_le16(GetBitContext *gb)
  156. {
  157. return av_bswap16(get_bits_long(gb, 16));
  158. }
  159. static int decode_wave_header(AVCodecContext *avctx, uint8_t *header, int header_size)
  160. {
  161. GetBitContext hb;
  162. int len;
  163. short wave_format;
  164. init_get_bits(&hb, header, header_size*8);
  165. if (get_le32(&hb) != MKTAG('R','I','F','F')) {
  166. av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n");
  167. return -1;
  168. }
  169. skip_bits_long(&hb, 32); /* chunk_size */
  170. if (get_le32(&hb) != MKTAG('W','A','V','E')) {
  171. av_log(avctx, AV_LOG_ERROR, "missing WAVE tag\n");
  172. return -1;
  173. }
  174. while (get_le32(&hb) != MKTAG('f','m','t',' ')) {
  175. len = get_le32(&hb);
  176. skip_bits(&hb, 8*len);
  177. }
  178. len = get_le32(&hb);
  179. if (len < 16) {
  180. av_log(avctx, AV_LOG_ERROR, "fmt chunk was too short\n");
  181. return -1;
  182. }
  183. wave_format = get_le16(&hb);
  184. switch (wave_format) {
  185. case WAVE_FORMAT_PCM:
  186. break;
  187. default:
  188. av_log(avctx, AV_LOG_ERROR, "unsupported wave format\n");
  189. return -1;
  190. }
  191. avctx->channels = get_le16(&hb);
  192. avctx->sample_rate = get_le32(&hb);
  193. avctx->bit_rate = get_le32(&hb) * 8;
  194. avctx->block_align = get_le16(&hb);
  195. avctx->bits_per_coded_sample = get_le16(&hb);
  196. if (avctx->bits_per_coded_sample != 16) {
  197. av_log(avctx, AV_LOG_ERROR, "unsupported number of bits per sample\n");
  198. return -1;
  199. }
  200. len -= 16;
  201. if (len > 0)
  202. av_log(avctx, AV_LOG_INFO, "%d header bytes unparsed\n", len);
  203. return 0;
  204. }
  205. static int16_t * interleave_buffer(int16_t *samples, int nchan, int blocksize, int32_t **buffer) {
  206. int i, chan;
  207. for (i=0; i<blocksize; i++)
  208. for (chan=0; chan < nchan; chan++)
  209. *samples++ = FFMIN(buffer[chan][i], 32768);
  210. return samples;
  211. }
  212. static void decode_subframe_lpc(ShortenContext *s, int channel, int residual_size, int pred_order)
  213. {
  214. int sum, i, j;
  215. int *coeffs = s->coeffs;
  216. for (i=0; i<pred_order; i++)
  217. coeffs[i] = get_sr_golomb_shorten(&s->gb, LPCQUANT);
  218. for (i=0; i < s->blocksize; i++) {
  219. sum = s->lpcqoffset;
  220. for (j=0; j<pred_order; j++)
  221. sum += coeffs[j] * s->decoded[channel][i-j-1];
  222. s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + (sum >> LPCQUANT);
  223. }
  224. }
  225. static int read_header(ShortenContext *s)
  226. {
  227. int i;
  228. int maxnlpc = 0;
  229. /* shorten signature */
  230. if (get_bits_long(&s->gb, 32) != AV_RB32("ajkg")) {
  231. av_log(s->avctx, AV_LOG_ERROR, "missing shorten magic 'ajkg'\n");
  232. return -1;
  233. }
  234. s->lpcqoffset = 0;
  235. s->blocksize = DEFAULT_BLOCK_SIZE;
  236. s->channels = 1;
  237. s->nmean = -1;
  238. s->version = get_bits(&s->gb, 8);
  239. s->internal_ftype = get_uint(s, TYPESIZE);
  240. s->channels = get_uint(s, CHANSIZE);
  241. if (s->channels > MAX_CHANNELS) {
  242. av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
  243. return -1;
  244. }
  245. /* get blocksize if version > 0 */
  246. if (s->version > 0) {
  247. int skip_bytes;
  248. s->blocksize = get_uint(s, av_log2(DEFAULT_BLOCK_SIZE));
  249. maxnlpc = get_uint(s, LPCQSIZE);
  250. s->nmean = get_uint(s, 0);
  251. skip_bytes = get_uint(s, NSKIPSIZE);
  252. for (i=0; i<skip_bytes; i++) {
  253. skip_bits(&s->gb, 8);
  254. }
  255. }
  256. s->nwrap = FFMAX(NWRAP, maxnlpc);
  257. if (allocate_buffers(s))
  258. return -1;
  259. init_offset(s);
  260. if (s->version > 1)
  261. s->lpcqoffset = V2LPCQOFFSET;
  262. if (get_ur_golomb_shorten(&s->gb, FNSIZE) != FN_VERBATIM) {
  263. av_log(s->avctx, AV_LOG_ERROR, "missing verbatim section at beginning of stream\n");
  264. return -1;
  265. }
  266. s->header_size = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
  267. if (s->header_size >= OUT_BUFFER_SIZE || s->header_size < CANONICAL_HEADER_SIZE) {
  268. av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n", s->header_size);
  269. return -1;
  270. }
  271. for (i=0; i<s->header_size; i++)
  272. s->header[i] = (char)get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
  273. if (decode_wave_header(s->avctx, s->header, s->header_size) < 0)
  274. return -1;
  275. s->cur_chan = 0;
  276. s->bitshift = 0;
  277. return 0;
  278. }
  279. static int shorten_decode_frame(AVCodecContext *avctx,
  280. void *data, int *data_size,
  281. AVPacket *avpkt)
  282. {
  283. const uint8_t *buf = avpkt->data;
  284. int buf_size = avpkt->size;
  285. ShortenContext *s = avctx->priv_data;
  286. int i, input_buf_size = 0;
  287. int16_t *samples = data;
  288. if(s->max_framesize == 0){
  289. s->max_framesize= 1024; // should hopefully be enough for the first header
  290. s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->max_framesize);
  291. }
  292. if(1 && s->max_framesize){//FIXME truncated
  293. buf_size= FFMIN(buf_size, s->max_framesize - s->bitstream_size);
  294. input_buf_size= buf_size;
  295. if(s->bitstream_index + s->bitstream_size + buf_size > s->allocated_bitstream_size){
  296. // printf("memmove\n");
  297. memmove(s->bitstream, &s->bitstream[s->bitstream_index], s->bitstream_size);
  298. s->bitstream_index=0;
  299. }
  300. memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf, buf_size);
  301. buf= &s->bitstream[s->bitstream_index];
  302. buf_size += s->bitstream_size;
  303. s->bitstream_size= buf_size;
  304. if(buf_size < s->max_framesize){
  305. *data_size = 0;
  306. return input_buf_size;
  307. }
  308. }
  309. init_get_bits(&s->gb, buf, buf_size*8);
  310. skip_bits(&s->gb, s->bitindex);
  311. if (!s->blocksize)
  312. {
  313. int ret;
  314. if ((ret = read_header(s)) < 0)
  315. return ret;
  316. }
  317. else
  318. {
  319. int cmd;
  320. int len;
  321. cmd = get_ur_golomb_shorten(&s->gb, FNSIZE);
  322. switch (cmd) {
  323. case FN_ZERO:
  324. case FN_DIFF0:
  325. case FN_DIFF1:
  326. case FN_DIFF2:
  327. case FN_DIFF3:
  328. case FN_QLPC:
  329. {
  330. int residual_size = 0;
  331. int channel = s->cur_chan;
  332. int32_t coffset;
  333. if (cmd != FN_ZERO) {
  334. residual_size = get_ur_golomb_shorten(&s->gb, ENERGYSIZE);
  335. /* this is a hack as version 0 differed in defintion of get_sr_golomb_shorten */
  336. if (s->version == 0)
  337. residual_size--;
  338. }
  339. if (s->nmean == 0)
  340. coffset = s->offset[channel][0];
  341. else {
  342. int32_t sum = (s->version < 2) ? 0 : s->nmean / 2;
  343. for (i=0; i<s->nmean; i++)
  344. sum += s->offset[channel][i];
  345. coffset = sum / s->nmean;
  346. if (s->version >= 2)
  347. coffset >>= FFMIN(1, s->bitshift);
  348. }
  349. switch (cmd) {
  350. case FN_ZERO:
  351. for (i=0; i<s->blocksize; i++)
  352. s->decoded[channel][i] = 0;
  353. break;
  354. case FN_DIFF0:
  355. for (i=0; i<s->blocksize; i++)
  356. s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + coffset;
  357. break;
  358. case FN_DIFF1:
  359. for (i=0; i<s->blocksize; i++)
  360. s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + s->decoded[channel][i - 1];
  361. break;
  362. case FN_DIFF2:
  363. for (i=0; i<s->blocksize; i++)
  364. s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + 2*s->decoded[channel][i-1]
  365. - s->decoded[channel][i-2];
  366. break;
  367. case FN_DIFF3:
  368. for (i=0; i<s->blocksize; i++)
  369. s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + 3*s->decoded[channel][i-1]
  370. - 3*s->decoded[channel][i-2]
  371. + s->decoded[channel][i-3];
  372. break;
  373. case FN_QLPC:
  374. {
  375. int pred_order = get_ur_golomb_shorten(&s->gb, LPCQSIZE);
  376. if (pred_order > s->nwrap) {
  377. av_log(avctx, AV_LOG_ERROR,
  378. "invalid pred_order %d\n",
  379. pred_order);
  380. return -1;
  381. }
  382. for (i=0; i<pred_order; i++)
  383. s->decoded[channel][i - pred_order] -= coffset;
  384. decode_subframe_lpc(s, channel, residual_size, pred_order);
  385. if (coffset != 0)
  386. for (i=0; i < s->blocksize; i++)
  387. s->decoded[channel][i] += coffset;
  388. }
  389. }
  390. if (s->nmean > 0) {
  391. int32_t sum = (s->version < 2) ? 0 : s->blocksize / 2;
  392. for (i=0; i<s->blocksize; i++)
  393. sum += s->decoded[channel][i];
  394. for (i=1; i<s->nmean; i++)
  395. s->offset[channel][i-1] = s->offset[channel][i];
  396. if (s->version < 2)
  397. s->offset[channel][s->nmean - 1] = sum / s->blocksize;
  398. else
  399. s->offset[channel][s->nmean - 1] = (sum / s->blocksize) << s->bitshift;
  400. }
  401. for (i=-s->nwrap; i<0; i++)
  402. s->decoded[channel][i] = s->decoded[channel][i + s->blocksize];
  403. fix_bitshift(s, s->decoded[channel]);
  404. s->cur_chan++;
  405. if (s->cur_chan == s->channels) {
  406. samples = interleave_buffer(samples, s->channels, s->blocksize, s->decoded);
  407. s->cur_chan = 0;
  408. goto frame_done;
  409. }
  410. }
  411. break;
  412. case FN_VERBATIM:
  413. len = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
  414. while (len--) {
  415. get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
  416. }
  417. break;
  418. case FN_BITSHIFT:
  419. s->bitshift = get_ur_golomb_shorten(&s->gb, BITSHIFTSIZE);
  420. break;
  421. case FN_BLOCKSIZE: {
  422. int blocksize = get_uint(s, av_log2(s->blocksize));
  423. if (blocksize > s->blocksize) {
  424. av_log(avctx, AV_LOG_ERROR, "Increasing block size is not supported\n");
  425. return AVERROR_PATCHWELCOME;
  426. }
  427. s->blocksize = blocksize;
  428. break;
  429. }
  430. case FN_QUIT:
  431. *data_size = 0;
  432. return buf_size;
  433. default:
  434. av_log(avctx, AV_LOG_ERROR, "unknown shorten function %d\n", cmd);
  435. return -1;
  436. }
  437. }
  438. frame_done:
  439. *data_size = (int8_t *)samples - (int8_t *)data;
  440. // s->last_blocksize = s->blocksize;
  441. s->bitindex = get_bits_count(&s->gb) - 8*((get_bits_count(&s->gb))/8);
  442. i= (get_bits_count(&s->gb))/8;
  443. if (i > buf_size) {
  444. av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
  445. s->bitstream_size=0;
  446. s->bitstream_index=0;
  447. return -1;
  448. }
  449. if (s->bitstream_size) {
  450. s->bitstream_index += i;
  451. s->bitstream_size -= i;
  452. return input_buf_size;
  453. } else
  454. return i;
  455. }
  456. static av_cold int shorten_decode_close(AVCodecContext *avctx)
  457. {
  458. ShortenContext *s = avctx->priv_data;
  459. int i;
  460. for (i = 0; i < s->channels; i++) {
  461. s->decoded[i] -= s->nwrap;
  462. av_freep(&s->decoded[i]);
  463. av_freep(&s->offset[i]);
  464. }
  465. av_freep(&s->bitstream);
  466. av_freep(&s->coeffs);
  467. return 0;
  468. }
  469. AVCodec ff_shorten_decoder = {
  470. .name = "shorten",
  471. .type = AVMEDIA_TYPE_AUDIO,
  472. .id = CODEC_ID_SHORTEN,
  473. .priv_data_size = sizeof(ShortenContext),
  474. .init = shorten_decode_init,
  475. .close = shorten_decode_close,
  476. .decode = shorten_decode_frame,
  477. .long_name= NULL_IF_CONFIG_SMALL("Shorten"),
  478. };