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