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.

556 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. #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 = 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 bswap_32(get_bits_long(gb, 32));
  155. }
  156. static inline short get_le16(GetBitContext *gb)
  157. {
  158. return bswap_16(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. int chunk_size;
  165. short wave_format;
  166. init_get_bits(&hb, header, header_size*8);
  167. if (get_le32(&hb) != MKTAG('R','I','F','F')) {
  168. av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n");
  169. return -1;
  170. }
  171. chunk_size = get_le32(&hb);
  172. if (get_le32(&hb) != MKTAG('W','A','V','E')) {
  173. av_log(avctx, AV_LOG_ERROR, "missing WAVE tag\n");
  174. return -1;
  175. }
  176. while (get_le32(&hb) != MKTAG('f','m','t',' ')) {
  177. len = get_le32(&hb);
  178. skip_bits(&hb, 8*len);
  179. }
  180. len = get_le32(&hb);
  181. if (len < 16) {
  182. av_log(avctx, AV_LOG_ERROR, "fmt chunk was too short\n");
  183. return -1;
  184. }
  185. wave_format = get_le16(&hb);
  186. switch (wave_format) {
  187. case WAVE_FORMAT_PCM:
  188. break;
  189. default:
  190. av_log(avctx, AV_LOG_ERROR, "unsupported wave format\n");
  191. return -1;
  192. }
  193. avctx->channels = get_le16(&hb);
  194. avctx->sample_rate = get_le32(&hb);
  195. avctx->bit_rate = get_le32(&hb) * 8;
  196. avctx->block_align = get_le16(&hb);
  197. avctx->bits_per_coded_sample = get_le16(&hb);
  198. if (avctx->bits_per_coded_sample != 16) {
  199. av_log(avctx, AV_LOG_ERROR, "unsupported number of bits per sample\n");
  200. return -1;
  201. }
  202. len -= 16;
  203. if (len > 0)
  204. av_log(avctx, AV_LOG_INFO, "%d header bytes unparsed\n", len);
  205. return 0;
  206. }
  207. static int16_t * interleave_buffer(int16_t *samples, int nchan, int blocksize, int32_t **buffer) {
  208. int i, chan;
  209. for (i=0; i<blocksize; i++)
  210. for (chan=0; chan < nchan; chan++)
  211. *samples++ = FFMIN(buffer[chan][i], 32768);
  212. return samples;
  213. }
  214. static void decode_subframe_lpc(ShortenContext *s, int channel, int residual_size, int pred_order)
  215. {
  216. int sum, i, j;
  217. int *coeffs = s->coeffs;
  218. for (i=0; i<pred_order; i++)
  219. coeffs[i] = get_sr_golomb_shorten(&s->gb, LPCQUANT);
  220. for (i=0; i < s->blocksize; i++) {
  221. sum = s->lpcqoffset;
  222. for (j=0; j<pred_order; j++)
  223. sum += coeffs[j] * s->decoded[channel][i-j-1];
  224. s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + (sum >> LPCQUANT);
  225. }
  226. }
  227. static int shorten_decode_frame(AVCodecContext *avctx,
  228. void *data, int *data_size,
  229. AVPacket *avpkt)
  230. {
  231. const uint8_t *buf = avpkt->data;
  232. int buf_size = avpkt->size;
  233. ShortenContext *s = avctx->priv_data;
  234. int i, input_buf_size = 0;
  235. int16_t *samples = data;
  236. if(s->max_framesize == 0){
  237. s->max_framesize= 1024; // should hopefully be enough for the first header
  238. s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->max_framesize);
  239. }
  240. if(1 && s->max_framesize){//FIXME truncated
  241. buf_size= FFMIN(buf_size, s->max_framesize - s->bitstream_size);
  242. input_buf_size= buf_size;
  243. if(s->bitstream_index + s->bitstream_size + buf_size > s->allocated_bitstream_size){
  244. // printf("memmove\n");
  245. memmove(s->bitstream, &s->bitstream[s->bitstream_index], s->bitstream_size);
  246. s->bitstream_index=0;
  247. }
  248. memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf, buf_size);
  249. buf= &s->bitstream[s->bitstream_index];
  250. buf_size += s->bitstream_size;
  251. s->bitstream_size= buf_size;
  252. if(buf_size < s->max_framesize){
  253. //dprintf(avctx, "wanna more data ... %d\n", buf_size);
  254. *data_size = 0;
  255. return input_buf_size;
  256. }
  257. }
  258. init_get_bits(&s->gb, buf, buf_size*8);
  259. skip_bits(&s->gb, s->bitindex);
  260. if (!s->blocksize)
  261. {
  262. int maxnlpc = 0;
  263. /* shorten signature */
  264. if (get_bits_long(&s->gb, 32) != AV_RB32("ajkg")) {
  265. av_log(s->avctx, AV_LOG_ERROR, "missing shorten magic 'ajkg'\n");
  266. return -1;
  267. }
  268. s->lpcqoffset = 0;
  269. s->blocksize = DEFAULT_BLOCK_SIZE;
  270. s->channels = 1;
  271. s->nmean = -1;
  272. s->version = get_bits(&s->gb, 8);
  273. s->internal_ftype = get_uint(s, TYPESIZE);
  274. s->channels = get_uint(s, CHANSIZE);
  275. if (s->channels > MAX_CHANNELS) {
  276. av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
  277. return -1;
  278. }
  279. /* get blocksize if version > 0 */
  280. if (s->version > 0) {
  281. int skip_bytes;
  282. s->blocksize = get_uint(s, av_log2(DEFAULT_BLOCK_SIZE));
  283. maxnlpc = get_uint(s, LPCQSIZE);
  284. s->nmean = get_uint(s, 0);
  285. skip_bytes = get_uint(s, NSKIPSIZE);
  286. for (i=0; i<skip_bytes; i++) {
  287. skip_bits(&s->gb, 8);
  288. }
  289. }
  290. s->nwrap = FFMAX(NWRAP, maxnlpc);
  291. if (allocate_buffers(s))
  292. return -1;
  293. init_offset(s);
  294. if (s->version > 1)
  295. s->lpcqoffset = V2LPCQOFFSET;
  296. if (get_ur_golomb_shorten(&s->gb, FNSIZE) != FN_VERBATIM) {
  297. av_log(s->avctx, AV_LOG_ERROR, "missing verbatim section at beginning of stream\n");
  298. return -1;
  299. }
  300. s->header_size = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
  301. if (s->header_size >= OUT_BUFFER_SIZE || s->header_size < CANONICAL_HEADER_SIZE) {
  302. av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n", s->header_size);
  303. return -1;
  304. }
  305. for (i=0; i<s->header_size; i++)
  306. s->header[i] = (char)get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
  307. if (decode_wave_header(avctx, s->header, s->header_size) < 0)
  308. return -1;
  309. s->cur_chan = 0;
  310. s->bitshift = 0;
  311. }
  312. else
  313. {
  314. int cmd;
  315. int len;
  316. cmd = get_ur_golomb_shorten(&s->gb, FNSIZE);
  317. switch (cmd) {
  318. case FN_ZERO:
  319. case FN_DIFF0:
  320. case FN_DIFF1:
  321. case FN_DIFF2:
  322. case FN_DIFF3:
  323. case FN_QLPC:
  324. {
  325. int residual_size = 0;
  326. int channel = s->cur_chan;
  327. int32_t coffset;
  328. if (cmd != FN_ZERO) {
  329. residual_size = get_ur_golomb_shorten(&s->gb, ENERGYSIZE);
  330. /* this is a hack as version 0 differed in defintion of get_sr_golomb_shorten */
  331. if (s->version == 0)
  332. residual_size--;
  333. }
  334. if (s->nmean == 0)
  335. coffset = s->offset[channel][0];
  336. else {
  337. int32_t sum = (s->version < 2) ? 0 : s->nmean / 2;
  338. for (i=0; i<s->nmean; i++)
  339. sum += s->offset[channel][i];
  340. coffset = sum / s->nmean;
  341. if (s->version >= 2)
  342. coffset >>= FFMIN(1, s->bitshift);
  343. }
  344. switch (cmd) {
  345. case FN_ZERO:
  346. for (i=0; i<s->blocksize; i++)
  347. s->decoded[channel][i] = 0;
  348. break;
  349. case FN_DIFF0:
  350. for (i=0; i<s->blocksize; i++)
  351. s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + coffset;
  352. break;
  353. case FN_DIFF1:
  354. for (i=0; i<s->blocksize; i++)
  355. s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + s->decoded[channel][i - 1];
  356. break;
  357. case FN_DIFF2:
  358. for (i=0; i<s->blocksize; i++)
  359. s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + 2*s->decoded[channel][i-1]
  360. - s->decoded[channel][i-2];
  361. break;
  362. case FN_DIFF3:
  363. for (i=0; i<s->blocksize; i++)
  364. s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + 3*s->decoded[channel][i-1]
  365. - 3*s->decoded[channel][i-2]
  366. + s->decoded[channel][i-3];
  367. break;
  368. case FN_QLPC:
  369. {
  370. int pred_order = get_ur_golomb_shorten(&s->gb, LPCQSIZE);
  371. if (pred_order > s->nwrap) {
  372. av_log(avctx, AV_LOG_ERROR,
  373. "invalid pred_order %d\n",
  374. pred_order);
  375. return -1;
  376. }
  377. for (i=0; i<pred_order; i++)
  378. s->decoded[channel][i - pred_order] -= coffset;
  379. decode_subframe_lpc(s, channel, residual_size, pred_order);
  380. if (coffset != 0)
  381. for (i=0; i < s->blocksize; i++)
  382. s->decoded[channel][i] += coffset;
  383. }
  384. }
  385. if (s->nmean > 0) {
  386. int32_t sum = (s->version < 2) ? 0 : s->blocksize / 2;
  387. for (i=0; i<s->blocksize; i++)
  388. sum += s->decoded[channel][i];
  389. for (i=1; i<s->nmean; i++)
  390. s->offset[channel][i-1] = s->offset[channel][i];
  391. if (s->version < 2)
  392. s->offset[channel][s->nmean - 1] = sum / s->blocksize;
  393. else
  394. s->offset[channel][s->nmean - 1] = (sum / s->blocksize) << s->bitshift;
  395. }
  396. for (i=-s->nwrap; i<0; i++)
  397. s->decoded[channel][i] = s->decoded[channel][i + s->blocksize];
  398. fix_bitshift(s, s->decoded[channel]);
  399. s->cur_chan++;
  400. if (s->cur_chan == s->channels) {
  401. samples = interleave_buffer(samples, s->channels, s->blocksize, s->decoded);
  402. s->cur_chan = 0;
  403. goto frame_done;
  404. }
  405. break;
  406. }
  407. break;
  408. case FN_VERBATIM:
  409. len = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
  410. while (len--) {
  411. get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
  412. }
  413. break;
  414. case FN_BITSHIFT:
  415. s->bitshift = get_ur_golomb_shorten(&s->gb, BITSHIFTSIZE);
  416. break;
  417. case FN_BLOCKSIZE:
  418. s->blocksize = get_uint(s, av_log2(s->blocksize));
  419. break;
  420. case FN_QUIT:
  421. *data_size = 0;
  422. return buf_size;
  423. break;
  424. default:
  425. av_log(avctx, AV_LOG_ERROR, "unknown shorten function %d\n", cmd);
  426. return -1;
  427. break;
  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 shorten_decoder = {
  467. "shorten",
  468. AVMEDIA_TYPE_AUDIO,
  469. CODEC_ID_SHORTEN,
  470. sizeof(ShortenContext),
  471. shorten_decode_init,
  472. NULL,
  473. shorten_decode_close,
  474. shorten_decode_frame,
  475. .flush= shorten_flush,
  476. .long_name= NULL_IF_CONFIG_SMALL("Shorten"),
  477. };