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.

803 lines
25KB

  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] <<= 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] -= 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] * s->decoded[channel][i - j - 1];
  331. s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) +
  332. (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] += 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. skip_bytes = get_uint(s, NSKIPSIZE);
  384. if ((unsigned)skip_bytes > get_bits_left(&s->gb)/8) {
  385. av_log(s->avctx, AV_LOG_ERROR, "invalid skip_bytes: %d\n", skip_bytes);
  386. return AVERROR_INVALIDDATA;
  387. }
  388. for (i = 0; i < skip_bytes; i++)
  389. skip_bits(&s->gb, 8);
  390. }
  391. s->nwrap = FFMAX(NWRAP, maxnlpc);
  392. if (s->version > 1)
  393. s->lpcqoffset = V2LPCQOFFSET;
  394. if (s->avctx->extradata_size > 0)
  395. goto end;
  396. if (get_ur_golomb_shorten(&s->gb, FNSIZE) != FN_VERBATIM) {
  397. av_log(s->avctx, AV_LOG_ERROR,
  398. "missing verbatim section at beginning of stream\n");
  399. return AVERROR_INVALIDDATA;
  400. }
  401. s->header_size = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
  402. if (s->header_size >= OUT_BUFFER_SIZE ||
  403. s->header_size < CANONICAL_HEADER_SIZE) {
  404. av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n",
  405. s->header_size);
  406. return AVERROR_INVALIDDATA;
  407. }
  408. for (i = 0; i < s->header_size; i++)
  409. s->header[i] = (char)get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
  410. if (AV_RL32(s->header) == MKTAG('R','I','F','F')) {
  411. if ((ret = decode_wave_header(s->avctx, s->header, s->header_size)) < 0)
  412. return ret;
  413. } else if (AV_RL32(s->header) == MKTAG('F','O','R','M')) {
  414. if ((ret = decode_aiff_header(s->avctx, s->header, s->header_size)) < 0)
  415. return ret;
  416. } else {
  417. avpriv_report_missing_feature(s->avctx, "unsupported bit packing %"
  418. PRIX32, AV_RL32(s->header));
  419. return AVERROR_PATCHWELCOME;
  420. }
  421. end:
  422. if ((ret = allocate_buffers(s)) < 0)
  423. return ret;
  424. if ((ret = init_offset(s)) < 0)
  425. return ret;
  426. s->cur_chan = 0;
  427. s->bitshift = 0;
  428. s->got_header = 1;
  429. return 0;
  430. }
  431. static int shorten_decode_frame(AVCodecContext *avctx, void *data,
  432. int *got_frame_ptr, AVPacket *avpkt)
  433. {
  434. AVFrame *frame = data;
  435. const uint8_t *buf = avpkt->data;
  436. int buf_size = avpkt->size;
  437. ShortenContext *s = avctx->priv_data;
  438. int i, input_buf_size = 0;
  439. int ret;
  440. /* allocate internal bitstream buffer */
  441. if (s->max_framesize == 0) {
  442. void *tmp_ptr;
  443. s->max_framesize = 8192; // should hopefully be enough for the first header
  444. tmp_ptr = av_fast_realloc(s->bitstream, &s->allocated_bitstream_size,
  445. s->max_framesize + AV_INPUT_BUFFER_PADDING_SIZE);
  446. if (!tmp_ptr) {
  447. s->max_framesize = 0;
  448. av_log(avctx, AV_LOG_ERROR, "error allocating bitstream buffer\n");
  449. return AVERROR(ENOMEM);
  450. }
  451. memset(tmp_ptr, 0, s->allocated_bitstream_size);
  452. s->bitstream = tmp_ptr;
  453. }
  454. /* append current packet data to bitstream buffer */
  455. buf_size = FFMIN(buf_size, s->max_framesize - s->bitstream_size);
  456. input_buf_size = buf_size;
  457. if (s->bitstream_index + s->bitstream_size + buf_size + AV_INPUT_BUFFER_PADDING_SIZE >
  458. s->allocated_bitstream_size) {
  459. memmove(s->bitstream, &s->bitstream[s->bitstream_index],
  460. s->bitstream_size);
  461. s->bitstream_index = 0;
  462. }
  463. if (buf)
  464. memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf,
  465. buf_size);
  466. buf = &s->bitstream[s->bitstream_index];
  467. buf_size += s->bitstream_size;
  468. s->bitstream_size = buf_size;
  469. /* do not decode until buffer has at least max_framesize bytes or
  470. * the end of the file has been reached */
  471. if (buf_size < s->max_framesize && avpkt->data) {
  472. *got_frame_ptr = 0;
  473. return input_buf_size;
  474. }
  475. /* init and position bitstream reader */
  476. if ((ret = init_get_bits8(&s->gb, buf, buf_size)) < 0)
  477. return ret;
  478. skip_bits(&s->gb, s->bitindex);
  479. /* process header or next subblock */
  480. if (!s->got_header) {
  481. if ((ret = read_header(s)) < 0)
  482. return ret;
  483. if (avpkt->size) {
  484. int max_framesize;
  485. void *tmp_ptr;
  486. max_framesize = FFMAX(s->max_framesize, s->blocksize * s->channels * 8);
  487. tmp_ptr = av_fast_realloc(s->bitstream, &s->allocated_bitstream_size,
  488. max_framesize + AV_INPUT_BUFFER_PADDING_SIZE);
  489. if (!tmp_ptr) {
  490. av_log(avctx, AV_LOG_ERROR, "error allocating bitstream buffer\n");
  491. return AVERROR(ENOMEM);
  492. }
  493. s->bitstream = tmp_ptr;
  494. s->max_framesize = max_framesize;
  495. *got_frame_ptr = 0;
  496. goto finish_frame;
  497. }
  498. }
  499. /* if quit command was read previously, don't decode anything */
  500. if (s->got_quit_command) {
  501. *got_frame_ptr = 0;
  502. return avpkt->size;
  503. }
  504. s->cur_chan = 0;
  505. while (s->cur_chan < s->channels) {
  506. unsigned cmd;
  507. int len;
  508. if (get_bits_left(&s->gb) < 3 + FNSIZE) {
  509. *got_frame_ptr = 0;
  510. break;
  511. }
  512. cmd = get_ur_golomb_shorten(&s->gb, FNSIZE);
  513. if (cmd > FN_VERBATIM) {
  514. av_log(avctx, AV_LOG_ERROR, "unknown shorten function %d\n", cmd);
  515. *got_frame_ptr = 0;
  516. break;
  517. }
  518. if (!is_audio_command[cmd]) {
  519. /* process non-audio command */
  520. switch (cmd) {
  521. case FN_VERBATIM:
  522. len = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
  523. while (len--)
  524. get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
  525. break;
  526. case FN_BITSHIFT: {
  527. unsigned bitshift = get_ur_golomb_shorten(&s->gb, BITSHIFTSIZE);
  528. if (bitshift > 32) {
  529. av_log(avctx, AV_LOG_ERROR, "bitshift %d is invalid\n",
  530. bitshift);
  531. return AVERROR_INVALIDDATA;
  532. }
  533. s->bitshift = bitshift;
  534. break;
  535. }
  536. case FN_BLOCKSIZE: {
  537. unsigned blocksize = get_uint(s, av_log2(s->blocksize));
  538. if (blocksize > s->blocksize) {
  539. avpriv_report_missing_feature(avctx,
  540. "Increasing block size");
  541. return AVERROR_PATCHWELCOME;
  542. }
  543. if (!blocksize || blocksize > MAX_BLOCKSIZE) {
  544. av_log(avctx, AV_LOG_ERROR, "invalid or unsupported "
  545. "block size: %d\n", blocksize);
  546. return AVERROR(EINVAL);
  547. }
  548. s->blocksize = blocksize;
  549. break;
  550. }
  551. case FN_QUIT:
  552. s->got_quit_command = 1;
  553. break;
  554. }
  555. if (cmd == FN_QUIT)
  556. break;
  557. } else {
  558. /* process audio command */
  559. int residual_size = 0;
  560. int channel = s->cur_chan;
  561. int32_t coffset;
  562. /* get Rice code for residual decoding */
  563. if (cmd != FN_ZERO) {
  564. residual_size = get_ur_golomb_shorten(&s->gb, ENERGYSIZE);
  565. /* This is a hack as version 0 differed in the definition
  566. * of get_sr_golomb_shorten(). */
  567. if (s->version == 0)
  568. residual_size--;
  569. }
  570. /* calculate sample offset using means from previous blocks */
  571. if (s->nmean == 0)
  572. coffset = s->offset[channel][0];
  573. else {
  574. int32_t sum = (s->version < 2) ? 0 : s->nmean / 2;
  575. for (i = 0; i < s->nmean; i++)
  576. sum += s->offset[channel][i];
  577. coffset = sum / s->nmean;
  578. if (s->version >= 2)
  579. coffset = s->bitshift == 0 ? coffset : coffset >> s->bitshift - 1 >> 1;
  580. }
  581. /* decode samples for this channel */
  582. if (cmd == FN_ZERO) {
  583. for (i = 0; i < s->blocksize; i++)
  584. s->decoded[channel][i] = 0;
  585. } else {
  586. if ((ret = decode_subframe_lpc(s, cmd, channel,
  587. residual_size, coffset)) < 0)
  588. return ret;
  589. }
  590. /* update means with info from the current block */
  591. if (s->nmean > 0) {
  592. int32_t sum = (s->version < 2) ? 0 : s->blocksize / 2;
  593. for (i = 0; i < s->blocksize; i++)
  594. sum += s->decoded[channel][i];
  595. for (i = 1; i < s->nmean; i++)
  596. s->offset[channel][i - 1] = s->offset[channel][i];
  597. if (s->version < 2)
  598. s->offset[channel][s->nmean - 1] = sum / s->blocksize;
  599. else
  600. s->offset[channel][s->nmean - 1] = s->bitshift == 32 ? 0 : (sum / s->blocksize) << s->bitshift;
  601. }
  602. /* copy wrap samples for use with next block */
  603. for (i = -s->nwrap; i < 0; i++)
  604. s->decoded[channel][i] = s->decoded[channel][i + s->blocksize];
  605. /* shift samples to add in unused zero bits which were removed
  606. * during encoding */
  607. fix_bitshift(s, s->decoded[channel]);
  608. /* if this is the last channel in the block, output the samples */
  609. s->cur_chan++;
  610. if (s->cur_chan == s->channels) {
  611. uint8_t *samples_u8;
  612. int16_t *samples_s16;
  613. int chan;
  614. /* get output buffer */
  615. frame->nb_samples = s->blocksize;
  616. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  617. return ret;
  618. for (chan = 0; chan < s->channels; chan++) {
  619. samples_u8 = ((uint8_t **)frame->extended_data)[chan];
  620. samples_s16 = ((int16_t **)frame->extended_data)[chan];
  621. for (i = 0; i < s->blocksize; i++) {
  622. switch (s->internal_ftype) {
  623. case TYPE_U8:
  624. *samples_u8++ = av_clip_uint8(s->decoded[chan][i]);
  625. break;
  626. case TYPE_S16HL:
  627. case TYPE_S16LH:
  628. *samples_s16++ = av_clip_int16(s->decoded[chan][i]);
  629. break;
  630. }
  631. }
  632. if (s->swap && s->internal_ftype != TYPE_U8)
  633. s->bdsp.bswap16_buf(((uint16_t **)frame->extended_data)[chan],
  634. ((uint16_t **)frame->extended_data)[chan],
  635. s->blocksize);
  636. }
  637. *got_frame_ptr = 1;
  638. }
  639. }
  640. }
  641. if (s->cur_chan < s->channels)
  642. *got_frame_ptr = 0;
  643. finish_frame:
  644. s->bitindex = get_bits_count(&s->gb) - 8 * (get_bits_count(&s->gb) / 8);
  645. i = get_bits_count(&s->gb) / 8;
  646. if (i > buf_size) {
  647. av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
  648. s->bitstream_size = 0;
  649. s->bitstream_index = 0;
  650. return AVERROR_INVALIDDATA;
  651. }
  652. if (s->bitstream_size) {
  653. s->bitstream_index += i;
  654. s->bitstream_size -= i;
  655. return input_buf_size;
  656. } else
  657. return i;
  658. }
  659. static av_cold int shorten_decode_close(AVCodecContext *avctx)
  660. {
  661. ShortenContext *s = avctx->priv_data;
  662. int i;
  663. for (i = 0; i < s->channels; i++) {
  664. s->decoded[i] = NULL;
  665. av_freep(&s->decoded_base[i]);
  666. av_freep(&s->offset[i]);
  667. }
  668. av_freep(&s->bitstream);
  669. av_freep(&s->coeffs);
  670. return 0;
  671. }
  672. AVCodec ff_shorten_decoder = {
  673. .name = "shorten",
  674. .long_name = NULL_IF_CONFIG_SMALL("Shorten"),
  675. .type = AVMEDIA_TYPE_AUDIO,
  676. .id = AV_CODEC_ID_SHORTEN,
  677. .priv_data_size = sizeof(ShortenContext),
  678. .init = shorten_decode_init,
  679. .close = shorten_decode_close,
  680. .decode = shorten_decode_frame,
  681. .capabilities = AV_CODEC_CAP_SUBFRAMES | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
  682. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
  683. AV_SAMPLE_FMT_U8P,
  684. AV_SAMPLE_FMT_NONE },
  685. };