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.

538 lines
17KB

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