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.

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