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.

776 lines
24KB

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