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.

630 lines
19KB

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