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.

536 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 inline int get_le32(GetBitContext *gb)
  146. {
  147. return bswap_32(get_bits_long(gb, 32));
  148. }
  149. static inline short 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(avctx, "wanna more data ... %d\n", buf_size);
  245. *data_size = 0;
  246. return input_buf_size;
  247. }
  248. }
  249. init_get_bits(&s->gb, buf, buf_size*8);
  250. skip_bits(&s->gb, s->bitindex);
  251. if (!s->blocksize)
  252. {
  253. int maxnlpc = 0;
  254. /* shorten signature */
  255. if (get_bits_long(&s->gb, 32) != bswap_32(ff_get_fourcc("ajkg"))) {
  256. av_log(s->avctx, AV_LOG_ERROR, "missing shorten magic 'ajkg'\n");
  257. return -1;
  258. }
  259. s->lpcqoffset = 0;
  260. s->blocksize = DEFAULT_BLOCK_SIZE;
  261. s->channels = 1;
  262. s->nmean = -1;
  263. s->version = get_bits(&s->gb, 8);
  264. s->internal_ftype = get_uint(s, TYPESIZE);
  265. s->channels = get_uint(s, CHANSIZE);
  266. if (s->channels > MAX_CHANNELS) {
  267. av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
  268. return -1;
  269. }
  270. /* get blocksize if version > 0 */
  271. if (s->version > 0) {
  272. int skip_bytes;
  273. s->blocksize = get_uint(s, av_log2(DEFAULT_BLOCK_SIZE));
  274. maxnlpc = get_uint(s, LPCQSIZE);
  275. s->nmean = get_uint(s, 0);
  276. skip_bytes = get_uint(s, NSKIPSIZE);
  277. for (i=0; i<skip_bytes; i++) {
  278. skip_bits(&s->gb, 8);
  279. }
  280. }
  281. s->nwrap = FFMAX(NWRAP, maxnlpc);
  282. if (allocate_buffers(s))
  283. return -1;
  284. init_offset(s);
  285. if (s->version > 1)
  286. s->lpcqoffset = V2LPCQOFFSET;
  287. if (get_ur_golomb_shorten(&s->gb, FNSIZE) != FN_VERBATIM) {
  288. av_log(s->avctx, AV_LOG_ERROR, "missing verbatim section at begining of stream\n");
  289. return -1;
  290. }
  291. s->header_size = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
  292. if (s->header_size >= OUT_BUFFER_SIZE || s->header_size < CANONICAL_HEADER_SIZE) {
  293. av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n", s->header_size);
  294. return -1;
  295. }
  296. for (i=0; i<s->header_size; i++)
  297. s->header[i] = (char)get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
  298. if (decode_wave_header(avctx, s->header, s->header_size) < 0)
  299. return -1;
  300. s->cur_chan = 0;
  301. s->bitshift = 0;
  302. }
  303. else
  304. {
  305. int cmd;
  306. int len;
  307. cmd = get_ur_golomb_shorten(&s->gb, FNSIZE);
  308. switch (cmd) {
  309. case FN_ZERO:
  310. case FN_DIFF0:
  311. case FN_DIFF1:
  312. case FN_DIFF2:
  313. case FN_DIFF3:
  314. case FN_QLPC:
  315. {
  316. int residual_size = 0;
  317. int channel = s->cur_chan;
  318. int32_t coffset;
  319. if (cmd != FN_ZERO) {
  320. residual_size = get_ur_golomb_shorten(&s->gb, ENERGYSIZE);
  321. /* this is a hack as version 0 differed in defintion of get_sr_golomb_shorten */
  322. if (s->version == 0)
  323. residual_size--;
  324. }
  325. if (s->nmean == 0)
  326. coffset = s->offset[channel][0];
  327. else {
  328. int32_t sum = (s->version < 2) ? 0 : s->nmean / 2;
  329. for (i=0; i<s->nmean; i++)
  330. sum += s->offset[channel][i];
  331. coffset = sum / s->nmean;
  332. if (s->version >= 2)
  333. coffset >>= FFMIN(1, s->bitshift);
  334. }
  335. switch (cmd) {
  336. case FN_ZERO:
  337. for (i=0; i<s->blocksize; i++)
  338. s->decoded[channel][i] = 0;
  339. break;
  340. case FN_DIFF0:
  341. for (i=0; i<s->blocksize; i++)
  342. s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + coffset;
  343. break;
  344. case FN_DIFF1:
  345. for (i=0; i<s->blocksize; i++)
  346. s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + s->decoded[channel][i - 1];
  347. break;
  348. case FN_DIFF2:
  349. for (i=0; i<s->blocksize; i++)
  350. s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + 2*s->decoded[channel][i-1]
  351. - s->decoded[channel][i-2];
  352. break;
  353. case FN_DIFF3:
  354. for (i=0; i<s->blocksize; i++)
  355. s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + 3*s->decoded[channel][i-1]
  356. - 3*s->decoded[channel][i-2]
  357. + s->decoded[channel][i-3];
  358. break;
  359. case FN_QLPC:
  360. {
  361. int pred_order = get_ur_golomb_shorten(&s->gb, LPCQSIZE);
  362. for (i=0; i<pred_order; i++)
  363. s->decoded[channel][i - pred_order] -= coffset;
  364. decode_subframe_lpc(s, channel, residual_size, pred_order);
  365. if (coffset != 0)
  366. for (i=0; i < s->blocksize; i++)
  367. s->decoded[channel][i] += coffset;
  368. }
  369. }
  370. if (s->nmean > 0) {
  371. int32_t sum = (s->version < 2) ? 0 : s->blocksize / 2;
  372. for (i=0; i<s->blocksize; i++)
  373. sum += s->decoded[channel][i];
  374. for (i=1; i<s->nmean; i++)
  375. s->offset[channel][i-1] = s->offset[channel][i];
  376. if (s->version < 2)
  377. s->offset[channel][s->nmean - 1] = sum / s->blocksize;
  378. else
  379. s->offset[channel][s->nmean - 1] = (sum / s->blocksize) << s->bitshift;
  380. }
  381. for (i=-s->nwrap; i<0; i++)
  382. s->decoded[channel][i] = s->decoded[channel][i + s->blocksize];
  383. fix_bitshift(s, s->decoded[channel]);
  384. s->cur_chan++;
  385. if (s->cur_chan == s->channels) {
  386. samples = interleave_buffer(samples, s->channels, s->blocksize, s->decoded);
  387. s->cur_chan = 0;
  388. goto frame_done;
  389. }
  390. break;
  391. }
  392. break;
  393. case FN_VERBATIM:
  394. len = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
  395. while (len--) {
  396. get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
  397. }
  398. break;
  399. case FN_BITSHIFT:
  400. s->bitshift = get_ur_golomb_shorten(&s->gb, BITSHIFTSIZE);
  401. break;
  402. case FN_BLOCKSIZE:
  403. s->blocksize = get_uint(s, av_log2(s->blocksize));
  404. break;
  405. case FN_QUIT:
  406. *data_size = 0;
  407. return buf_size;
  408. break;
  409. default:
  410. av_log(avctx, AV_LOG_ERROR, "unknown shorten function %d\n", cmd);
  411. return -1;
  412. break;
  413. }
  414. }
  415. frame_done:
  416. *data_size = (int8_t *)samples - (int8_t *)data;
  417. // s->last_blocksize = s->blocksize;
  418. s->bitindex = get_bits_count(&s->gb) - 8*((get_bits_count(&s->gb))/8);
  419. i= (get_bits_count(&s->gb))/8;
  420. if (i > buf_size) {
  421. av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
  422. s->bitstream_size=0;
  423. s->bitstream_index=0;
  424. return -1;
  425. }
  426. if (s->bitstream_size) {
  427. s->bitstream_index += i;
  428. s->bitstream_size -= i;
  429. return input_buf_size;
  430. } else
  431. return i;
  432. }
  433. static int shorten_decode_close(AVCodecContext *avctx)
  434. {
  435. ShortenContext *s = avctx->priv_data;
  436. int i;
  437. for (i = 0; i < s->channels; i++) {
  438. s->decoded[i] -= s->nwrap;
  439. av_freep(&s->decoded[i]);
  440. av_freep(&s->offset[i]);
  441. }
  442. av_freep(&s->bitstream);
  443. return 0;
  444. }
  445. static void shorten_flush(AVCodecContext *avctx){
  446. ShortenContext *s = avctx->priv_data;
  447. s->bitstream_size=
  448. s->bitstream_index= 0;
  449. }
  450. AVCodec shorten_decoder = {
  451. "shorten",
  452. CODEC_TYPE_AUDIO,
  453. CODEC_ID_SHORTEN,
  454. sizeof(ShortenContext),
  455. shorten_decode_init,
  456. NULL,
  457. shorten_decode_close,
  458. shorten_decode_frame,
  459. .flush= shorten_flush,
  460. };