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