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.

540 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 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 "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. 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_coded_sample = get_le16(&hb);
  192. if (avctx->bits_per_coded_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. AVPacket *avpkt)
  224. {
  225. const uint8_t *buf = avpkt->data;
  226. int buf_size = avpkt->size;
  227. ShortenContext *s = avctx->priv_data;
  228. int i, input_buf_size = 0;
  229. int16_t *samples = data;
  230. if(s->max_framesize == 0){
  231. s->max_framesize= 1024; // should hopefully be enough for the first header
  232. s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->max_framesize);
  233. }
  234. if(1 && s->max_framesize){//FIXME truncated
  235. buf_size= FFMIN(buf_size, s->max_framesize - s->bitstream_size);
  236. input_buf_size= buf_size;
  237. if(s->bitstream_index + s->bitstream_size + buf_size > s->allocated_bitstream_size){
  238. // printf("memmove\n");
  239. memmove(s->bitstream, &s->bitstream[s->bitstream_index], s->bitstream_size);
  240. s->bitstream_index=0;
  241. }
  242. memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf, buf_size);
  243. buf= &s->bitstream[s->bitstream_index];
  244. buf_size += s->bitstream_size;
  245. s->bitstream_size= buf_size;
  246. if(buf_size < s->max_framesize){
  247. //dprintf(avctx, "wanna more data ... %d\n", buf_size);
  248. *data_size = 0;
  249. return input_buf_size;
  250. }
  251. }
  252. init_get_bits(&s->gb, buf, buf_size*8);
  253. skip_bits(&s->gb, s->bitindex);
  254. if (!s->blocksize)
  255. {
  256. int maxnlpc = 0;
  257. /* shorten signature */
  258. if (get_bits_long(&s->gb, 32) != AV_RB32("ajkg")) {
  259. av_log(s->avctx, AV_LOG_ERROR, "missing shorten magic 'ajkg'\n");
  260. return -1;
  261. }
  262. s->lpcqoffset = 0;
  263. s->blocksize = DEFAULT_BLOCK_SIZE;
  264. s->channels = 1;
  265. s->nmean = -1;
  266. s->version = get_bits(&s->gb, 8);
  267. s->internal_ftype = get_uint(s, TYPESIZE);
  268. s->channels = get_uint(s, CHANSIZE);
  269. if (s->channels > MAX_CHANNELS) {
  270. av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
  271. return -1;
  272. }
  273. /* get blocksize if version > 0 */
  274. if (s->version > 0) {
  275. int skip_bytes;
  276. s->blocksize = get_uint(s, av_log2(DEFAULT_BLOCK_SIZE));
  277. maxnlpc = get_uint(s, LPCQSIZE);
  278. s->nmean = get_uint(s, 0);
  279. skip_bytes = get_uint(s, NSKIPSIZE);
  280. for (i=0; i<skip_bytes; i++) {
  281. skip_bits(&s->gb, 8);
  282. }
  283. }
  284. s->nwrap = FFMAX(NWRAP, maxnlpc);
  285. if (allocate_buffers(s))
  286. return -1;
  287. init_offset(s);
  288. if (s->version > 1)
  289. s->lpcqoffset = V2LPCQOFFSET;
  290. if (get_ur_golomb_shorten(&s->gb, FNSIZE) != FN_VERBATIM) {
  291. av_log(s->avctx, AV_LOG_ERROR, "missing verbatim section at beginning of stream\n");
  292. return -1;
  293. }
  294. s->header_size = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
  295. if (s->header_size >= OUT_BUFFER_SIZE || s->header_size < CANONICAL_HEADER_SIZE) {
  296. av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n", s->header_size);
  297. return -1;
  298. }
  299. for (i=0; i<s->header_size; i++)
  300. s->header[i] = (char)get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
  301. if (decode_wave_header(avctx, s->header, s->header_size) < 0)
  302. return -1;
  303. s->cur_chan = 0;
  304. s->bitshift = 0;
  305. }
  306. else
  307. {
  308. int cmd;
  309. int len;
  310. cmd = get_ur_golomb_shorten(&s->gb, FNSIZE);
  311. switch (cmd) {
  312. case FN_ZERO:
  313. case FN_DIFF0:
  314. case FN_DIFF1:
  315. case FN_DIFF2:
  316. case FN_DIFF3:
  317. case FN_QLPC:
  318. {
  319. int residual_size = 0;
  320. int channel = s->cur_chan;
  321. int32_t coffset;
  322. if (cmd != FN_ZERO) {
  323. residual_size = get_ur_golomb_shorten(&s->gb, ENERGYSIZE);
  324. /* this is a hack as version 0 differed in defintion of get_sr_golomb_shorten */
  325. if (s->version == 0)
  326. residual_size--;
  327. }
  328. if (s->nmean == 0)
  329. coffset = s->offset[channel][0];
  330. else {
  331. int32_t sum = (s->version < 2) ? 0 : s->nmean / 2;
  332. for (i=0; i<s->nmean; i++)
  333. sum += s->offset[channel][i];
  334. coffset = sum / s->nmean;
  335. if (s->version >= 2)
  336. coffset >>= FFMIN(1, s->bitshift);
  337. }
  338. switch (cmd) {
  339. case FN_ZERO:
  340. for (i=0; i<s->blocksize; i++)
  341. s->decoded[channel][i] = 0;
  342. break;
  343. case FN_DIFF0:
  344. for (i=0; i<s->blocksize; i++)
  345. s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + coffset;
  346. break;
  347. case FN_DIFF1:
  348. for (i=0; i<s->blocksize; i++)
  349. s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + s->decoded[channel][i - 1];
  350. break;
  351. case FN_DIFF2:
  352. for (i=0; i<s->blocksize; i++)
  353. s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + 2*s->decoded[channel][i-1]
  354. - s->decoded[channel][i-2];
  355. break;
  356. case FN_DIFF3:
  357. for (i=0; i<s->blocksize; i++)
  358. s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + 3*s->decoded[channel][i-1]
  359. - 3*s->decoded[channel][i-2]
  360. + s->decoded[channel][i-3];
  361. break;
  362. case FN_QLPC:
  363. {
  364. int pred_order = get_ur_golomb_shorten(&s->gb, LPCQSIZE);
  365. for (i=0; i<pred_order; i++)
  366. s->decoded[channel][i - pred_order] -= coffset;
  367. decode_subframe_lpc(s, channel, residual_size, pred_order);
  368. if (coffset != 0)
  369. for (i=0; i < s->blocksize; i++)
  370. s->decoded[channel][i] += coffset;
  371. }
  372. }
  373. if (s->nmean > 0) {
  374. int32_t sum = (s->version < 2) ? 0 : s->blocksize / 2;
  375. for (i=0; i<s->blocksize; i++)
  376. sum += s->decoded[channel][i];
  377. for (i=1; i<s->nmean; i++)
  378. s->offset[channel][i-1] = s->offset[channel][i];
  379. if (s->version < 2)
  380. s->offset[channel][s->nmean - 1] = sum / s->blocksize;
  381. else
  382. s->offset[channel][s->nmean - 1] = (sum / s->blocksize) << s->bitshift;
  383. }
  384. for (i=-s->nwrap; i<0; i++)
  385. s->decoded[channel][i] = s->decoded[channel][i + s->blocksize];
  386. fix_bitshift(s, s->decoded[channel]);
  387. s->cur_chan++;
  388. if (s->cur_chan == s->channels) {
  389. samples = interleave_buffer(samples, s->channels, s->blocksize, s->decoded);
  390. s->cur_chan = 0;
  391. goto frame_done;
  392. }
  393. break;
  394. }
  395. break;
  396. case FN_VERBATIM:
  397. len = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
  398. while (len--) {
  399. get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
  400. }
  401. break;
  402. case FN_BITSHIFT:
  403. s->bitshift = get_ur_golomb_shorten(&s->gb, BITSHIFTSIZE);
  404. break;
  405. case FN_BLOCKSIZE:
  406. s->blocksize = get_uint(s, av_log2(s->blocksize));
  407. break;
  408. case FN_QUIT:
  409. *data_size = 0;
  410. return buf_size;
  411. break;
  412. default:
  413. av_log(avctx, AV_LOG_ERROR, "unknown shorten function %d\n", cmd);
  414. return -1;
  415. break;
  416. }
  417. }
  418. frame_done:
  419. *data_size = (int8_t *)samples - (int8_t *)data;
  420. // s->last_blocksize = s->blocksize;
  421. s->bitindex = get_bits_count(&s->gb) - 8*((get_bits_count(&s->gb))/8);
  422. i= (get_bits_count(&s->gb))/8;
  423. if (i > buf_size) {
  424. av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
  425. s->bitstream_size=0;
  426. s->bitstream_index=0;
  427. return -1;
  428. }
  429. if (s->bitstream_size) {
  430. s->bitstream_index += i;
  431. s->bitstream_size -= i;
  432. return input_buf_size;
  433. } else
  434. return i;
  435. }
  436. static av_cold int shorten_decode_close(AVCodecContext *avctx)
  437. {
  438. ShortenContext *s = avctx->priv_data;
  439. int i;
  440. for (i = 0; i < s->channels; i++) {
  441. s->decoded[i] -= s->nwrap;
  442. av_freep(&s->decoded[i]);
  443. av_freep(&s->offset[i]);
  444. }
  445. av_freep(&s->bitstream);
  446. return 0;
  447. }
  448. static void shorten_flush(AVCodecContext *avctx){
  449. ShortenContext *s = avctx->priv_data;
  450. s->bitstream_size=
  451. s->bitstream_index= 0;
  452. }
  453. AVCodec shorten_decoder = {
  454. "shorten",
  455. CODEC_TYPE_AUDIO,
  456. CODEC_ID_SHORTEN,
  457. sizeof(ShortenContext),
  458. shorten_decode_init,
  459. NULL,
  460. shorten_decode_close,
  461. shorten_decode_frame,
  462. .flush= shorten_flush,
  463. .long_name= NULL_IF_CONFIG_SMALL("Shorten"),
  464. };