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 + s->nwrap >= UINT_MAX / sizeof(int32_t) ||
  112. s->blocksize + s->nwrap <= (unsigned)s->nwrap) {
  113. av_log(s->avctx, AV_LOG_ERROR,
  114. "s->blocksize + s->nwrap too large\n");
  115. return AVERROR_INVALIDDATA;
  116. }
  117. if ((err = av_reallocp_array(&s->offset[chan],
  118. sizeof(int32_t),
  119. FFMAX(1, s->nmean))) < 0)
  120. return err;
  121. if ((err = av_reallocp_array(&s->decoded_base[chan], (s->blocksize + s->nwrap),
  122. sizeof(s->decoded_base[0][0]))) < 0)
  123. return err;
  124. for (i = 0; i < s->nwrap; i++)
  125. s->decoded_base[chan][i] = 0;
  126. s->decoded[chan] = s->decoded_base[chan] + s->nwrap;
  127. }
  128. if ((err = av_reallocp_array(&s->coeffs, s->nwrap, sizeof(*s->coeffs))) < 0)
  129. return err;
  130. return 0;
  131. }
  132. static inline unsigned int get_uint(ShortenContext *s, int k)
  133. {
  134. if (s->version != 0)
  135. k = get_ur_golomb_shorten(&s->gb, ULONGSIZE);
  136. return get_ur_golomb_shorten(&s->gb, k);
  137. }
  138. static void fix_bitshift(ShortenContext *s, int32_t *buffer)
  139. {
  140. int i;
  141. if (s->bitshift != 0)
  142. for (i = 0; i < s->blocksize; i++)
  143. buffer[i] <<= s->bitshift;
  144. }
  145. static int init_offset(ShortenContext *s)
  146. {
  147. int32_t mean = 0;
  148. int chan, i;
  149. int nblock = FFMAX(1, s->nmean);
  150. /* initialise offset */
  151. switch (s->internal_ftype) {
  152. case TYPE_U8:
  153. s->avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
  154. mean = 0x80;
  155. break;
  156. case TYPE_S16HL:
  157. case TYPE_S16LH:
  158. s->avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
  159. break;
  160. default:
  161. av_log(s->avctx, AV_LOG_ERROR, "unknown audio type\n");
  162. return AVERROR_PATCHWELCOME;
  163. }
  164. for (chan = 0; chan < s->channels; chan++)
  165. for (i = 0; i < nblock; i++)
  166. s->offset[chan][i] = mean;
  167. return 0;
  168. }
  169. static int decode_wave_header(AVCodecContext *avctx, const uint8_t *header,
  170. int header_size)
  171. {
  172. int len, bps;
  173. short wave_format;
  174. GetByteContext gb;
  175. bytestream2_init(&gb, header, header_size);
  176. if (bytestream2_get_le32(&gb) != MKTAG('R', 'I', 'F', 'F')) {
  177. av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n");
  178. return AVERROR_INVALIDDATA;
  179. }
  180. bytestream2_skip(&gb, 4); /* chunk size */
  181. if (bytestream2_get_le32(&gb) != MKTAG('W', 'A', 'V', 'E')) {
  182. av_log(avctx, AV_LOG_ERROR, "missing WAVE tag\n");
  183. return AVERROR_INVALIDDATA;
  184. }
  185. while (bytestream2_get_le32(&gb) != MKTAG('f', 'm', 't', ' ')) {
  186. len = bytestream2_get_le32(&gb);
  187. bytestream2_skip(&gb, len);
  188. if (len < 0 || bytestream2_get_bytes_left(&gb) < 16) {
  189. av_log(avctx, AV_LOG_ERROR, "no fmt chunk found\n");
  190. return AVERROR_INVALIDDATA;
  191. }
  192. }
  193. len = bytestream2_get_le32(&gb);
  194. if (len < 16) {
  195. av_log(avctx, AV_LOG_ERROR, "fmt chunk was too short\n");
  196. return AVERROR_INVALIDDATA;
  197. }
  198. wave_format = bytestream2_get_le16(&gb);
  199. switch (wave_format) {
  200. case WAVE_FORMAT_PCM:
  201. break;
  202. default:
  203. av_log(avctx, AV_LOG_ERROR, "unsupported wave format\n");
  204. return AVERROR(ENOSYS);
  205. }
  206. bytestream2_skip(&gb, 2); // skip channels (already got from shorten header)
  207. avctx->sample_rate = bytestream2_get_le32(&gb);
  208. bytestream2_skip(&gb, 4); // skip bit rate (represents original uncompressed bit rate)
  209. bytestream2_skip(&gb, 2); // skip block align (not needed)
  210. bps = bytestream2_get_le16(&gb);
  211. avctx->bits_per_coded_sample = bps;
  212. if (bps != 16 && bps != 8) {
  213. av_log(avctx, AV_LOG_ERROR, "unsupported number of bits per sample: %d\n", bps);
  214. return AVERROR(ENOSYS);
  215. }
  216. len -= 16;
  217. if (len > 0)
  218. av_log(avctx, AV_LOG_INFO, "%d header bytes unparsed\n", len);
  219. return 0;
  220. }
  221. static const int fixed_coeffs[][3] = {
  222. { 0, 0, 0 },
  223. { 1, 0, 0 },
  224. { 2, -1, 0 },
  225. { 3, -3, 1 }
  226. };
  227. static int decode_subframe_lpc(ShortenContext *s, int command, int channel,
  228. int residual_size, int32_t coffset)
  229. {
  230. int pred_order, sum, qshift, init_sum, i, j;
  231. const int *coeffs;
  232. if (command == FN_QLPC) {
  233. /* read/validate prediction order */
  234. pred_order = get_ur_golomb_shorten(&s->gb, LPCQSIZE);
  235. if (pred_order > s->nwrap) {
  236. av_log(s->avctx, AV_LOG_ERROR, "invalid pred_order %d\n",
  237. pred_order);
  238. return AVERROR(EINVAL);
  239. }
  240. /* read LPC coefficients */
  241. for (i = 0; i < pred_order; i++)
  242. s->coeffs[i] = get_sr_golomb_shorten(&s->gb, LPCQUANT);
  243. coeffs = s->coeffs;
  244. qshift = LPCQUANT;
  245. } else {
  246. /* fixed LPC coeffs */
  247. pred_order = command;
  248. if (pred_order >= FF_ARRAY_ELEMS(fixed_coeffs)) {
  249. av_log(s->avctx, AV_LOG_ERROR, "invalid pred_order %d\n",
  250. pred_order);
  251. return AVERROR_INVALIDDATA;
  252. }
  253. coeffs = fixed_coeffs[pred_order];
  254. qshift = 0;
  255. }
  256. /* subtract offset from previous samples to use in prediction */
  257. if (command == FN_QLPC && coffset)
  258. for (i = -pred_order; i < 0; i++)
  259. s->decoded[channel][i] -= coffset;
  260. /* decode residual and do LPC prediction */
  261. init_sum = pred_order ? (command == FN_QLPC ? s->lpcqoffset : 0) : coffset;
  262. for (i = 0; i < s->blocksize; i++) {
  263. sum = init_sum;
  264. for (j = 0; j < pred_order; j++)
  265. sum += coeffs[j] * s->decoded[channel][i - j - 1];
  266. s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) +
  267. (sum >> qshift);
  268. }
  269. /* add offset to current samples */
  270. if (command == FN_QLPC && coffset)
  271. for (i = 0; i < s->blocksize; i++)
  272. s->decoded[channel][i] += coffset;
  273. return 0;
  274. }
  275. static int read_header(ShortenContext *s)
  276. {
  277. int i, ret;
  278. int maxnlpc = 0;
  279. /* shorten signature */
  280. if (get_bits_long(&s->gb, 32) != AV_RB32("ajkg")) {
  281. av_log(s->avctx, AV_LOG_ERROR, "missing shorten magic 'ajkg'\n");
  282. return AVERROR_INVALIDDATA;
  283. }
  284. s->lpcqoffset = 0;
  285. s->blocksize = DEFAULT_BLOCK_SIZE;
  286. s->nmean = -1;
  287. s->version = get_bits(&s->gb, 8);
  288. s->internal_ftype = get_uint(s, TYPESIZE);
  289. s->channels = get_uint(s, CHANSIZE);
  290. if (!s->channels) {
  291. av_log(s->avctx, AV_LOG_ERROR, "No channels reported\n");
  292. return AVERROR_INVALIDDATA;
  293. }
  294. if (s->channels > MAX_CHANNELS) {
  295. av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
  296. s->channels = 0;
  297. return AVERROR_INVALIDDATA;
  298. }
  299. s->avctx->channels = s->channels;
  300. /* get blocksize if version > 0 */
  301. if (s->version > 0) {
  302. int skip_bytes;
  303. unsigned blocksize;
  304. blocksize = get_uint(s, av_log2(DEFAULT_BLOCK_SIZE));
  305. if (!blocksize || blocksize > MAX_BLOCKSIZE) {
  306. av_log(s->avctx, AV_LOG_ERROR,
  307. "invalid or unsupported block size: %d\n",
  308. blocksize);
  309. return AVERROR(EINVAL);
  310. }
  311. s->blocksize = blocksize;
  312. maxnlpc = get_uint(s, LPCQSIZE);
  313. s->nmean = get_uint(s, 0);
  314. skip_bytes = get_uint(s, NSKIPSIZE);
  315. if ((unsigned)skip_bytes > get_bits_left(&s->gb)/8) {
  316. av_log(s->avctx, AV_LOG_ERROR, "invalid skip_bytes: %d\n", skip_bytes);
  317. return AVERROR_INVALIDDATA;
  318. }
  319. for (i = 0; i < skip_bytes; i++)
  320. skip_bits(&s->gb, 8);
  321. }
  322. s->nwrap = FFMAX(NWRAP, maxnlpc);
  323. if ((ret = allocate_buffers(s)) < 0)
  324. return ret;
  325. if ((ret = init_offset(s)) < 0)
  326. return ret;
  327. if (s->version > 1)
  328. s->lpcqoffset = V2LPCQOFFSET;
  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. s->cur_chan = 0;
  346. s->bitshift = 0;
  347. s->got_header = 1;
  348. return 0;
  349. }
  350. static int shorten_decode_frame(AVCodecContext *avctx, void *data,
  351. int *got_frame_ptr, AVPacket *avpkt)
  352. {
  353. AVFrame *frame = data;
  354. const uint8_t *buf = avpkt->data;
  355. int buf_size = avpkt->size;
  356. ShortenContext *s = avctx->priv_data;
  357. int i, input_buf_size = 0;
  358. int ret;
  359. /* allocate internal bitstream buffer */
  360. if (s->max_framesize == 0) {
  361. void *tmp_ptr;
  362. s->max_framesize = 8192; // should hopefully be enough for the first header
  363. tmp_ptr = av_fast_realloc(s->bitstream, &s->allocated_bitstream_size,
  364. s->max_framesize + FF_INPUT_BUFFER_PADDING_SIZE);
  365. if (!tmp_ptr) {
  366. av_log(avctx, AV_LOG_ERROR, "error allocating bitstream buffer\n");
  367. return AVERROR(ENOMEM);
  368. }
  369. memset(tmp_ptr, 0, s->allocated_bitstream_size);
  370. s->bitstream = tmp_ptr;
  371. }
  372. /* append current packet data to bitstream buffer */
  373. if (1 && s->max_framesize) { //FIXME truncated
  374. buf_size = FFMIN(buf_size, s->max_framesize - s->bitstream_size);
  375. input_buf_size = buf_size;
  376. if (s->bitstream_index + s->bitstream_size + buf_size + FF_INPUT_BUFFER_PADDING_SIZE >
  377. s->allocated_bitstream_size) {
  378. memmove(s->bitstream, &s->bitstream[s->bitstream_index],
  379. s->bitstream_size);
  380. s->bitstream_index = 0;
  381. }
  382. if (buf)
  383. memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf,
  384. buf_size);
  385. buf = &s->bitstream[s->bitstream_index];
  386. buf_size += s->bitstream_size;
  387. s->bitstream_size = buf_size;
  388. /* do not decode until buffer has at least max_framesize bytes or
  389. * the end of the file has been reached */
  390. if (buf_size < s->max_framesize && avpkt->data) {
  391. *got_frame_ptr = 0;
  392. return input_buf_size;
  393. }
  394. }
  395. /* init and position bitstream reader */
  396. init_get_bits(&s->gb, buf, buf_size * 8);
  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. };