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.

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