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. */
  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] * (unsigned)s->decoded[channel][i - j - 1];
  268. s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) +
  269. (unsigned)(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. if (s->nmean > 32768U) {
  321. av_log(s->avctx, AV_LOG_ERROR, "nmean is: %d\n", s->nmean);
  322. return AVERROR_INVALIDDATA;
  323. }
  324. skip_bytes = get_uint(s, NSKIPSIZE);
  325. if ((unsigned)skip_bytes > get_bits_left(&s->gb)/8) {
  326. av_log(s->avctx, AV_LOG_ERROR, "invalid skip_bytes: %d\n", skip_bytes);
  327. return AVERROR_INVALIDDATA;
  328. }
  329. for (i = 0; i < skip_bytes; i++)
  330. skip_bits(&s->gb, 8);
  331. }
  332. s->nwrap = FFMAX(NWRAP, maxnlpc);
  333. if ((ret = allocate_buffers(s)) < 0)
  334. return ret;
  335. if ((ret = init_offset(s)) < 0)
  336. return ret;
  337. if (s->version > 1)
  338. s->lpcqoffset = V2LPCQOFFSET;
  339. if (get_ur_golomb_shorten(&s->gb, FNSIZE) != FN_VERBATIM) {
  340. av_log(s->avctx, AV_LOG_ERROR,
  341. "missing verbatim section at beginning of stream\n");
  342. return AVERROR_INVALIDDATA;
  343. }
  344. s->header_size = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
  345. if (s->header_size >= OUT_BUFFER_SIZE ||
  346. s->header_size < CANONICAL_HEADER_SIZE) {
  347. av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n",
  348. s->header_size);
  349. return AVERROR_INVALIDDATA;
  350. }
  351. for (i = 0; i < s->header_size; i++)
  352. s->header[i] = (char)get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
  353. if ((ret = decode_wave_header(s->avctx, s->header, s->header_size)) < 0)
  354. return ret;
  355. s->cur_chan = 0;
  356. s->bitshift = 0;
  357. s->got_header = 1;
  358. return 0;
  359. }
  360. static int shorten_decode_frame(AVCodecContext *avctx, void *data,
  361. int *got_frame_ptr, AVPacket *avpkt)
  362. {
  363. AVFrame *frame = data;
  364. const uint8_t *buf = avpkt->data;
  365. int buf_size = avpkt->size;
  366. ShortenContext *s = avctx->priv_data;
  367. int i, input_buf_size = 0;
  368. int ret;
  369. /* allocate internal bitstream buffer */
  370. if (s->max_framesize == 0) {
  371. void *tmp_ptr;
  372. s->max_framesize = 8192; // should hopefully be enough for the first header
  373. tmp_ptr = av_fast_realloc(s->bitstream, &s->allocated_bitstream_size,
  374. s->max_framesize + AV_INPUT_BUFFER_PADDING_SIZE);
  375. if (!tmp_ptr) {
  376. av_log(avctx, AV_LOG_ERROR, "error allocating bitstream buffer\n");
  377. return AVERROR(ENOMEM);
  378. }
  379. memset(tmp_ptr, 0, s->allocated_bitstream_size);
  380. s->bitstream = tmp_ptr;
  381. }
  382. /* append current packet data to bitstream buffer */
  383. if (1 && s->max_framesize) { //FIXME truncated
  384. buf_size = FFMIN(buf_size, s->max_framesize - s->bitstream_size);
  385. input_buf_size = buf_size;
  386. if (s->bitstream_index + s->bitstream_size + buf_size + AV_INPUT_BUFFER_PADDING_SIZE >
  387. s->allocated_bitstream_size) {
  388. memmove(s->bitstream, &s->bitstream[s->bitstream_index],
  389. s->bitstream_size);
  390. s->bitstream_index = 0;
  391. }
  392. if (buf)
  393. memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf,
  394. buf_size);
  395. buf = &s->bitstream[s->bitstream_index];
  396. buf_size += s->bitstream_size;
  397. s->bitstream_size = buf_size;
  398. /* do not decode until buffer has at least max_framesize bytes or
  399. * the end of the file has been reached */
  400. if (buf_size < s->max_framesize && avpkt->data) {
  401. *got_frame_ptr = 0;
  402. return input_buf_size;
  403. }
  404. }
  405. /* init and position bitstream reader */
  406. if ((ret = init_get_bits8(&s->gb, buf, buf_size)) < 0)
  407. return ret;
  408. skip_bits(&s->gb, s->bitindex);
  409. /* process header or next subblock */
  410. if (!s->got_header) {
  411. if ((ret = read_header(s)) < 0)
  412. return ret;
  413. *got_frame_ptr = 0;
  414. goto finish_frame;
  415. }
  416. /* if quit command was read previously, don't decode anything */
  417. if (s->got_quit_command) {
  418. *got_frame_ptr = 0;
  419. return avpkt->size;
  420. }
  421. s->cur_chan = 0;
  422. while (s->cur_chan < s->channels) {
  423. unsigned cmd;
  424. int len;
  425. if (get_bits_left(&s->gb) < 3 + FNSIZE) {
  426. *got_frame_ptr = 0;
  427. break;
  428. }
  429. cmd = get_ur_golomb_shorten(&s->gb, FNSIZE);
  430. if (cmd > FN_VERBATIM) {
  431. av_log(avctx, AV_LOG_ERROR, "unknown shorten function %d\n", cmd);
  432. *got_frame_ptr = 0;
  433. break;
  434. }
  435. if (!is_audio_command[cmd]) {
  436. /* process non-audio command */
  437. switch (cmd) {
  438. case FN_VERBATIM:
  439. len = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
  440. if (len < 0 || len > get_bits_left(&s->gb)) {
  441. av_log(avctx, AV_LOG_ERROR, "verbatim length %d invalid\n",
  442. len);
  443. return AVERROR_INVALIDDATA;
  444. }
  445. while (len--)
  446. get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
  447. break;
  448. case FN_BITSHIFT: {
  449. unsigned bitshift = get_ur_golomb_shorten(&s->gb, BITSHIFTSIZE);
  450. if (bitshift > 31) {
  451. av_log(avctx, AV_LOG_ERROR, "bitshift %d is invalid\n",
  452. bitshift);
  453. return AVERROR_INVALIDDATA;
  454. }
  455. s->bitshift = bitshift;
  456. break;
  457. }
  458. case FN_BLOCKSIZE: {
  459. unsigned blocksize = get_uint(s, av_log2(s->blocksize));
  460. if (blocksize > s->blocksize) {
  461. av_log(avctx, AV_LOG_ERROR,
  462. "Increasing block size is not supported\n");
  463. return AVERROR_PATCHWELCOME;
  464. }
  465. if (!blocksize || blocksize > MAX_BLOCKSIZE) {
  466. av_log(avctx, AV_LOG_ERROR, "invalid or unsupported "
  467. "block size: %d\n", blocksize);
  468. return AVERROR(EINVAL);
  469. }
  470. s->blocksize = blocksize;
  471. break;
  472. }
  473. case FN_QUIT:
  474. s->got_quit_command = 1;
  475. break;
  476. }
  477. if (cmd == FN_BLOCKSIZE || cmd == FN_QUIT) {
  478. *got_frame_ptr = 0;
  479. break;
  480. }
  481. } else {
  482. /* process audio command */
  483. int residual_size = 0;
  484. int channel = s->cur_chan;
  485. int32_t coffset;
  486. /* get Rice code for residual decoding */
  487. if (cmd != FN_ZERO) {
  488. residual_size = get_ur_golomb_shorten(&s->gb, ENERGYSIZE);
  489. /* This is a hack as version 0 differed in the definition
  490. * of get_sr_golomb_shorten(). */
  491. if (s->version == 0)
  492. residual_size--;
  493. }
  494. /* calculate sample offset using means from previous blocks */
  495. if (s->nmean == 0)
  496. coffset = s->offset[channel][0];
  497. else {
  498. int32_t sum = (s->version < 2) ? 0 : s->nmean / 2;
  499. for (i = 0; i < s->nmean; i++)
  500. sum += (unsigned)s->offset[channel][i];
  501. coffset = sum / s->nmean;
  502. if (s->version >= 2)
  503. coffset = s->bitshift == 0 ? coffset : coffset >> s->bitshift - 1 >> 1;
  504. }
  505. /* decode samples for this channel */
  506. if (cmd == FN_ZERO) {
  507. for (i = 0; i < s->blocksize; i++)
  508. s->decoded[channel][i] = 0;
  509. } else {
  510. if ((ret = decode_subframe_lpc(s, cmd, channel,
  511. residual_size, coffset)) < 0)
  512. return ret;
  513. }
  514. /* update means with info from the current block */
  515. if (s->nmean > 0) {
  516. int64_t sum = (s->version < 2) ? 0 : s->blocksize / 2;
  517. for (i = 0; i < s->blocksize; i++)
  518. sum += s->decoded[channel][i];
  519. for (i = 1; i < s->nmean; i++)
  520. s->offset[channel][i - 1] = s->offset[channel][i];
  521. if (s->version < 2)
  522. s->offset[channel][s->nmean - 1] = sum / s->blocksize;
  523. else
  524. s->offset[channel][s->nmean - 1] = (sum / s->blocksize) << s->bitshift;
  525. }
  526. /* copy wrap samples for use with next block */
  527. for (i = -s->nwrap; i < 0; i++)
  528. s->decoded[channel][i] = s->decoded[channel][i + s->blocksize];
  529. /* shift samples to add in unused zero bits which were removed
  530. * during encoding */
  531. fix_bitshift(s, s->decoded[channel]);
  532. /* if this is the last channel in the block, output the samples */
  533. s->cur_chan++;
  534. if (s->cur_chan == s->channels) {
  535. uint8_t *samples_u8;
  536. int16_t *samples_s16;
  537. int chan;
  538. /* get output buffer */
  539. frame->nb_samples = s->blocksize;
  540. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  541. return ret;
  542. for (chan = 0; chan < s->channels; chan++) {
  543. samples_u8 = ((uint8_t **)frame->extended_data)[chan];
  544. samples_s16 = ((int16_t **)frame->extended_data)[chan];
  545. for (i = 0; i < s->blocksize; i++) {
  546. switch (s->internal_ftype) {
  547. case TYPE_U8:
  548. *samples_u8++ = av_clip_uint8(s->decoded[chan][i]);
  549. break;
  550. case TYPE_S16HL:
  551. case TYPE_S16LH:
  552. *samples_s16++ = av_clip_int16(s->decoded[chan][i]);
  553. break;
  554. }
  555. }
  556. }
  557. *got_frame_ptr = 1;
  558. }
  559. }
  560. }
  561. if (s->cur_chan < s->channels)
  562. *got_frame_ptr = 0;
  563. finish_frame:
  564. s->bitindex = get_bits_count(&s->gb) - 8 * (get_bits_count(&s->gb) / 8);
  565. i = get_bits_count(&s->gb) / 8;
  566. if (i > buf_size) {
  567. av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
  568. s->bitstream_size = 0;
  569. s->bitstream_index = 0;
  570. return AVERROR_INVALIDDATA;
  571. }
  572. if (s->bitstream_size) {
  573. s->bitstream_index += i;
  574. s->bitstream_size -= i;
  575. return input_buf_size;
  576. } else
  577. return i;
  578. }
  579. static av_cold int shorten_decode_close(AVCodecContext *avctx)
  580. {
  581. ShortenContext *s = avctx->priv_data;
  582. int i;
  583. for (i = 0; i < s->channels; i++) {
  584. s->decoded[i] = NULL;
  585. av_freep(&s->decoded_base[i]);
  586. av_freep(&s->offset[i]);
  587. }
  588. av_freep(&s->bitstream);
  589. av_freep(&s->coeffs);
  590. return 0;
  591. }
  592. AVCodec ff_shorten_decoder = {
  593. .name = "shorten",
  594. .long_name = NULL_IF_CONFIG_SMALL("Shorten"),
  595. .type = AVMEDIA_TYPE_AUDIO,
  596. .id = AV_CODEC_ID_SHORTEN,
  597. .priv_data_size = sizeof(ShortenContext),
  598. .init = shorten_decode_init,
  599. .close = shorten_decode_close,
  600. .decode = shorten_decode_frame,
  601. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
  602. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
  603. AV_SAMPLE_FMT_U8P,
  604. AV_SAMPLE_FMT_NONE },
  605. };