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.

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