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