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.

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