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.

688 lines
21KB

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