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.

648 lines
19KB

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