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.

531 lines
16KB

  1. /*
  2. * Shorten decoder
  3. * Copyright (c) 2005 Jeff Muizelaar
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. /**
  20. * @file shorten.c
  21. * Shorten decoder
  22. * @author Jeff Muizelaar
  23. *
  24. */
  25. #define DEBUG
  26. #include <limits.h>
  27. #include "avcodec.h"
  28. #include "bitstream.h"
  29. #include "golomb.h"
  30. #define MAX_CHANNELS 8
  31. #define MAX_BLOCKSIZE 65535
  32. #define OUT_BUFFER_SIZE 16384
  33. #define ULONGSIZE 2
  34. #define WAVE_FORMAT_PCM 0x0001
  35. #define DEFAULT_BLOCK_SIZE 256
  36. #define TYPESIZE 4
  37. #define CHANSIZE 0
  38. #define LPCQSIZE 2
  39. #define ENERGYSIZE 3
  40. #define BITSHIFTSIZE 2
  41. #define TYPE_S16HL 3
  42. #define TYPE_S16LH 5
  43. #define NWRAP 3
  44. #define NSKIPSIZE 1
  45. #define LPCQUANT 5
  46. #define V2LPCQOFFSET (1 << LPCQUANT)
  47. #define FNSIZE 2
  48. #define FN_DIFF0 0
  49. #define FN_DIFF1 1
  50. #define FN_DIFF2 2
  51. #define FN_DIFF3 3
  52. #define FN_QUIT 4
  53. #define FN_BLOCKSIZE 5
  54. #define FN_BITSHIFT 6
  55. #define FN_QLPC 7
  56. #define FN_ZERO 8
  57. #define FN_VERBATIM 9
  58. #define VERBATIM_CKSIZE_SIZE 5
  59. #define VERBATIM_BYTE_SIZE 8
  60. #define CANONICAL_HEADER_SIZE 44
  61. typedef struct ShortenContext {
  62. AVCodecContext *avctx;
  63. GetBitContext gb;
  64. int min_framesize, max_framesize;
  65. int channels;
  66. int32_t *decoded[MAX_CHANNELS];
  67. int32_t *offset[MAX_CHANNELS];
  68. uint8_t *bitstream;
  69. int bitstream_size;
  70. int bitstream_index;
  71. unsigned int allocated_bitstream_size;
  72. int header_size;
  73. uint8_t header[OUT_BUFFER_SIZE];
  74. int version;
  75. int cur_chan;
  76. int bitshift;
  77. int nmean;
  78. int internal_ftype;
  79. int nwrap;
  80. int blocksize;
  81. int bitindex;
  82. int32_t lpcqoffset;
  83. } ShortenContext;
  84. static int shorten_decode_init(AVCodecContext * avctx)
  85. {
  86. ShortenContext *s = avctx->priv_data;
  87. s->avctx = avctx;
  88. return 0;
  89. }
  90. static int allocate_buffers(ShortenContext *s)
  91. {
  92. int i, chan;
  93. for (chan=0; chan<s->channels; chan++) {
  94. if(FFMAX(1, s->nmean) >= UINT_MAX/sizeof(int32_t)){
  95. av_log(s->avctx, AV_LOG_ERROR, "nmean too large\n");
  96. return -1;
  97. }
  98. if(s->blocksize + s->nwrap >= UINT_MAX/sizeof(int32_t) || s->blocksize + s->nwrap <= (unsigned)s->nwrap){
  99. av_log(s->avctx, AV_LOG_ERROR, "s->blocksize + s->nwrap too large\n");
  100. return -1;
  101. }
  102. s->offset[chan] = av_realloc(s->offset[chan], sizeof(int32_t)*FFMAX(1, s->nmean));
  103. s->decoded[chan] = av_realloc(s->decoded[chan], sizeof(int32_t)*(s->blocksize + s->nwrap));
  104. for (i=0; i<s->nwrap; i++)
  105. s->decoded[chan][i] = 0;
  106. s->decoded[chan] += s->nwrap;
  107. }
  108. return 0;
  109. }
  110. static inline unsigned int get_uint(ShortenContext *s, int k)
  111. {
  112. if (s->version != 0)
  113. k = get_ur_golomb_shorten(&s->gb, ULONGSIZE);
  114. return get_ur_golomb_shorten(&s->gb, k);
  115. }
  116. static void fix_bitshift(ShortenContext *s, int32_t *buffer)
  117. {
  118. int i;
  119. if (s->bitshift != 0)
  120. for (i = 0; i < s->blocksize; i++)
  121. buffer[s->nwrap + i] <<= s->bitshift;
  122. }
  123. static void init_offset(ShortenContext *s)
  124. {
  125. int32_t mean = 0;
  126. int chan, i;
  127. int nblock = FFMAX(1, s->nmean);
  128. /* initialise offset */
  129. switch (s->internal_ftype)
  130. {
  131. case TYPE_S16HL:
  132. case TYPE_S16LH:
  133. mean = 0;
  134. break;
  135. default:
  136. av_log(s->avctx, AV_LOG_ERROR, "unknown audio type");
  137. abort();
  138. }
  139. for (chan = 0; chan < s->channels; chan++)
  140. for (i = 0; i < nblock; i++)
  141. s->offset[chan][i] = mean;
  142. }
  143. static int inline get_le32(GetBitContext *gb)
  144. {
  145. return bswap_32(get_bits_long(gb, 32));
  146. }
  147. static short inline get_le16(GetBitContext *gb)
  148. {
  149. return bswap_16(get_bits_long(gb, 16));
  150. }
  151. static int decode_wave_header(AVCodecContext *avctx, uint8_t *header, int header_size)
  152. {
  153. GetBitContext hb;
  154. int len;
  155. int chunk_size;
  156. short wave_format;
  157. init_get_bits(&hb, header, header_size*8);
  158. if (get_le32(&hb) != MKTAG('R','I','F','F')) {
  159. av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n");
  160. return -1;
  161. }
  162. chunk_size = get_le32(&hb);
  163. if (get_le32(&hb) != MKTAG('W','A','V','E')) {
  164. av_log(avctx, AV_LOG_ERROR, "missing WAVE tag\n");
  165. return -1;
  166. }
  167. while (get_le32(&hb) != MKTAG('f','m','t',' ')) {
  168. len = get_le32(&hb);
  169. skip_bits(&hb, 8*len);
  170. }
  171. len = get_le32(&hb);
  172. if (len < 16) {
  173. av_log(avctx, AV_LOG_ERROR, "fmt chunk was too short\n");
  174. return -1;
  175. }
  176. wave_format = get_le16(&hb);
  177. switch (wave_format) {
  178. case WAVE_FORMAT_PCM:
  179. break;
  180. default:
  181. av_log(avctx, AV_LOG_ERROR, "unsupported wave format\n");
  182. return -1;
  183. }
  184. avctx->channels = get_le16(&hb);
  185. avctx->sample_rate = get_le32(&hb);
  186. avctx->bit_rate = get_le32(&hb) * 8;
  187. avctx->block_align = get_le16(&hb);
  188. avctx->bits_per_sample = get_le16(&hb);
  189. if (avctx->bits_per_sample != 16) {
  190. av_log(avctx, AV_LOG_ERROR, "unsupported number of bits per sample\n");
  191. return -1;
  192. }
  193. len -= 16;
  194. if (len > 0)
  195. av_log(avctx, AV_LOG_INFO, "%d header bytes unparsed\n", len);
  196. return 0;
  197. }
  198. static int16_t * interleave_buffer(int16_t *samples, int nchan, int blocksize, int32_t **buffer) {
  199. int i, chan;
  200. for (i=0; i<blocksize; i++)
  201. for (chan=0; chan < nchan; chan++)
  202. *samples++ = FFMIN(buffer[chan][i], 32768);
  203. return samples;
  204. }
  205. static void decode_subframe_lpc(ShortenContext *s, int channel, int residual_size, int pred_order)
  206. {
  207. int sum, i, j;
  208. int coeffs[pred_order];
  209. for (i=0; i<pred_order; i++)
  210. coeffs[i] = get_sr_golomb_shorten(&s->gb, LPCQUANT);
  211. for (i=0; i < s->blocksize; i++) {
  212. sum = s->lpcqoffset;
  213. for (j=0; j<pred_order; j++)
  214. sum += coeffs[j] * s->decoded[channel][i-j-1];
  215. s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + (sum >> LPCQUANT);
  216. }
  217. }
  218. static int shorten_decode_frame(AVCodecContext *avctx,
  219. void *data, int *data_size,
  220. uint8_t *buf, int buf_size)
  221. {
  222. ShortenContext *s = avctx->priv_data;
  223. int i, input_buf_size = 0;
  224. int16_t *samples = data;
  225. if(s->max_framesize == 0){
  226. s->max_framesize= 1024; // should hopefully be enough for the first header
  227. s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->max_framesize);
  228. }
  229. if(1 && s->max_framesize){//FIXME truncated
  230. buf_size= FFMIN(buf_size, s->max_framesize - s->bitstream_size);
  231. input_buf_size= buf_size;
  232. if(s->bitstream_index + s->bitstream_size + buf_size > s->allocated_bitstream_size){
  233. // printf("memmove\n");
  234. memmove(s->bitstream, &s->bitstream[s->bitstream_index], s->bitstream_size);
  235. s->bitstream_index=0;
  236. }
  237. memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf, buf_size);
  238. buf= &s->bitstream[s->bitstream_index];
  239. buf_size += s->bitstream_size;
  240. s->bitstream_size= buf_size;
  241. if(buf_size < s->max_framesize){
  242. //dprintf("wanna more data ... %d\n", buf_size);
  243. return input_buf_size;
  244. }
  245. }
  246. init_get_bits(&s->gb, buf, buf_size*8);
  247. get_bits(&s->gb, s->bitindex);
  248. if (!s->blocksize)
  249. {
  250. int maxnlpc = 0;
  251. /* shorten signature */
  252. if (get_bits_long(&s->gb, 32) != bswap_32(ff_get_fourcc("ajkg"))) {
  253. av_log(s->avctx, AV_LOG_ERROR, "missing shorten magic 'ajkg'\n");
  254. return -1;
  255. }
  256. s->lpcqoffset = 0;
  257. s->blocksize = DEFAULT_BLOCK_SIZE;
  258. s->channels = 1;
  259. s->nmean = -1;
  260. s->version = get_bits(&s->gb, 8);
  261. s->internal_ftype = get_uint(s, TYPESIZE);
  262. s->channels = get_uint(s, CHANSIZE);
  263. if (s->channels > MAX_CHANNELS) {
  264. av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
  265. return -1;
  266. }
  267. /* get blocksize if version > 0 */
  268. if (s->version > 0) {
  269. int skip_bytes;
  270. s->blocksize = get_uint(s, av_log2(DEFAULT_BLOCK_SIZE));
  271. maxnlpc = get_uint(s, LPCQSIZE);
  272. s->nmean = get_uint(s, 0);
  273. skip_bytes = get_uint(s, NSKIPSIZE);
  274. for (i=0; i<skip_bytes; i++) {
  275. skip_bits(&s->gb, 8);
  276. }
  277. }
  278. s->nwrap = FFMAX(NWRAP, maxnlpc);
  279. allocate_buffers(s);
  280. init_offset(s);
  281. if (s->version > 1)
  282. s->lpcqoffset = V2LPCQOFFSET;
  283. if (get_ur_golomb_shorten(&s->gb, FNSIZE) != FN_VERBATIM) {
  284. av_log(s->avctx, AV_LOG_ERROR, "missing verbatim section at begining of stream\n");
  285. return -1;
  286. }
  287. s->header_size = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
  288. if (s->header_size >= OUT_BUFFER_SIZE || s->header_size < CANONICAL_HEADER_SIZE) {
  289. av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n", s->header_size);
  290. return -1;
  291. }
  292. for (i=0; i<s->header_size; i++)
  293. s->header[i] = (char)get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
  294. if (decode_wave_header(avctx, s->header, s->header_size) < 0)
  295. return -1;
  296. s->cur_chan = 0;
  297. s->bitshift = 0;
  298. }
  299. else
  300. {
  301. int cmd;
  302. int len;
  303. cmd = get_ur_golomb_shorten(&s->gb, FNSIZE);
  304. switch (cmd) {
  305. case FN_ZERO:
  306. case FN_DIFF0:
  307. case FN_DIFF1:
  308. case FN_DIFF2:
  309. case FN_DIFF3:
  310. case FN_QLPC:
  311. {
  312. int residual_size = 0;
  313. int channel = s->cur_chan;
  314. int32_t coffset;
  315. if (cmd != FN_ZERO) {
  316. residual_size = get_ur_golomb_shorten(&s->gb, ENERGYSIZE);
  317. /* this is a hack as version 0 differed in defintion of get_sr_golomb_shorten */
  318. if (s->version == 0)
  319. residual_size--;
  320. }
  321. if (s->nmean == 0)
  322. coffset = s->offset[channel][0];
  323. else {
  324. int32_t sum = (s->version < 2) ? 0 : s->nmean / 2;
  325. for (i=0; i<s->nmean; i++)
  326. sum += s->offset[channel][i];
  327. coffset = sum / s->nmean;
  328. if (s->version >= 2)
  329. coffset >>= FFMIN(1, s->bitshift);
  330. }
  331. switch (cmd) {
  332. case FN_ZERO:
  333. for (i=0; i<s->blocksize; i++)
  334. s->decoded[channel][i] = 0;
  335. break;
  336. case FN_DIFF0:
  337. for (i=0; i<s->blocksize; i++)
  338. s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + coffset;
  339. break;
  340. case FN_DIFF1:
  341. for (i=0; i<s->blocksize; i++)
  342. s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + s->decoded[channel][i - 1];
  343. break;
  344. case FN_DIFF2:
  345. for (i=0; i<s->blocksize; i++)
  346. s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + 2*s->decoded[channel][i-1]
  347. - s->decoded[channel][i-2];
  348. break;
  349. case FN_DIFF3:
  350. for (i=0; i<s->blocksize; i++)
  351. s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + 3*s->decoded[channel][i-1]
  352. - 3*s->decoded[channel][i-2]
  353. + s->decoded[channel][i-3];
  354. break;
  355. case FN_QLPC:
  356. {
  357. int pred_order = get_ur_golomb_shorten(&s->gb, LPCQSIZE);
  358. for (i=0; i<pred_order; i++)
  359. s->decoded[channel][i - pred_order] -= coffset;
  360. decode_subframe_lpc(s, channel, residual_size, pred_order);
  361. if (coffset != 0)
  362. for (i=0; i < s->blocksize; i++)
  363. s->decoded[channel][i] += coffset;
  364. }
  365. }
  366. if (s->nmean > 0) {
  367. int32_t sum = (s->version < 2) ? 0 : s->blocksize / 2;
  368. for (i=0; i<s->blocksize; i++)
  369. sum += s->decoded[channel][i];
  370. for (i=1; i<s->nmean; i++)
  371. s->offset[channel][i-1] = s->offset[channel][i];
  372. if (s->version < 2)
  373. s->offset[channel][s->nmean - 1] = sum / s->blocksize;
  374. else
  375. s->offset[channel][s->nmean - 1] = (sum / s->blocksize) << s->bitshift;
  376. }
  377. for (i=-s->nwrap; i<0; i++)
  378. s->decoded[channel][i] = s->decoded[channel][i + s->blocksize];
  379. fix_bitshift(s, s->decoded[channel]);
  380. s->cur_chan++;
  381. if (s->cur_chan == s->channels) {
  382. samples = interleave_buffer(samples, s->channels, s->blocksize, s->decoded);
  383. s->cur_chan = 0;
  384. goto frame_done;
  385. }
  386. break;
  387. }
  388. break;
  389. case FN_VERBATIM:
  390. len = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
  391. while (len--) {
  392. get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
  393. }
  394. break;
  395. case FN_BITSHIFT:
  396. s->bitshift = get_ur_golomb_shorten(&s->gb, BITSHIFTSIZE);
  397. break;
  398. case FN_BLOCKSIZE:
  399. s->blocksize = get_uint(s, av_log2(s->blocksize));
  400. break;
  401. case FN_QUIT:
  402. return buf_size;
  403. break;
  404. default:
  405. av_log(avctx, AV_LOG_ERROR, "unknown shorten function %d\n", cmd);
  406. return -1;
  407. break;
  408. }
  409. }
  410. frame_done:
  411. *data_size = (int8_t *)samples - (int8_t *)data;
  412. // s->last_blocksize = s->blocksize;
  413. s->bitindex = get_bits_count(&s->gb) - 8*((get_bits_count(&s->gb))/8);
  414. i= (get_bits_count(&s->gb))/8;
  415. if (i > buf_size) {
  416. av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
  417. s->bitstream_size=0;
  418. s->bitstream_index=0;
  419. return -1;
  420. }
  421. if (s->bitstream_size) {
  422. s->bitstream_index += i;
  423. s->bitstream_size -= i;
  424. return input_buf_size;
  425. } else
  426. return i;
  427. }
  428. static int shorten_decode_close(AVCodecContext *avctx)
  429. {
  430. ShortenContext *s = avctx->priv_data;
  431. int i;
  432. for (i = 0; i < s->channels; i++) {
  433. s->decoded[i] -= s->nwrap;
  434. av_freep(&s->decoded[i]);
  435. av_freep(&s->offset[i]);
  436. }
  437. av_freep(&s->bitstream);
  438. return 0;
  439. }
  440. static void shorten_flush(AVCodecContext *avctx){
  441. ShortenContext *s = avctx->priv_data;
  442. s->bitstream_size=
  443. s->bitstream_index= 0;
  444. }
  445. AVCodec shorten_decoder = {
  446. "shorten",
  447. CODEC_TYPE_AUDIO,
  448. CODEC_ID_SHORTEN,
  449. sizeof(ShortenContext),
  450. shorten_decode_init,
  451. NULL,
  452. shorten_decode_close,
  453. shorten_decode_frame,
  454. .flush= shorten_flush,
  455. };