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.

601 lines
18KB

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