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.

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