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.

704 lines
22KB

  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. #include <limits.h>
  27. #include "avcodec.h"
  28. #include "bytestream.h"
  29. #include "get_bits.h"
  30. #include "golomb.h"
  31. #include "internal.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_S8 1
  44. #define TYPE_U8 2
  45. #define TYPE_S16HL 3
  46. #define TYPE_U16HL 4
  47. #define TYPE_S16LH 5
  48. #define TYPE_U16LH 6
  49. #define NWRAP 3
  50. #define NSKIPSIZE 1
  51. #define LPCQUANT 5
  52. #define V2LPCQOFFSET (1 << LPCQUANT)
  53. #define FNSIZE 2
  54. #define FN_DIFF0 0
  55. #define FN_DIFF1 1
  56. #define FN_DIFF2 2
  57. #define FN_DIFF3 3
  58. #define FN_QUIT 4
  59. #define FN_BLOCKSIZE 5
  60. #define FN_BITSHIFT 6
  61. #define FN_QLPC 7
  62. #define FN_ZERO 8
  63. #define FN_VERBATIM 9
  64. /** indicates if the FN_* command is audio or non-audio */
  65. static const uint8_t is_audio_command[10] = { 1, 1, 1, 1, 0, 0, 0, 1, 1, 0 };
  66. #define VERBATIM_CKSIZE_SIZE 5
  67. #define VERBATIM_BYTE_SIZE 8
  68. #define CANONICAL_HEADER_SIZE 44
  69. typedef struct ShortenContext {
  70. AVCodecContext *avctx;
  71. GetBitContext gb;
  72. int min_framesize, max_framesize;
  73. unsigned channels;
  74. int32_t *decoded[MAX_CHANNELS];
  75. int32_t *decoded_base[MAX_CHANNELS];
  76. int32_t *offset[MAX_CHANNELS];
  77. int *coeffs;
  78. uint8_t *bitstream;
  79. int bitstream_size;
  80. int bitstream_index;
  81. unsigned int allocated_bitstream_size;
  82. int header_size;
  83. uint8_t header[OUT_BUFFER_SIZE];
  84. int version;
  85. int cur_chan;
  86. int bitshift;
  87. int nmean;
  88. int internal_ftype;
  89. int nwrap;
  90. int blocksize;
  91. int bitindex;
  92. int32_t lpcqoffset;
  93. int got_header;
  94. int got_quit_command;
  95. } ShortenContext;
  96. static av_cold int shorten_decode_init(AVCodecContext *avctx)
  97. {
  98. ShortenContext *s = avctx->priv_data;
  99. s->avctx = avctx;
  100. return 0;
  101. }
  102. static int allocate_buffers(ShortenContext *s)
  103. {
  104. int i, chan, err;
  105. for (chan = 0; chan < s->channels; chan++) {
  106. if (FFMAX(1, s->nmean) >= UINT_MAX / sizeof(int32_t)) {
  107. av_log(s->avctx, AV_LOG_ERROR, "nmean too large\n");
  108. return AVERROR_INVALIDDATA;
  109. }
  110. if (s->blocksize + (uint64_t)s->nwrap >= UINT_MAX / sizeof(int32_t)) {
  111. av_log(s->avctx, AV_LOG_ERROR,
  112. "s->blocksize + s->nwrap too large\n");
  113. return AVERROR_INVALIDDATA;
  114. }
  115. if ((err = av_reallocp_array(&s->offset[chan],
  116. sizeof(int32_t),
  117. FFMAX(1, s->nmean))) < 0)
  118. return err;
  119. if ((err = av_reallocp_array(&s->decoded_base[chan], (s->blocksize + s->nwrap),
  120. sizeof(s->decoded_base[0][0]))) < 0)
  121. return err;
  122. for (i = 0; i < s->nwrap; i++)
  123. s->decoded_base[chan][i] = 0;
  124. s->decoded[chan] = s->decoded_base[chan] + s->nwrap;
  125. }
  126. if ((err = av_reallocp_array(&s->coeffs, s->nwrap, sizeof(*s->coeffs))) < 0)
  127. return err;
  128. return 0;
  129. }
  130. static inline unsigned int get_uint(ShortenContext *s, int k)
  131. {
  132. if (s->version != 0)
  133. k = get_ur_golomb_shorten(&s->gb, ULONGSIZE);
  134. return get_ur_golomb_shorten(&s->gb, k);
  135. }
  136. static void fix_bitshift(ShortenContext *s, int32_t *buffer)
  137. {
  138. int i;
  139. if (s->bitshift != 0)
  140. for (i = 0; i < s->blocksize; i++)
  141. buffer[i] <<= s->bitshift;
  142. }
  143. static int init_offset(ShortenContext *s)
  144. {
  145. int32_t mean = 0;
  146. int chan, i;
  147. int nblock = FFMAX(1, s->nmean);
  148. /* initialise offset */
  149. switch (s->internal_ftype) {
  150. case TYPE_U8:
  151. s->avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
  152. mean = 0x80;
  153. break;
  154. case TYPE_S16HL:
  155. case TYPE_S16LH:
  156. s->avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
  157. break;
  158. default:
  159. av_log(s->avctx, AV_LOG_ERROR, "unknown audio type\n");
  160. return AVERROR_PATCHWELCOME;
  161. }
  162. for (chan = 0; chan < s->channels; chan++)
  163. for (i = 0; i < nblock; i++)
  164. s->offset[chan][i] = mean;
  165. return 0;
  166. }
  167. static int decode_wave_header(AVCodecContext *avctx, const uint8_t *header,
  168. int header_size)
  169. {
  170. int len, bps;
  171. short wave_format;
  172. GetByteContext gb;
  173. bytestream2_init(&gb, header, header_size);
  174. if (bytestream2_get_le32(&gb) != MKTAG('R', 'I', 'F', 'F')) {
  175. av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n");
  176. return AVERROR_INVALIDDATA;
  177. }
  178. bytestream2_skip(&gb, 4); /* chunk size */
  179. if (bytestream2_get_le32(&gb) != MKTAG('W', 'A', 'V', 'E')) {
  180. av_log(avctx, AV_LOG_ERROR, "missing WAVE tag\n");
  181. return AVERROR_INVALIDDATA;
  182. }
  183. while (bytestream2_get_le32(&gb) != MKTAG('f', 'm', 't', ' ')) {
  184. len = bytestream2_get_le32(&gb);
  185. bytestream2_skip(&gb, len);
  186. if (len < 0 || bytestream2_get_bytes_left(&gb) < 16) {
  187. av_log(avctx, AV_LOG_ERROR, "no fmt chunk found\n");
  188. return AVERROR_INVALIDDATA;
  189. }
  190. }
  191. len = bytestream2_get_le32(&gb);
  192. if (len < 16) {
  193. av_log(avctx, AV_LOG_ERROR, "fmt chunk was too short\n");
  194. return AVERROR_INVALIDDATA;
  195. }
  196. wave_format = bytestream2_get_le16(&gb);
  197. switch (wave_format) {
  198. case WAVE_FORMAT_PCM:
  199. break;
  200. default:
  201. av_log(avctx, AV_LOG_ERROR, "unsupported wave format\n");
  202. return AVERROR(ENOSYS);
  203. }
  204. bytestream2_skip(&gb, 2); // skip channels (already got from shorten header)
  205. avctx->sample_rate = bytestream2_get_le32(&gb);
  206. bytestream2_skip(&gb, 4); // skip bit rate (represents original uncompressed bit rate)
  207. bytestream2_skip(&gb, 2); // skip block align (not needed)
  208. bps = bytestream2_get_le16(&gb);
  209. avctx->bits_per_coded_sample = bps;
  210. if (bps != 16 && bps != 8) {
  211. av_log(avctx, AV_LOG_ERROR, "unsupported number of bits per sample: %d\n", bps);
  212. return AVERROR(ENOSYS);
  213. }
  214. len -= 16;
  215. if (len > 0)
  216. av_log(avctx, AV_LOG_INFO, "%d header bytes unparsed\n", len);
  217. return 0;
  218. }
  219. static const int fixed_coeffs[][3] = {
  220. { 0, 0, 0 },
  221. { 1, 0, 0 },
  222. { 2, -1, 0 },
  223. { 3, -3, 1 }
  224. };
  225. static int decode_subframe_lpc(ShortenContext *s, int command, int channel,
  226. int residual_size, int32_t coffset)
  227. {
  228. int pred_order, sum, qshift, init_sum, i, j;
  229. const int *coeffs;
  230. if (command == FN_QLPC) {
  231. /* read/validate prediction order */
  232. pred_order = get_ur_golomb_shorten(&s->gb, LPCQSIZE);
  233. if ((unsigned)pred_order > s->nwrap) {
  234. av_log(s->avctx, AV_LOG_ERROR, "invalid pred_order %d\n",
  235. pred_order);
  236. return AVERROR(EINVAL);
  237. }
  238. /* read LPC coefficients */
  239. for (i = 0; i < pred_order; i++)
  240. s->coeffs[i] = get_sr_golomb_shorten(&s->gb, LPCQUANT);
  241. coeffs = s->coeffs;
  242. qshift = LPCQUANT;
  243. } else {
  244. /* fixed LPC coeffs */
  245. pred_order = command;
  246. if (pred_order >= FF_ARRAY_ELEMS(fixed_coeffs)) {
  247. av_log(s->avctx, AV_LOG_ERROR, "invalid pred_order %d\n",
  248. pred_order);
  249. return AVERROR_INVALIDDATA;
  250. }
  251. coeffs = fixed_coeffs[pred_order];
  252. qshift = 0;
  253. }
  254. /* subtract offset from previous samples to use in prediction */
  255. if (command == FN_QLPC && coffset)
  256. for (i = -pred_order; i < 0; i++)
  257. s->decoded[channel][i] -= coffset;
  258. /* decode residual and do LPC prediction */
  259. init_sum = pred_order ? (command == FN_QLPC ? s->lpcqoffset : 0) : coffset;
  260. for (i = 0; i < s->blocksize; i++) {
  261. sum = init_sum;
  262. for (j = 0; j < pred_order; j++)
  263. sum += coeffs[j] * s->decoded[channel][i - j - 1];
  264. s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) +
  265. (sum >> qshift);
  266. }
  267. /* add offset to current samples */
  268. if (command == FN_QLPC && coffset)
  269. for (i = 0; i < s->blocksize; i++)
  270. s->decoded[channel][i] += coffset;
  271. return 0;
  272. }
  273. static int read_header(ShortenContext *s)
  274. {
  275. int i, ret;
  276. int maxnlpc = 0;
  277. /* shorten signature */
  278. if (get_bits_long(&s->gb, 32) != AV_RB32("ajkg")) {
  279. av_log(s->avctx, AV_LOG_ERROR, "missing shorten magic 'ajkg'\n");
  280. return AVERROR_INVALIDDATA;
  281. }
  282. s->lpcqoffset = 0;
  283. s->blocksize = DEFAULT_BLOCK_SIZE;
  284. s->nmean = -1;
  285. s->version = get_bits(&s->gb, 8);
  286. s->internal_ftype = get_uint(s, TYPESIZE);
  287. s->channels = get_uint(s, CHANSIZE);
  288. if (!s->channels) {
  289. av_log(s->avctx, AV_LOG_ERROR, "No channels reported\n");
  290. return AVERROR_INVALIDDATA;
  291. }
  292. if (s->channels > MAX_CHANNELS) {
  293. av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
  294. s->channels = 0;
  295. return AVERROR_INVALIDDATA;
  296. }
  297. s->avctx->channels = s->channels;
  298. /* get blocksize if version > 0 */
  299. if (s->version > 0) {
  300. int skip_bytes;
  301. unsigned blocksize;
  302. blocksize = get_uint(s, av_log2(DEFAULT_BLOCK_SIZE));
  303. if (!blocksize || blocksize > MAX_BLOCKSIZE) {
  304. av_log(s->avctx, AV_LOG_ERROR,
  305. "invalid or unsupported block size: %d\n",
  306. blocksize);
  307. return AVERROR(EINVAL);
  308. }
  309. s->blocksize = blocksize;
  310. maxnlpc = get_uint(s, LPCQSIZE);
  311. s->nmean = get_uint(s, 0);
  312. skip_bytes = get_uint(s, NSKIPSIZE);
  313. if ((unsigned)skip_bytes > get_bits_left(&s->gb)/8) {
  314. av_log(s->avctx, AV_LOG_ERROR, "invalid skip_bytes: %d\n", skip_bytes);
  315. return AVERROR_INVALIDDATA;
  316. }
  317. for (i = 0; i < skip_bytes; i++)
  318. skip_bits(&s->gb, 8);
  319. }
  320. s->nwrap = FFMAX(NWRAP, maxnlpc);
  321. if ((ret = allocate_buffers(s)) < 0)
  322. return ret;
  323. if ((ret = init_offset(s)) < 0)
  324. return ret;
  325. if (s->version > 1)
  326. s->lpcqoffset = V2LPCQOFFSET;
  327. if (s->avctx->extradata_size > 0)
  328. goto end;
  329. if (get_ur_golomb_shorten(&s->gb, FNSIZE) != FN_VERBATIM) {
  330. av_log(s->avctx, AV_LOG_ERROR,
  331. "missing verbatim section at beginning of stream\n");
  332. return AVERROR_INVALIDDATA;
  333. }
  334. s->header_size = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
  335. if (s->header_size >= OUT_BUFFER_SIZE ||
  336. s->header_size < CANONICAL_HEADER_SIZE) {
  337. av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n",
  338. s->header_size);
  339. return AVERROR_INVALIDDATA;
  340. }
  341. for (i = 0; i < s->header_size; i++)
  342. s->header[i] = (char)get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
  343. if ((ret = decode_wave_header(s->avctx, s->header, s->header_size)) < 0)
  344. return ret;
  345. end:
  346. s->cur_chan = 0;
  347. s->bitshift = 0;
  348. s->got_header = 1;
  349. return 0;
  350. }
  351. static int shorten_decode_frame(AVCodecContext *avctx, void *data,
  352. int *got_frame_ptr, AVPacket *avpkt)
  353. {
  354. AVFrame *frame = data;
  355. const uint8_t *buf = avpkt->data;
  356. int buf_size = avpkt->size;
  357. ShortenContext *s = avctx->priv_data;
  358. int i, input_buf_size = 0;
  359. int ret;
  360. /* allocate internal bitstream buffer */
  361. if (s->max_framesize == 0) {
  362. void *tmp_ptr;
  363. s->max_framesize = 8192; // should hopefully be enough for the first header
  364. tmp_ptr = av_fast_realloc(s->bitstream, &s->allocated_bitstream_size,
  365. s->max_framesize + AV_INPUT_BUFFER_PADDING_SIZE);
  366. if (!tmp_ptr) {
  367. s->max_framesize = 0;
  368. av_log(avctx, AV_LOG_ERROR, "error allocating bitstream buffer\n");
  369. return AVERROR(ENOMEM);
  370. }
  371. memset(tmp_ptr, 0, s->allocated_bitstream_size);
  372. s->bitstream = tmp_ptr;
  373. }
  374. /* append current packet data to bitstream buffer */
  375. buf_size = FFMIN(buf_size, s->max_framesize - s->bitstream_size);
  376. input_buf_size = buf_size;
  377. if (s->bitstream_index + s->bitstream_size + buf_size + AV_INPUT_BUFFER_PADDING_SIZE >
  378. s->allocated_bitstream_size) {
  379. memmove(s->bitstream, &s->bitstream[s->bitstream_index],
  380. s->bitstream_size);
  381. s->bitstream_index = 0;
  382. }
  383. if (buf)
  384. memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf,
  385. buf_size);
  386. buf = &s->bitstream[s->bitstream_index];
  387. buf_size += s->bitstream_size;
  388. s->bitstream_size = buf_size;
  389. /* do not decode until buffer has at least max_framesize bytes or
  390. * the end of the file has been reached */
  391. if (buf_size < s->max_framesize && avpkt->data) {
  392. *got_frame_ptr = 0;
  393. return input_buf_size;
  394. }
  395. /* init and position bitstream reader */
  396. if ((ret = init_get_bits8(&s->gb, buf, buf_size)) < 0)
  397. return ret;
  398. skip_bits(&s->gb, s->bitindex);
  399. /* process header or next subblock */
  400. if (!s->got_header) {
  401. if ((ret = read_header(s)) < 0)
  402. return ret;
  403. if (avpkt->size) {
  404. int max_framesize;
  405. void *tmp_ptr;
  406. max_framesize = FFMAX(s->max_framesize, s->blocksize * s->channels * 2);
  407. tmp_ptr = av_fast_realloc(s->bitstream, &s->allocated_bitstream_size,
  408. max_framesize + AV_INPUT_BUFFER_PADDING_SIZE);
  409. if (!tmp_ptr) {
  410. av_log(avctx, AV_LOG_ERROR, "error allocating bitstream buffer\n");
  411. return AVERROR(ENOMEM);
  412. }
  413. s->bitstream = tmp_ptr;
  414. s->max_framesize = max_framesize;
  415. *got_frame_ptr = 0;
  416. goto finish_frame;
  417. }
  418. }
  419. /* if quit command was read previously, don't decode anything */
  420. if (s->got_quit_command) {
  421. *got_frame_ptr = 0;
  422. return avpkt->size;
  423. }
  424. s->cur_chan = 0;
  425. while (s->cur_chan < s->channels) {
  426. unsigned cmd;
  427. int len;
  428. if (get_bits_left(&s->gb) < 3 + FNSIZE) {
  429. *got_frame_ptr = 0;
  430. break;
  431. }
  432. cmd = get_ur_golomb_shorten(&s->gb, FNSIZE);
  433. if (cmd > FN_VERBATIM) {
  434. av_log(avctx, AV_LOG_ERROR, "unknown shorten function %d\n", cmd);
  435. *got_frame_ptr = 0;
  436. break;
  437. }
  438. if (!is_audio_command[cmd]) {
  439. /* process non-audio command */
  440. switch (cmd) {
  441. case FN_VERBATIM:
  442. len = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
  443. while (len--)
  444. get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
  445. break;
  446. case FN_BITSHIFT: {
  447. unsigned bitshift = get_ur_golomb_shorten(&s->gb, BITSHIFTSIZE);
  448. if (bitshift > 31) {
  449. av_log(avctx, AV_LOG_ERROR, "bitshift %d is invalid\n",
  450. bitshift);
  451. return AVERROR_INVALIDDATA;
  452. }
  453. s->bitshift = bitshift;
  454. break;
  455. }
  456. case FN_BLOCKSIZE: {
  457. unsigned blocksize = get_uint(s, av_log2(s->blocksize));
  458. if (blocksize > s->blocksize) {
  459. av_log(avctx, AV_LOG_ERROR,
  460. "Increasing block size is not supported\n");
  461. return AVERROR_PATCHWELCOME;
  462. }
  463. if (!blocksize || blocksize > MAX_BLOCKSIZE) {
  464. av_log(avctx, AV_LOG_ERROR, "invalid or unsupported "
  465. "block size: %d\n", blocksize);
  466. return AVERROR(EINVAL);
  467. }
  468. s->blocksize = blocksize;
  469. break;
  470. }
  471. case FN_QUIT:
  472. s->got_quit_command = 1;
  473. break;
  474. }
  475. if (cmd == FN_QUIT)
  476. break;
  477. } else {
  478. /* process audio command */
  479. int residual_size = 0;
  480. int channel = s->cur_chan;
  481. int32_t coffset;
  482. /* get Rice code for residual decoding */
  483. if (cmd != FN_ZERO) {
  484. residual_size = get_ur_golomb_shorten(&s->gb, ENERGYSIZE);
  485. /* This is a hack as version 0 differed in the definition
  486. * of get_sr_golomb_shorten(). */
  487. if (s->version == 0)
  488. residual_size--;
  489. }
  490. /* calculate sample offset using means from previous blocks */
  491. if (s->nmean == 0)
  492. coffset = s->offset[channel][0];
  493. else {
  494. int32_t sum = (s->version < 2) ? 0 : s->nmean / 2;
  495. for (i = 0; i < s->nmean; i++)
  496. sum += s->offset[channel][i];
  497. coffset = sum / s->nmean;
  498. if (s->version >= 2)
  499. coffset = s->bitshift == 0 ? coffset : coffset >> s->bitshift - 1 >> 1;
  500. }
  501. /* decode samples for this channel */
  502. if (cmd == FN_ZERO) {
  503. for (i = 0; i < s->blocksize; i++)
  504. s->decoded[channel][i] = 0;
  505. } else {
  506. if ((ret = decode_subframe_lpc(s, cmd, channel,
  507. residual_size, coffset)) < 0)
  508. return ret;
  509. }
  510. /* update means with info from the current block */
  511. if (s->nmean > 0) {
  512. int32_t sum = (s->version < 2) ? 0 : s->blocksize / 2;
  513. for (i = 0; i < s->blocksize; i++)
  514. sum += s->decoded[channel][i];
  515. for (i = 1; i < s->nmean; i++)
  516. s->offset[channel][i - 1] = s->offset[channel][i];
  517. if (s->version < 2)
  518. s->offset[channel][s->nmean - 1] = sum / s->blocksize;
  519. else
  520. s->offset[channel][s->nmean - 1] = (sum / s->blocksize) << s->bitshift;
  521. }
  522. /* copy wrap samples for use with next block */
  523. for (i = -s->nwrap; i < 0; i++)
  524. s->decoded[channel][i] = s->decoded[channel][i + s->blocksize];
  525. /* shift samples to add in unused zero bits which were removed
  526. * during encoding */
  527. fix_bitshift(s, s->decoded[channel]);
  528. /* if this is the last channel in the block, output the samples */
  529. s->cur_chan++;
  530. if (s->cur_chan == s->channels) {
  531. uint8_t *samples_u8;
  532. int16_t *samples_s16;
  533. int chan;
  534. /* get output buffer */
  535. frame->nb_samples = s->blocksize;
  536. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  537. return ret;
  538. for (chan = 0; chan < s->channels; chan++) {
  539. samples_u8 = ((uint8_t **)frame->extended_data)[chan];
  540. samples_s16 = ((int16_t **)frame->extended_data)[chan];
  541. for (i = 0; i < s->blocksize; i++) {
  542. switch (s->internal_ftype) {
  543. case TYPE_U8:
  544. *samples_u8++ = av_clip_uint8(s->decoded[chan][i]);
  545. break;
  546. case TYPE_S16HL:
  547. case TYPE_S16LH:
  548. *samples_s16++ = av_clip_int16(s->decoded[chan][i]);
  549. break;
  550. }
  551. }
  552. }
  553. *got_frame_ptr = 1;
  554. }
  555. }
  556. }
  557. if (s->cur_chan < s->channels)
  558. *got_frame_ptr = 0;
  559. finish_frame:
  560. s->bitindex = get_bits_count(&s->gb) - 8 * (get_bits_count(&s->gb) / 8);
  561. i = get_bits_count(&s->gb) / 8;
  562. if (i > buf_size) {
  563. av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
  564. s->bitstream_size = 0;
  565. s->bitstream_index = 0;
  566. return AVERROR_INVALIDDATA;
  567. }
  568. if (s->bitstream_size) {
  569. s->bitstream_index += i;
  570. s->bitstream_size -= i;
  571. return input_buf_size;
  572. } else
  573. return i;
  574. }
  575. static av_cold int shorten_decode_close(AVCodecContext *avctx)
  576. {
  577. ShortenContext *s = avctx->priv_data;
  578. int i;
  579. for (i = 0; i < s->channels; i++) {
  580. s->decoded[i] = NULL;
  581. av_freep(&s->decoded_base[i]);
  582. av_freep(&s->offset[i]);
  583. }
  584. av_freep(&s->bitstream);
  585. av_freep(&s->coeffs);
  586. return 0;
  587. }
  588. AVCodec ff_shorten_decoder = {
  589. .name = "shorten",
  590. .long_name = NULL_IF_CONFIG_SMALL("Shorten"),
  591. .type = AVMEDIA_TYPE_AUDIO,
  592. .id = AV_CODEC_ID_SHORTEN,
  593. .priv_data_size = sizeof(ShortenContext),
  594. .init = shorten_decode_init,
  595. .close = shorten_decode_close,
  596. .decode = shorten_decode_frame,
  597. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
  598. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
  599. AV_SAMPLE_FMT_U8P,
  600. AV_SAMPLE_FMT_NONE },
  601. };