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.

816 lines
26KB

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