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.

534 lines
16KB

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