shorten.c 17 KB

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