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.

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