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.

579 lines
18KB

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