FFmpeg
shorten.c
Go to the documentation of this file.
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 /**
23  * @file
24  * Shorten decoder
25  * @author Jeff Muizelaar
26  */
27 
28 #include <limits.h>
29 #include "avcodec.h"
30 #include "bswapdsp.h"
31 #include "bytestream.h"
32 #include "codec_internal.h"
33 #include "get_bits.h"
34 #include "golomb.h"
35 #include "internal.h"
36 
37 #define MAX_CHANNELS 8
38 #define MAX_BLOCKSIZE 65535
39 
40 #define OUT_BUFFER_SIZE 16384
41 
42 #define ULONGSIZE 2
43 
44 #define WAVE_FORMAT_PCM 0x0001
45 
46 #define DEFAULT_BLOCK_SIZE 256
47 
48 #define TYPESIZE 4
49 #define CHANSIZE 0
50 #define LPCQSIZE 2
51 #define ENERGYSIZE 3
52 #define BITSHIFTSIZE 2
53 
54 #define TYPE_S8 1
55 #define TYPE_U8 2
56 #define TYPE_S16HL 3
57 #define TYPE_U16HL 4
58 #define TYPE_S16LH 5
59 #define TYPE_U16LH 6
60 
61 #define NWRAP 3
62 #define NSKIPSIZE 1
63 
64 #define LPCQUANT 5
65 #define V2LPCQOFFSET (1 << LPCQUANT)
66 
67 #define FNSIZE 2
68 #define FN_DIFF0 0
69 #define FN_DIFF1 1
70 #define FN_DIFF2 2
71 #define FN_DIFF3 3
72 #define FN_QUIT 4
73 #define FN_BLOCKSIZE 5
74 #define FN_BITSHIFT 6
75 #define FN_QLPC 7
76 #define FN_ZERO 8
77 #define FN_VERBATIM 9
78 
79 /** indicates if the FN_* command is audio or non-audio */
80 static const uint8_t is_audio_command[10] = { 1, 1, 1, 1, 0, 0, 0, 1, 1, 0 };
81 
82 #define VERBATIM_CKSIZE_SIZE 5
83 #define VERBATIM_BYTE_SIZE 8
84 #define CANONICAL_HEADER_SIZE 44
85 
86 typedef struct ShortenContext {
89 
91  unsigned channels;
92 
96  int *coeffs;
97  uint8_t *bitstream;
103  int version;
104  int cur_chan;
105  int bitshift;
106  int nmean;
108  int nwrap;
110  int bitindex;
114  int swap;
117 
119 {
120  ShortenContext *s = avctx->priv_data;
121  s->avctx = avctx;
122 
123  ff_bswapdsp_init(&s->bdsp);
124 
125  return 0;
126 }
127 
129 {
130  int i, chan, err;
131 
132  for (chan = 0; chan < s->channels; chan++) {
133  if (FFMAX(1, s->nmean) >= UINT_MAX / sizeof(int32_t)) {
134  av_log(s->avctx, AV_LOG_ERROR, "nmean too large\n");
135  return AVERROR_INVALIDDATA;
136  }
137  if (s->blocksize + (uint64_t)s->nwrap >= UINT_MAX / sizeof(int32_t)) {
138  av_log(s->avctx, AV_LOG_ERROR,
139  "s->blocksize + s->nwrap too large\n");
140  return AVERROR_INVALIDDATA;
141  }
142 
143  if ((err = av_reallocp_array(&s->offset[chan],
144  sizeof(int32_t),
145  FFMAX(1, s->nmean))) < 0)
146  return err;
147 
148  if ((err = av_reallocp_array(&s->decoded_base[chan], (s->blocksize + s->nwrap),
149  sizeof(s->decoded_base[0][0]))) < 0)
150  return err;
151  for (i = 0; i < s->nwrap; i++)
152  s->decoded_base[chan][i] = 0;
153  s->decoded[chan] = s->decoded_base[chan] + s->nwrap;
154  }
155 
156  if ((err = av_reallocp_array(&s->coeffs, s->nwrap, sizeof(*s->coeffs))) < 0)
157  return err;
158 
159  return 0;
160 }
161 
162 static inline unsigned int get_uint(ShortenContext *s, int k)
163 {
164  if (s->version != 0) {
165  k = get_ur_golomb_shorten(&s->gb, ULONGSIZE);
166  if (k > 31U)
167  return AVERROR_INVALIDDATA;
168  }
169  return get_ur_golomb_shorten(&s->gb, k);
170 }
171 
173 {
174  int i;
175 
176  if (s->bitshift == 32) {
177  for (i = 0; i < s->blocksize; i++)
178  buffer[i] = 0;
179  } else if (s->bitshift != 0) {
180  for (i = 0; i < s->blocksize; i++)
181  buffer[i] *= 1U << s->bitshift;
182  }
183 }
184 
186 {
187  int32_t mean = 0;
188  int chan, i;
189  int nblock = FFMAX(1, s->nmean);
190  /* initialise offset */
191  switch (s->internal_ftype) {
192  case TYPE_U8:
193  s->avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
194  mean = 0x80;
195  break;
196  case TYPE_S16HL:
197  case TYPE_S16LH:
198  s->avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
199  break;
200  default:
201  av_log(s->avctx, AV_LOG_ERROR, "unknown audio type\n");
202  return AVERROR_PATCHWELCOME;
203  }
204 
205  for (chan = 0; chan < s->channels; chan++)
206  for (i = 0; i < nblock; i++)
207  s->offset[chan][i] = mean;
208  return 0;
209 }
210 
211 static int decode_aiff_header(AVCodecContext *avctx, const uint8_t *header,
212  int header_size)
213 {
214  ShortenContext *s = avctx->priv_data;
215  int len, bps, exp;
216  GetByteContext gb;
217  uint64_t val;
218  uint32_t tag;
219 
220  bytestream2_init(&gb, header, header_size);
221 
222  if (bytestream2_get_le32(&gb) != MKTAG('F', 'O', 'R', 'M')) {
223  av_log(avctx, AV_LOG_ERROR, "missing FORM tag\n");
224  return AVERROR_INVALIDDATA;
225  }
226 
227  bytestream2_skip(&gb, 4); /* chunk size */
228 
229  tag = bytestream2_get_le32(&gb);
230  if (tag != MKTAG('A', 'I', 'F', 'F') &&
231  tag != MKTAG('A', 'I', 'F', 'C')) {
232  av_log(avctx, AV_LOG_ERROR, "missing AIFF tag\n");
233  return AVERROR_INVALIDDATA;
234  }
235 
236  while (bytestream2_get_le32(&gb) != MKTAG('C', 'O', 'M', 'M')) {
237  len = bytestream2_get_be32(&gb);
238  if (len < 0 || bytestream2_get_bytes_left(&gb) < 18LL + len + (len&1)) {
239  av_log(avctx, AV_LOG_ERROR, "no COMM chunk found\n");
240  return AVERROR_INVALIDDATA;
241  }
242  bytestream2_skip(&gb, len + (len & 1));
243  }
244  len = bytestream2_get_be32(&gb);
245 
246  if (len < 18) {
247  av_log(avctx, AV_LOG_ERROR, "COMM chunk was too short\n");
248  return AVERROR_INVALIDDATA;
249  }
250 
251  bytestream2_skip(&gb, 6);
252  bps = bytestream2_get_be16(&gb);
253  avctx->bits_per_coded_sample = bps;
254 
255  s->swap = tag == MKTAG('A', 'I', 'F', 'C');
256 
257  if (bps != 16 && bps != 8) {
258  av_log(avctx, AV_LOG_ERROR, "unsupported number of bits per sample: %d\n", bps);
259  return AVERROR(ENOSYS);
260  }
261 
262  exp = bytestream2_get_be16(&gb) - 16383 - 63;
263  val = bytestream2_get_be64(&gb);
264  if (exp < -63 || exp > 63) {
265  av_log(avctx, AV_LOG_ERROR, "exp %d is out of range\n", exp);
266  return AVERROR_INVALIDDATA;
267  }
268  if (exp >= 0)
269  avctx->sample_rate = val << exp;
270  else
271  avctx->sample_rate = (val + (1ULL<<(-exp-1))) >> -exp;
272  len -= 18;
273  if (len > 0)
274  av_log(avctx, AV_LOG_INFO, "%d header bytes unparsed\n", len);
275 
276  return 0;
277 }
278 
279 static int decode_wave_header(AVCodecContext *avctx, const uint8_t *header,
280  int header_size)
281 {
282  int len, bps;
283  short wave_format;
284  GetByteContext gb;
285 
286  bytestream2_init(&gb, header, header_size);
287 
288  if (bytestream2_get_le32(&gb) != MKTAG('R', 'I', 'F', 'F')) {
289  av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n");
290  return AVERROR_INVALIDDATA;
291  }
292 
293  bytestream2_skip(&gb, 4); /* chunk size */
294 
295  if (bytestream2_get_le32(&gb) != MKTAG('W', 'A', 'V', 'E')) {
296  av_log(avctx, AV_LOG_ERROR, "missing WAVE tag\n");
297  return AVERROR_INVALIDDATA;
298  }
299 
300  while (bytestream2_get_le32(&gb) != MKTAG('f', 'm', 't', ' ')) {
301  len = bytestream2_get_le32(&gb);
302  bytestream2_skip(&gb, len);
303  if (len < 0 || bytestream2_get_bytes_left(&gb) < 16) {
304  av_log(avctx, AV_LOG_ERROR, "no fmt chunk found\n");
305  return AVERROR_INVALIDDATA;
306  }
307  }
308  len = bytestream2_get_le32(&gb);
309 
310  if (len < 16) {
311  av_log(avctx, AV_LOG_ERROR, "fmt chunk was too short\n");
312  return AVERROR_INVALIDDATA;
313  }
314 
315  wave_format = bytestream2_get_le16(&gb);
316 
317  switch (wave_format) {
318  case WAVE_FORMAT_PCM:
319  break;
320  default:
321  av_log(avctx, AV_LOG_ERROR, "unsupported wave format\n");
322  return AVERROR(ENOSYS);
323  }
324 
325  bytestream2_skip(&gb, 2); // skip channels (already got from shorten header)
326  avctx->sample_rate = bytestream2_get_le32(&gb);
327  bytestream2_skip(&gb, 4); // skip bit rate (represents original uncompressed bit rate)
328  bytestream2_skip(&gb, 2); // skip block align (not needed)
329  bps = bytestream2_get_le16(&gb);
330  avctx->bits_per_coded_sample = bps;
331 
332  if (bps != 16 && bps != 8) {
333  av_log(avctx, AV_LOG_ERROR, "unsupported number of bits per sample: %d\n", bps);
334  return AVERROR(ENOSYS);
335  }
336 
337  len -= 16;
338  if (len > 0)
339  av_log(avctx, AV_LOG_INFO, "%d header bytes unparsed\n", len);
340 
341  return 0;
342 }
343 
344 static const int fixed_coeffs[][3] = {
345  { 0, 0, 0 },
346  { 1, 0, 0 },
347  { 2, -1, 0 },
348  { 3, -3, 1 }
349 };
350 
352  int residual_size, int32_t coffset)
353 {
354  int pred_order, sum, qshift, init_sum, i, j;
355  const int *coeffs;
356 
357  if (command == FN_QLPC) {
358  /* read/validate prediction order */
359  pred_order = get_ur_golomb_shorten(&s->gb, LPCQSIZE);
360  if ((unsigned)pred_order > s->nwrap) {
361  av_log(s->avctx, AV_LOG_ERROR, "invalid pred_order %d\n",
362  pred_order);
363  return AVERROR(EINVAL);
364  }
365  /* read LPC coefficients */
366  for (i = 0; i < pred_order; i++)
367  s->coeffs[i] = get_sr_golomb_shorten(&s->gb, LPCQUANT);
368  coeffs = s->coeffs;
369 
370  qshift = LPCQUANT;
371  } else {
372  /* fixed LPC coeffs */
373  pred_order = command;
374  if (pred_order >= FF_ARRAY_ELEMS(fixed_coeffs)) {
375  av_log(s->avctx, AV_LOG_ERROR, "invalid pred_order %d\n",
376  pred_order);
377  return AVERROR_INVALIDDATA;
378  }
379  coeffs = fixed_coeffs[pred_order];
380  qshift = 0;
381  }
382 
383  /* subtract offset from previous samples to use in prediction */
384  if (command == FN_QLPC && coffset)
385  for (i = -pred_order; i < 0; i++)
386  s->decoded[channel][i] -= (unsigned)coffset;
387 
388  /* decode residual and do LPC prediction */
389  init_sum = pred_order ? (command == FN_QLPC ? s->lpcqoffset : 0) : coffset;
390  for (i = 0; i < s->blocksize; i++) {
391  sum = init_sum;
392  for (j = 0; j < pred_order; j++)
393  sum += coeffs[j] * (unsigned)s->decoded[channel][i - j - 1];
394  s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) +
395  (unsigned)(sum >> qshift);
396  }
397 
398  /* add offset to current samples */
399  if (command == FN_QLPC && coffset)
400  for (i = 0; i < s->blocksize; i++)
401  s->decoded[channel][i] += (unsigned)coffset;
402 
403  return 0;
404 }
405 
407 {
408  int i, ret;
409  int maxnlpc = 0;
410  /* shorten signature */
411  if (get_bits_long(&s->gb, 32) != AV_RB32("ajkg")) {
412  av_log(s->avctx, AV_LOG_ERROR, "missing shorten magic 'ajkg'\n");
413  return AVERROR_INVALIDDATA;
414  }
415 
416  s->lpcqoffset = 0;
417  s->blocksize = DEFAULT_BLOCK_SIZE;
418  s->nmean = -1;
419  s->version = get_bits(&s->gb, 8);
420  s->internal_ftype = get_uint(s, TYPESIZE);
421 
422  s->channels = get_uint(s, CHANSIZE);
423  if (!s->channels) {
424  av_log(s->avctx, AV_LOG_ERROR, "No channels reported\n");
425  return AVERROR_INVALIDDATA;
426  }
427  if (s->channels > MAX_CHANNELS) {
428  av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
429  s->channels = 0;
430  return AVERROR_INVALIDDATA;
431  }
432  if (s->avctx->ch_layout.nb_channels != s->channels) {
433  av_channel_layout_uninit(&s->avctx->ch_layout);
434  s->avctx->ch_layout.nb_channels = s->channels;
435  s->avctx->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
436  }
437 
438  /* get blocksize if version > 0 */
439  if (s->version > 0) {
440  int skip_bytes;
441  unsigned blocksize;
442 
443  blocksize = get_uint(s, av_log2(DEFAULT_BLOCK_SIZE));
444  if (!blocksize || blocksize > MAX_BLOCKSIZE) {
445  av_log(s->avctx, AV_LOG_ERROR,
446  "invalid or unsupported block size: %d\n",
447  blocksize);
448  return AVERROR(EINVAL);
449  }
450  s->blocksize = blocksize;
451 
452  maxnlpc = get_uint(s, LPCQSIZE);
453  if (maxnlpc > 1024U) {
454  av_log(s->avctx, AV_LOG_ERROR, "maxnlpc is: %d\n", maxnlpc);
455  return AVERROR_INVALIDDATA;
456  }
457  s->nmean = get_uint(s, 0);
458  if (s->nmean > 32768U) {
459  av_log(s->avctx, AV_LOG_ERROR, "nmean is: %d\n", s->nmean);
460  return AVERROR_INVALIDDATA;
461  }
462 
464  if ((unsigned)skip_bytes > FFMAX(get_bits_left(&s->gb), 0)/8) {
465  av_log(s->avctx, AV_LOG_ERROR, "invalid skip_bytes: %d\n", skip_bytes);
466  return AVERROR_INVALIDDATA;
467  }
468 
469  for (i = 0; i < skip_bytes; i++)
470  skip_bits(&s->gb, 8);
471  }
472  s->nwrap = FFMAX(NWRAP, maxnlpc);
473 
474  if (s->version > 1)
475  s->lpcqoffset = V2LPCQOFFSET;
476 
477  if (s->avctx->extradata_size > 0)
478  goto end;
479 
480  if (get_ur_golomb_shorten(&s->gb, FNSIZE) != FN_VERBATIM) {
481  av_log(s->avctx, AV_LOG_ERROR,
482  "missing verbatim section at beginning of stream\n");
483  return AVERROR_INVALIDDATA;
484  }
485 
486  s->header_size = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
487  if (s->header_size >= OUT_BUFFER_SIZE ||
488  s->header_size < CANONICAL_HEADER_SIZE) {
489  av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n",
490  s->header_size);
491  return AVERROR_INVALIDDATA;
492  }
493 
494  for (i = 0; i < s->header_size; i++)
495  s->header[i] = (char)get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
496 
497  if (AV_RL32(s->header) == MKTAG('R','I','F','F')) {
498  if ((ret = decode_wave_header(s->avctx, s->header, s->header_size)) < 0)
499  return ret;
500  } else if (AV_RL32(s->header) == MKTAG('F','O','R','M')) {
501  if ((ret = decode_aiff_header(s->avctx, s->header, s->header_size)) < 0)
502  return ret;
503  } else {
504  avpriv_report_missing_feature(s->avctx, "unsupported bit packing %"
505  PRIX32, AV_RL32(s->header));
506  return AVERROR_PATCHWELCOME;
507  }
508 
509 end:
510 
511  if ((ret = allocate_buffers(s)) < 0)
512  return ret;
513 
514  if ((ret = init_offset(s)) < 0)
515  return ret;
516 
517  s->cur_chan = 0;
518  s->bitshift = 0;
519 
520  s->got_header = 1;
521 
522  return 0;
523 }
524 
526  int *got_frame_ptr, AVPacket *avpkt)
527 {
528  const uint8_t *buf = avpkt->data;
529  int buf_size = avpkt->size;
530  ShortenContext *s = avctx->priv_data;
531  int i, input_buf_size = 0;
532  int ret;
533 
534  /* allocate internal bitstream buffer */
535  if (s->max_framesize == 0) {
536  void *tmp_ptr;
537  s->max_framesize = 8192; // should hopefully be enough for the first header
538  tmp_ptr = av_fast_realloc(s->bitstream, &s->allocated_bitstream_size,
539  s->max_framesize + AV_INPUT_BUFFER_PADDING_SIZE);
540  if (!tmp_ptr) {
541  s->max_framesize = 0;
542  av_log(avctx, AV_LOG_ERROR, "error allocating bitstream buffer\n");
543  return AVERROR(ENOMEM);
544  }
545  memset(tmp_ptr, 0, s->allocated_bitstream_size);
546  s->bitstream = tmp_ptr;
547  }
548 
549  /* append current packet data to bitstream buffer */
550  buf_size = FFMIN(buf_size, s->max_framesize - s->bitstream_size);
551  input_buf_size = buf_size;
552 
553  if (s->bitstream_index + s->bitstream_size + buf_size + AV_INPUT_BUFFER_PADDING_SIZE >
554  s->allocated_bitstream_size) {
555  memmove(s->bitstream, &s->bitstream[s->bitstream_index],
556  s->bitstream_size);
557  s->bitstream_index = 0;
558  }
559  if (buf)
560  memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf,
561  buf_size);
562  buf = &s->bitstream[s->bitstream_index];
563  buf_size += s->bitstream_size;
564  s->bitstream_size = buf_size;
565 
566  /* do not decode until buffer has at least max_framesize bytes or
567  * the end of the file has been reached */
568  if (buf_size < s->max_framesize && avpkt->data) {
569  *got_frame_ptr = 0;
570  return input_buf_size;
571  }
572  /* init and position bitstream reader */
573  if ((ret = init_get_bits8(&s->gb, buf, buf_size)) < 0)
574  return ret;
575  skip_bits(&s->gb, s->bitindex);
576 
577  /* process header or next subblock */
578  if (!s->got_header) {
579 
580  if ((ret = read_header(s)) < 0)
581  return ret;
582 
583  if (avpkt->size) {
584  int max_framesize;
585  void *tmp_ptr;
586 
587  max_framesize = FFMAX(s->max_framesize, s->blocksize * s->channels * 8);
588  tmp_ptr = av_fast_realloc(s->bitstream, &s->allocated_bitstream_size,
589  max_framesize + AV_INPUT_BUFFER_PADDING_SIZE);
590  if (!tmp_ptr) {
591  av_log(avctx, AV_LOG_ERROR, "error allocating bitstream buffer\n");
592  return AVERROR(ENOMEM);
593  }
594  s->bitstream = tmp_ptr;
595  s->max_framesize = max_framesize;
596  *got_frame_ptr = 0;
597  goto finish_frame;
598  }
599  }
600 
601  /* if quit command was read previously, don't decode anything */
602  if (s->got_quit_command) {
603  *got_frame_ptr = 0;
604  return avpkt->size;
605  }
606 
607  s->cur_chan = 0;
608  while (s->cur_chan < s->channels) {
609  unsigned cmd;
610  int len;
611 
612  if (get_bits_left(&s->gb) < 3 + FNSIZE) {
613  *got_frame_ptr = 0;
614  break;
615  }
616 
617  cmd = get_ur_golomb_shorten(&s->gb, FNSIZE);
618 
619  if (cmd > FN_VERBATIM) {
620  av_log(avctx, AV_LOG_ERROR, "unknown shorten function %d\n", cmd);
621  *got_frame_ptr = 0;
622  break;
623  }
624 
625  if (!is_audio_command[cmd]) {
626  /* process non-audio command */
627  switch (cmd) {
628  case FN_VERBATIM:
630  if (len < 0 || len > get_bits_left(&s->gb)) {
631  av_log(avctx, AV_LOG_ERROR, "verbatim length %d invalid\n",
632  len);
633  return AVERROR_INVALIDDATA;
634  }
635  while (len--)
637  break;
638  case FN_BITSHIFT: {
639  unsigned bitshift = get_ur_golomb_shorten(&s->gb, BITSHIFTSIZE);
640  if (bitshift > 32) {
641  av_log(avctx, AV_LOG_ERROR, "bitshift %d is invalid\n",
642  bitshift);
643  return AVERROR_INVALIDDATA;
644  }
645  s->bitshift = bitshift;
646  break;
647  }
648  case FN_BLOCKSIZE: {
649  unsigned blocksize = get_uint(s, av_log2(s->blocksize));
650  if (blocksize > s->blocksize) {
652  "Increasing block size");
653  return AVERROR_PATCHWELCOME;
654  }
655  if (!blocksize || blocksize > MAX_BLOCKSIZE) {
656  av_log(avctx, AV_LOG_ERROR, "invalid or unsupported "
657  "block size: %d\n", blocksize);
658  return AVERROR(EINVAL);
659  }
660  s->blocksize = blocksize;
661  break;
662  }
663  case FN_QUIT:
664  s->got_quit_command = 1;
665  break;
666  }
667  if (cmd == FN_QUIT)
668  break;
669  } else {
670  /* process audio command */
671  int residual_size = 0;
672  int channel = s->cur_chan;
673  int32_t coffset;
674 
675  /* get Rice code for residual decoding */
676  if (cmd != FN_ZERO) {
677  residual_size = get_ur_golomb_shorten(&s->gb, ENERGYSIZE);
678  /* This is a hack as version 0 differed in the definition
679  * of get_sr_golomb_shorten(). */
680  if (s->version == 0)
681  residual_size--;
682  if (residual_size > 30U) {
683  av_log(avctx, AV_LOG_ERROR, "residual size unsupportd: %d\n", residual_size);
684  return AVERROR_INVALIDDATA;
685  }
686  }
687 
688  /* calculate sample offset using means from previous blocks */
689  if (s->nmean == 0)
690  coffset = s->offset[channel][0];
691  else {
692  int32_t sum = (s->version < 2) ? 0 : s->nmean / 2;
693  for (i = 0; i < s->nmean; i++)
694  sum += (unsigned)s->offset[channel][i];
695  coffset = sum / s->nmean;
696  if (s->version >= 2)
697  coffset = s->bitshift == 0 ? coffset : coffset >> s->bitshift - 1 >> 1;
698  }
699 
700  /* decode samples for this channel */
701  if (cmd == FN_ZERO) {
702  for (i = 0; i < s->blocksize; i++)
703  s->decoded[channel][i] = 0;
704  } else {
705  if ((ret = decode_subframe_lpc(s, cmd, channel,
706  residual_size, coffset)) < 0)
707  return ret;
708  }
709 
710  /* update means with info from the current block */
711  if (s->nmean > 0) {
712  int64_t sum = (s->version < 2) ? 0 : s->blocksize / 2;
713  for (i = 0; i < s->blocksize; i++)
714  sum += s->decoded[channel][i];
715 
716  for (i = 1; i < s->nmean; i++)
717  s->offset[channel][i - 1] = s->offset[channel][i];
718 
719  if (s->version < 2)
720  s->offset[channel][s->nmean - 1] = sum / s->blocksize;
721  else
722  s->offset[channel][s->nmean - 1] = s->bitshift == 32 ? 0 : (sum / s->blocksize) * (1LL << s->bitshift);
723  }
724 
725  /* copy wrap samples for use with next block */
726  for (i = -s->nwrap; i < 0; i++)
727  s->decoded[channel][i] = s->decoded[channel][i + s->blocksize];
728 
729  /* shift samples to add in unused zero bits which were removed
730  * during encoding */
731  fix_bitshift(s, s->decoded[channel]);
732 
733  /* if this is the last channel in the block, output the samples */
734  s->cur_chan++;
735  if (s->cur_chan == s->channels) {
736  uint8_t *samples_u8;
737  int16_t *samples_s16;
738  int chan;
739 
740  /* get output buffer */
741  frame->nb_samples = s->blocksize;
742  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
743  return ret;
744 
745  for (chan = 0; chan < s->channels; chan++) {
746  samples_u8 = ((uint8_t **)frame->extended_data)[chan];
747  samples_s16 = ((int16_t **)frame->extended_data)[chan];
748  for (i = 0; i < s->blocksize; i++) {
749  switch (s->internal_ftype) {
750  case TYPE_U8:
751  *samples_u8++ = av_clip_uint8(s->decoded[chan][i]);
752  break;
753  case TYPE_S16HL:
754  case TYPE_S16LH:
755  *samples_s16++ = av_clip_int16(s->decoded[chan][i]);
756  break;
757  }
758  }
759  if (s->swap && s->internal_ftype != TYPE_U8)
760  s->bdsp.bswap16_buf(((uint16_t **)frame->extended_data)[chan],
761  ((uint16_t **)frame->extended_data)[chan],
762  s->blocksize);
763 
764  }
765 
766  *got_frame_ptr = 1;
767  }
768  }
769  }
770  if (s->cur_chan < s->channels)
771  *got_frame_ptr = 0;
772 
774  s->bitindex = get_bits_count(&s->gb) - 8 * (get_bits_count(&s->gb) / 8);
775  i = get_bits_count(&s->gb) / 8;
776  if (i > buf_size) {
777  av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
778  s->bitstream_size = 0;
779  s->bitstream_index = 0;
780  return AVERROR_INVALIDDATA;
781  }
782  if (s->bitstream_size) {
783  s->bitstream_index += i;
784  s->bitstream_size -= i;
785  return input_buf_size;
786  } else
787  return i;
788 }
789 
791 {
792  ShortenContext *s = avctx->priv_data;
793  int i;
794 
795  for (i = 0; i < s->channels; i++) {
796  s->decoded[i] = NULL;
797  av_freep(&s->decoded_base[i]);
798  av_freep(&s->offset[i]);
799  }
800  av_freep(&s->bitstream);
801  av_freep(&s->coeffs);
802 
803  return 0;
804 }
805 
807  .p.name = "shorten",
808  .p.long_name = NULL_IF_CONFIG_SMALL("Shorten"),
809  .p.type = AVMEDIA_TYPE_AUDIO,
810  .p.id = AV_CODEC_ID_SHORTEN,
811  .priv_data_size = sizeof(ShortenContext),
813  .close = shorten_decode_close,
815  .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF |
819  .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
822  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
823 };
ShortenContext::gb
GetBitContext gb
Definition: shorten.c:88
bswapdsp.h
TYPE_S16LH
#define TYPE_S16LH
Definition: shorten.c:58
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:839
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
shorten_decode_init
static av_cold int shorten_decode_init(AVCodecContext *avctx)
Definition: shorten.c:118
ShortenContext::bitindex
int bitindex
Definition: shorten.c:110
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:998
GetByteContext
Definition: bytestream.h:33
get_bits_long
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:546
ENERGYSIZE
#define ENERGYSIZE
Definition: shorten.c:51
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:219
shorten_decode_frame
static int shorten_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition: shorten.c:525
is_audio_command
static const uint8_t is_audio_command[10]
indicates if the FN_* command is audio or non-audio
Definition: shorten.c:80
V2LPCQOFFSET
#define V2LPCQOFFSET
Definition: shorten.c:65
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
ShortenContext
Definition: shorten.c:86
get_ur_golomb_shorten
static unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k)
read unsigned golomb rice code (shorten).
Definition: golomb.h:549
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:374
OUT_BUFFER_SIZE
#define OUT_BUFFER_SIZE
Definition: shorten.c:40
NSKIPSIZE
#define NSKIPSIZE
Definition: shorten.c:62
init_offset
static int init_offset(ShortenContext *s)
Definition: shorten.c:185
FN_BITSHIFT
#define FN_BITSHIFT
Definition: shorten.c:74
TYPE_U8
#define TYPE_U8
Definition: shorten.c:55
LPCQSIZE
#define LPCQSIZE
Definition: shorten.c:50
FFCodec
Definition: codec_internal.h:112
ShortenContext::offset
int32_t * offset[MAX_CHANNELS]
Definition: shorten.c:95
ShortenContext::max_framesize
int max_framesize
Definition: shorten.c:90
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
ShortenContext::header
uint8_t header[OUT_BUFFER_SIZE]
Definition: shorten.c:102
init
static int init
Definition: av_tx.c:47
ShortenContext::header_size
int header_size
Definition: shorten.c:101
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:467
golomb.h
exp golomb vlc stuff
bytestream2_skip
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:116
U
#define U(x)
Definition: vp56_arith.h:37
ShortenContext::nmean
int nmean
Definition: shorten.c:106
FN_QLPC
#define FN_QLPC
Definition: shorten.c:75
fixed_coeffs
static const int fixed_coeffs[][3]
Definition: shorten.c:344
GetBitContext
Definition: get_bits.h:61
val
static double val(void *priv, double ch)
Definition: aeval.c:77
BITSHIFTSIZE
#define BITSHIFTSIZE
Definition: shorten.c:52
ShortenContext::bitshift
int bitshift
Definition: shorten.c:105
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:667
ShortenContext::bitstream
uint8_t * bitstream
Definition: shorten.c:97
decode_wave_header
static int decode_wave_header(AVCodecContext *avctx, const uint8_t *header, int header_size)
Definition: shorten.c:279
av_fast_realloc
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
Definition: mem.c:505
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:254
allocate_buffers
static int allocate_buffers(ShortenContext *s)
Definition: shorten.c:128
s
#define s(width, name)
Definition: cbs_vp9.c:256
AV_CHANNEL_ORDER_UNSPEC
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
Definition: channel_layout.h:106
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
ShortenContext::blocksize
int blocksize
Definition: shorten.c:109
get_bits.h
limits.h
VERBATIM_BYTE_SIZE
#define VERBATIM_BYTE_SIZE
Definition: shorten.c:83
finish_frame
static int finish_frame(AVCodecContext *avctx, AVFrame *pict)
Definition: rv34.c:1559
command
static int command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Definition: vf_drawtext.c:907
if
if(ret)
Definition: filter_design.txt:179
ff_bswapdsp_init
av_cold void ff_bswapdsp_init(BswapDSPContext *c)
Definition: bswapdsp.c:49
av_clip_int16
#define av_clip_int16
Definition: common.h:110
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
ShortenContext::bitstream_index
int bitstream_index
Definition: shorten.c:99
FNSIZE
#define FNSIZE
Definition: shorten.c:67
ShortenContext::bdsp
BswapDSPContext bdsp
Definition: shorten.c:115
ff_shorten_decoder
const FFCodec ff_shorten_decoder
Definition: shorten.c:806
FN_VERBATIM
#define FN_VERBATIM
Definition: shorten.c:77
exp
int8_t exp
Definition: eval.c:72
ShortenContext::bitstream_size
int bitstream_size
Definition: shorten.c:98
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
av_channel_layout_uninit
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
Definition: channel_layout.c:630
AV_CODEC_CAP_CHANNEL_CONF
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition: codec.h:109
ShortenContext::nwrap
int nwrap
Definition: shorten.c:108
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1403
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:375
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
AV_SAMPLE_FMT_U8P
@ AV_SAMPLE_FMT_U8P
unsigned 8 bits, planar
Definition: samplefmt.h:63
codec_internal.h
ShortenContext::version
int version
Definition: shorten.c:103
ShortenContext::decoded
int32_t * decoded[MAX_CHANNELS]
Definition: shorten.c:93
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
ShortenContext::internal_ftype
int internal_ftype
Definition: shorten.c:107
bps
unsigned bps
Definition: movenc.c:1647
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
CANONICAL_HEADER_SIZE
#define CANONICAL_HEADER_SIZE
Definition: shorten.c:84
MAX_BLOCKSIZE
#define MAX_BLOCKSIZE
Definition: shorten.c:38
AV_RB32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:96
decode_aiff_header
static int decode_aiff_header(AVCodecContext *avctx, const uint8_t *header, int header_size)
Definition: shorten.c:211
avpriv_report_missing_feature
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
ShortenContext::got_quit_command
int got_quit_command
Definition: shorten.c:113
FN_BLOCKSIZE
#define FN_BLOCKSIZE
Definition: shorten.c:73
header
static const uint8_t header[24]
Definition: sdr2.c:67
LPCQUANT
#define LPCQUANT
Definition: shorten.c:64
av_reallocp_array
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Allocate, reallocate an array through a pointer to a pointer.
Definition: mem.c:233
NWRAP
#define NWRAP
Definition: shorten.c:61
AV_SAMPLE_FMT_S16P
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition: samplefmt.h:64
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
fix_bitshift
static void fix_bitshift(ShortenContext *s, int32_t *buffer)
Definition: shorten.c:172
AVCodecContext::bits_per_coded_sample
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1441
ShortenContext::decoded_base
int32_t * decoded_base[MAX_CHANNELS]
Definition: shorten.c:94
TYPESIZE
#define TYPESIZE
Definition: shorten.c:48
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
WAVE_FORMAT_PCM
#define WAVE_FORMAT_PCM
Definition: shorten.c:44
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
FN_QUIT
#define FN_QUIT
Definition: shorten.c:72
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
FF_CODEC_CAP_INIT_THREADSAFE
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: codec_internal.h:31
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:203
len
int len
Definition: vorbis_enc_data.h:426
get_sr_golomb_shorten
static int get_sr_golomb_shorten(GetBitContext *gb, int k)
read signed golomb rice code (shorten).
Definition: golomb.h:557
shorten_decode_close
static av_cold int shorten_decode_close(AVCodecContext *avctx)
Definition: shorten.c:790
ShortenContext::cur_chan
int cur_chan
Definition: shorten.c:104
MAX_CHANNELS
#define MAX_CHANNELS
Definition: shorten.c:37
avcodec.h
tag
uint32_t tag
Definition: movenc.c:1646
ret
ret
Definition: filter_design.txt:187
CHANSIZE
#define CHANSIZE
Definition: shorten.c:49
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
ShortenContext::allocated_bitstream_size
unsigned int allocated_bitstream_size
Definition: shorten.c:100
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
decode_subframe_lpc
static int decode_subframe_lpc(ShortenContext *s, int command, int channel, int residual_size, int32_t coffset)
Definition: shorten.c:351
AVCodecContext
main external API structure.
Definition: avcodec.h:389
ShortenContext::avctx
AVCodecContext * avctx
Definition: shorten.c:87
ShortenContext::lpcqoffset
int32_t lpcqoffset
Definition: shorten.c:111
DEFAULT_BLOCK_SIZE
#define DEFAULT_BLOCK_SIZE
Definition: shorten.c:46
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
TYPE_S16HL
#define TYPE_S16HL
Definition: shorten.c:56
ShortenContext::channels
unsigned channels
Definition: shorten.c:91
AV_CODEC_CAP_DELAY
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: codec.h:82
mean
static float mean(const float *input, int size)
Definition: vf_nnedi.c:857
av_clip_uint8
#define av_clip_uint8
Definition: common.h:101
ShortenContext::swap
int swap
Definition: shorten.c:114
AV_CODEC_CAP_SUBFRAMES
#define AV_CODEC_CAP_SUBFRAMES
Codec can output multiple frames per AVPacket Normally demuxers return one frame at a time,...
Definition: codec.h:100
read_header
static int read_header(ShortenContext *s)
Definition: shorten.c:406
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:416
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
get_uint
static unsigned int get_uint(ShortenContext *s, int k)
Definition: shorten.c:162
FN_ZERO
#define FN_ZERO
Definition: shorten.c:76
int32_t
int32_t
Definition: audioconvert.c:56
bytestream.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
VERBATIM_CKSIZE_SIZE
#define VERBATIM_CKSIZE_SIZE
Definition: shorten.c:82
ShortenContext::coeffs
int * coeffs
Definition: shorten.c:96
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
skip_bytes
static const av_unused uint8_t * skip_bytes(CABACContext *c, int n)
Skip n bytes and reset the decoder.
Definition: cabac_functions.h:203
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
BswapDSPContext
Definition: bswapdsp.h:24
ULONGSIZE
#define ULONGSIZE
Definition: shorten.c:42
ShortenContext::min_framesize
int min_framesize
Definition: shorten.c:90
ShortenContext::got_header
int got_header
Definition: shorten.c:112
AV_CODEC_ID_SHORTEN
@ AV_CODEC_ID_SHORTEN
Definition: codec_id.h:442
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
channel
channel
Definition: ebur128.h:39