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.

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