Libav
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
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_S16HL 3
53 #define TYPE_S16LH 5
54 
55 #define NWRAP 3
56 #define NSKIPSIZE 1
57 
58 #define LPCQUANT 5
59 #define V2LPCQOFFSET (1 << LPCQUANT)
60 
61 #define FNSIZE 2
62 #define FN_DIFF0 0
63 #define FN_DIFF1 1
64 #define FN_DIFF2 2
65 #define FN_DIFF3 3
66 #define FN_QUIT 4
67 #define FN_BLOCKSIZE 5
68 #define FN_BITSHIFT 6
69 #define FN_QLPC 7
70 #define FN_ZERO 8
71 #define FN_VERBATIM 9
72 
74 static const uint8_t is_audio_command[10] = { 1, 1, 1, 1, 0, 0, 0, 1, 1, 0 };
75 
76 #define VERBATIM_CKSIZE_SIZE 5
77 #define VERBATIM_BYTE_SIZE 8
78 #define CANONICAL_HEADER_SIZE 44
79 
80 typedef struct ShortenContext {
83 
85  unsigned channels;
86 
90  int *coeffs;
97  int version;
98  int cur_chan;
99  int bitshift;
100  int nmean;
102  int nwrap;
104  int bitindex;
109 
111 {
112  ShortenContext *s = avctx->priv_data;
113  s->avctx = avctx;
115 
116  return 0;
117 }
118 
120 {
121  int i, chan, err;
122 
123  for (chan = 0; chan < s->channels; chan++) {
124  if (FFMAX(1, s->nmean) >= UINT_MAX / sizeof(int32_t)) {
125  av_log(s->avctx, AV_LOG_ERROR, "nmean too large\n");
126  return AVERROR_INVALIDDATA;
127  }
128  if (s->blocksize + s->nwrap >= UINT_MAX / sizeof(int32_t) ||
129  s->blocksize + s->nwrap <= (unsigned)s->nwrap) {
131  "s->blocksize + s->nwrap too large\n");
132  return AVERROR_INVALIDDATA;
133  }
134 
135  if ((err = av_reallocp(&s->offset[chan],
136  sizeof(int32_t) *
137  FFMAX(1, s->nmean))) < 0)
138  return err;
139 
140  if ((err = av_reallocp(&s->decoded_base[chan], (s->blocksize + s->nwrap) *
141  sizeof(s->decoded_base[0][0]))) < 0)
142  return err;
143  for (i = 0; i < s->nwrap; i++)
144  s->decoded_base[chan][i] = 0;
145  s->decoded[chan] = s->decoded_base[chan] + s->nwrap;
146  }
147 
148  if ((err = av_reallocp(&s->coeffs, s->nwrap * sizeof(*s->coeffs))) < 0)
149  return err;
150 
151  return 0;
152 }
153 
154 static inline unsigned int get_uint(ShortenContext *s, int k)
155 {
156  if (s->version != 0)
158  return get_ur_golomb_shorten(&s->gb, k);
159 }
160 
162 {
163  int i;
164 
165  if (s->bitshift != 0)
166  for (i = 0; i < s->blocksize; i++)
167  buffer[i] <<= s->bitshift;
168 }
169 
171 {
172  int32_t mean = 0;
173  int chan, i;
174  int nblock = FFMAX(1, s->nmean);
175  /* initialise offset */
176  switch (s->internal_ftype) {
177  case TYPE_S16HL:
178  case TYPE_S16LH:
179  mean = 0;
180  break;
181  default:
182  av_log(s->avctx, AV_LOG_ERROR, "unknown audio type");
183  return AVERROR_INVALIDDATA;
184  }
185 
186  for (chan = 0; chan < s->channels; chan++)
187  for (i = 0; i < nblock; i++)
188  s->offset[chan][i] = mean;
189  return 0;
190 }
191 
193  int header_size)
194 {
195  int len;
196  short wave_format;
198 
199  bytestream2_init(&gb, header, header_size);
200 
201  if (bytestream2_get_le32(&gb) != MKTAG('R', 'I', 'F', 'F')) {
202  av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n");
203  return AVERROR_INVALIDDATA;
204  }
205 
206  bytestream2_skip(&gb, 4); /* chunk size */
207 
208  if (bytestream2_get_le32(&gb) != MKTAG('W', 'A', 'V', 'E')) {
209  av_log(avctx, AV_LOG_ERROR, "missing WAVE tag\n");
210  return AVERROR_INVALIDDATA;
211  }
212 
213  while (bytestream2_get_le32(&gb) != MKTAG('f', 'm', 't', ' ')) {
214  len = bytestream2_get_le32(&gb);
215  bytestream2_skip(&gb, len);
216  if (bytestream2_get_bytes_left(&gb) < 16) {
217  av_log(avctx, AV_LOG_ERROR, "no fmt chunk found\n");
218  return AVERROR_INVALIDDATA;
219  }
220  }
221  len = bytestream2_get_le32(&gb);
222 
223  if (len < 16) {
224  av_log(avctx, AV_LOG_ERROR, "fmt chunk was too short\n");
225  return AVERROR_INVALIDDATA;
226  }
227 
228  wave_format = bytestream2_get_le16(&gb);
229 
230  switch (wave_format) {
231  case WAVE_FORMAT_PCM:
232  break;
233  default:
234  av_log(avctx, AV_LOG_ERROR, "unsupported wave format\n");
235  return AVERROR(ENOSYS);
236  }
237 
238  bytestream2_skip(&gb, 2); // skip channels (already got from shorten header)
239  avctx->sample_rate = bytestream2_get_le32(&gb);
240  bytestream2_skip(&gb, 4); // skip bit rate (represents original uncompressed bit rate)
241  bytestream2_skip(&gb, 2); // skip block align (not needed)
242  avctx->bits_per_coded_sample = bytestream2_get_le16(&gb);
243 
244  if (avctx->bits_per_coded_sample != 16) {
245  av_log(avctx, AV_LOG_ERROR, "unsupported number of bits per sample\n");
246  return AVERROR(ENOSYS);
247  }
248 
249  len -= 16;
250  if (len > 0)
251  av_log(avctx, AV_LOG_INFO, "%d header bytes unparsed\n", len);
252 
253  return 0;
254 }
255 
256 static void output_buffer(int16_t **samples, int nchan, int blocksize,
257  int32_t **buffer)
258 {
259  int i, ch;
260  for (ch = 0; ch < nchan; ch++) {
261  int32_t *in = buffer[ch];
262  int16_t *out = samples[ch];
263  for (i = 0; i < blocksize; i++)
264  out[i] = av_clip_int16(in[i]);
265  }
266 }
267 
268 static const int fixed_coeffs[][3] = {
269  { 0, 0, 0 },
270  { 1, 0, 0 },
271  { 2, -1, 0 },
272  { 3, -3, 1 }
273 };
274 
275 static int decode_subframe_lpc(ShortenContext *s, int command, int channel,
276  int residual_size, int32_t coffset)
277 {
278  int pred_order, sum, qshift, init_sum, i, j;
279  const int *coeffs;
280 
281  if (command == FN_QLPC) {
282  /* read/validate prediction order */
283  pred_order = get_ur_golomb_shorten(&s->gb, LPCQSIZE);
284  if (pred_order > s->nwrap) {
285  av_log(s->avctx, AV_LOG_ERROR, "invalid pred_order %d\n",
286  pred_order);
287  return AVERROR(EINVAL);
288  }
289  /* read LPC coefficients */
290  for (i = 0; i < pred_order; i++)
291  s->coeffs[i] = get_sr_golomb_shorten(&s->gb, LPCQUANT);
292  coeffs = s->coeffs;
293 
294  qshift = LPCQUANT;
295  } else {
296  /* fixed LPC coeffs */
297  pred_order = command;
298  if (pred_order >= FF_ARRAY_ELEMS(fixed_coeffs)) {
299  av_log(s->avctx, AV_LOG_ERROR, "invalid pred_order %d\n",
300  pred_order);
301  return AVERROR_INVALIDDATA;
302  }
303  coeffs = fixed_coeffs[pred_order];
304  qshift = 0;
305  }
306 
307  /* subtract offset from previous samples to use in prediction */
308  if (command == FN_QLPC && coffset)
309  for (i = -pred_order; i < 0; i++)
310  s->decoded[channel][i] -= coffset;
311 
312  /* decode residual and do LPC prediction */
313  init_sum = pred_order ? (command == FN_QLPC ? s->lpcqoffset : 0) : coffset;
314  for (i = 0; i < s->blocksize; i++) {
315  sum = init_sum;
316  for (j = 0; j < pred_order; j++)
317  sum += coeffs[j] * s->decoded[channel][i - j - 1];
318  s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) +
319  (sum >> qshift);
320  }
321 
322  /* add offset to current samples */
323  if (command == FN_QLPC && coffset)
324  for (i = 0; i < s->blocksize; i++)
325  s->decoded[channel][i] += coffset;
326 
327  return 0;
328 }
329 
331 {
332  int i, ret;
333  int maxnlpc = 0;
334  /* shorten signature */
335  if (get_bits_long(&s->gb, 32) != AV_RB32("ajkg")) {
336  av_log(s->avctx, AV_LOG_ERROR, "missing shorten magic 'ajkg'\n");
337  return AVERROR_INVALIDDATA;
338  }
339 
340  s->lpcqoffset = 0;
342  s->nmean = -1;
343  s->version = get_bits(&s->gb, 8);
345 
346  s->channels = get_uint(s, CHANSIZE);
347  if (!s->channels) {
348  av_log(s->avctx, AV_LOG_ERROR, "No channels reported\n");
349  return AVERROR_INVALIDDATA;
350  }
351  if (s->channels > MAX_CHANNELS) {
352  av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
353  s->channels = 0;
354  return AVERROR_INVALIDDATA;
355  }
356  s->avctx->channels = s->channels;
357 
358  /* get blocksize if version > 0 */
359  if (s->version > 0) {
360  int skip_bytes;
361  unsigned blocksize;
362 
363  blocksize = get_uint(s, av_log2(DEFAULT_BLOCK_SIZE));
364  if (!blocksize || blocksize > MAX_BLOCKSIZE) {
366  "invalid or unsupported block size: %d\n",
367  blocksize);
368  return AVERROR(EINVAL);
369  }
370  s->blocksize = blocksize;
371 
372  maxnlpc = get_uint(s, LPCQSIZE);
373  s->nmean = get_uint(s, 0);
374 
375  skip_bytes = get_uint(s, NSKIPSIZE);
376  for (i = 0; i < skip_bytes; i++)
377  skip_bits(&s->gb, 8);
378  }
379  s->nwrap = FFMAX(NWRAP, maxnlpc);
380 
381  if ((ret = allocate_buffers(s)) < 0)
382  return ret;
383 
384  if ((ret = init_offset(s)) < 0)
385  return ret;
386 
387  if (s->version > 1)
389 
392  "missing verbatim section at beginning of stream\n");
393  return AVERROR_INVALIDDATA;
394  }
395 
397  if (s->header_size >= OUT_BUFFER_SIZE ||
399  av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n",
400  s->header_size);
401  return AVERROR_INVALIDDATA;
402  }
403 
404  for (i = 0; i < s->header_size; i++)
406 
407  if ((ret = decode_wave_header(s->avctx, s->header, s->header_size)) < 0)
408  return ret;
409 
410  s->cur_chan = 0;
411  s->bitshift = 0;
412 
413  s->got_header = 1;
414 
415  return 0;
416 }
417 
419  int *got_frame_ptr, AVPacket *avpkt)
420 {
421  AVFrame *frame = data;
422  const uint8_t *buf = avpkt->data;
423  int buf_size = avpkt->size;
424  ShortenContext *s = avctx->priv_data;
425  int i, input_buf_size = 0;
426  int ret;
427 
428  /* allocate internal bitstream buffer */
429  if (s->max_framesize == 0) {
430  void *tmp_ptr;
431  s->max_framesize = 1024; // should hopefully be enough for the first header
434  if (!tmp_ptr) {
435  av_log(avctx, AV_LOG_ERROR, "error allocating bitstream buffer\n");
436  return AVERROR(ENOMEM);
437  }
438  s->bitstream = tmp_ptr;
439  }
440 
441  /* append current packet data to bitstream buffer */
442  if (1 && s->max_framesize) { //FIXME truncated
443  buf_size = FFMIN(buf_size, s->max_framesize - s->bitstream_size);
444  input_buf_size = buf_size;
445 
446  if (s->bitstream_index + s->bitstream_size + buf_size >
448  memmove(s->bitstream, &s->bitstream[s->bitstream_index],
449  s->bitstream_size);
450  s->bitstream_index = 0;
451  }
452  if (buf)
453  memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf,
454  buf_size);
455  buf = &s->bitstream[s->bitstream_index];
456  buf_size += s->bitstream_size;
457  s->bitstream_size = buf_size;
458 
459  /* do not decode until buffer has at least max_framesize bytes or
460  * the end of the file has been reached */
461  if (buf_size < s->max_framesize && avpkt->data) {
462  *got_frame_ptr = 0;
463  return input_buf_size;
464  }
465  }
466  /* init and position bitstream reader */
467  init_get_bits(&s->gb, buf, buf_size * 8);
468  skip_bits(&s->gb, s->bitindex);
469 
470  /* process header or next subblock */
471  if (!s->got_header) {
472  if ((ret = read_header(s)) < 0)
473  return ret;
474  *got_frame_ptr = 0;
475  goto finish_frame;
476  }
477 
478  /* if quit command was read previously, don't decode anything */
479  if (s->got_quit_command) {
480  *got_frame_ptr = 0;
481  return avpkt->size;
482  }
483 
484  s->cur_chan = 0;
485  while (s->cur_chan < s->channels) {
486  unsigned cmd;
487  int len;
488 
489  if (get_bits_left(&s->gb) < 3 + FNSIZE) {
490  *got_frame_ptr = 0;
491  break;
492  }
493 
494  cmd = get_ur_golomb_shorten(&s->gb, FNSIZE);
495 
496  if (cmd > FN_VERBATIM) {
497  av_log(avctx, AV_LOG_ERROR, "unknown shorten function %d\n", cmd);
498  *got_frame_ptr = 0;
499  break;
500  }
501 
502  if (!is_audio_command[cmd]) {
503  /* process non-audio command */
504  switch (cmd) {
505  case FN_VERBATIM:
507  while (len--)
509  break;
510  case FN_BITSHIFT:
512  if (s->bitshift < 0)
513  return AVERROR_INVALIDDATA;
514  break;
515  case FN_BLOCKSIZE: {
516  unsigned blocksize = get_uint(s, av_log2(s->blocksize));
517  if (blocksize > s->blocksize) {
518  av_log(avctx, AV_LOG_ERROR,
519  "Increasing block size is not supported\n");
520  return AVERROR_PATCHWELCOME;
521  }
522  if (!blocksize || blocksize > MAX_BLOCKSIZE) {
523  av_log(avctx, AV_LOG_ERROR, "invalid or unsupported "
524  "block size: %d\n", blocksize);
525  return AVERROR(EINVAL);
526  }
527  s->blocksize = blocksize;
528  break;
529  }
530  case FN_QUIT:
531  s->got_quit_command = 1;
532  break;
533  }
534  if (cmd == FN_BLOCKSIZE || cmd == FN_QUIT) {
535  *got_frame_ptr = 0;
536  break;
537  }
538  } else {
539  /* process audio command */
540  int residual_size = 0;
541  int channel = s->cur_chan;
542  int32_t coffset;
543 
544  /* get Rice code for residual decoding */
545  if (cmd != FN_ZERO) {
546  residual_size = get_ur_golomb_shorten(&s->gb, ENERGYSIZE);
547  /* This is a hack as version 0 differed in the definition
548  * of get_sr_golomb_shorten(). */
549  if (s->version == 0)
550  residual_size--;
551  }
552 
553  /* calculate sample offset using means from previous blocks */
554  if (s->nmean == 0)
555  coffset = s->offset[channel][0];
556  else {
557  int32_t sum = (s->version < 2) ? 0 : s->nmean / 2;
558  for (i = 0; i < s->nmean; i++)
559  sum += s->offset[channel][i];
560  coffset = sum / s->nmean;
561  if (s->version >= 2)
562  coffset >>= FFMIN(1, s->bitshift);
563  }
564 
565  /* decode samples for this channel */
566  if (cmd == FN_ZERO) {
567  for (i = 0; i < s->blocksize; i++)
568  s->decoded[channel][i] = 0;
569  } else {
570  if ((ret = decode_subframe_lpc(s, cmd, channel,
571  residual_size, coffset)) < 0)
572  return ret;
573  }
574 
575  /* update means with info from the current block */
576  if (s->nmean > 0) {
577  int32_t sum = (s->version < 2) ? 0 : s->blocksize / 2;
578  for (i = 0; i < s->blocksize; i++)
579  sum += s->decoded[channel][i];
580 
581  for (i = 1; i < s->nmean; i++)
582  s->offset[channel][i - 1] = s->offset[channel][i];
583 
584  if (s->version < 2)
585  s->offset[channel][s->nmean - 1] = sum / s->blocksize;
586  else
587  s->offset[channel][s->nmean - 1] = (sum / s->blocksize) << s->bitshift;
588  }
589 
590  /* copy wrap samples for use with next block */
591  for (i = -s->nwrap; i < 0; i++)
592  s->decoded[channel][i] = s->decoded[channel][i + s->blocksize];
593 
594  /* shift samples to add in unused zero bits which were removed
595  * during encoding */
596  fix_bitshift(s, s->decoded[channel]);
597 
598  /* if this is the last channel in the block, output the samples */
599  s->cur_chan++;
600  if (s->cur_chan == s->channels) {
601  /* get output buffer */
602  frame->nb_samples = s->blocksize;
603  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
604  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
605  return ret;
606  }
607  /* interleave output */
608  output_buffer((int16_t **)frame->extended_data, s->channels,
609  s->blocksize, s->decoded);
610 
611  *got_frame_ptr = 1;
612  }
613  }
614  }
615  if (s->cur_chan < s->channels)
616  *got_frame_ptr = 0;
617 
619  s->bitindex = get_bits_count(&s->gb) - 8 * (get_bits_count(&s->gb) / 8);
620  i = get_bits_count(&s->gb) / 8;
621  if (i > buf_size) {
622  av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
623  s->bitstream_size = 0;
624  s->bitstream_index = 0;
625  return AVERROR_INVALIDDATA;
626  }
627  if (s->bitstream_size) {
628  s->bitstream_index += i;
629  s->bitstream_size -= i;
630  return input_buf_size;
631  } else
632  return i;
633 }
634 
636 {
637  ShortenContext *s = avctx->priv_data;
638  int i;
639 
640  for (i = 0; i < s->channels; i++) {
641  s->decoded[i] = NULL;
642  av_freep(&s->decoded_base[i]);
643  av_freep(&s->offset[i]);
644  }
645  av_freep(&s->bitstream);
646  av_freep(&s->coeffs);
647 
648  return 0;
649 }
650 
652  .name = "shorten",
653  .long_name = NULL_IF_CONFIG_SMALL("Shorten"),
654  .type = AVMEDIA_TYPE_AUDIO,
655  .id = AV_CODEC_ID_SHORTEN,
656  .priv_data_size = sizeof(ShortenContext),
658  .close = shorten_decode_close,
660  .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
661  .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
663 };
#define NSKIPSIZE
Definition: shorten.c:56
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
This structure describes decoded (raw) audio or video data.
Definition: frame.h:140
#define ENERGYSIZE
Definition: shorten.c:49
#define VERBATIM_CKSIZE_SIZE
Definition: shorten.c:76
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:228
int internal_ftype
Definition: shorten.c:101
static int init_offset(ShortenContext *s)
Definition: shorten.c:170
#define FN_BITSHIFT
Definition: shorten.c:68
#define OUT_BUFFER_SIZE
Definition: shorten.c:38
unsigned int allocated_bitstream_size
Definition: shorten.c:94
int32_t * decoded[MAX_CHANNELS]
Definition: shorten.c:87
int size
Definition: avcodec.h:1347
static const int fixed_coeffs[][3]
Definition: shorten.c:268
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:132
#define FF_ARRAY_ELEMS(a)
static int finish_frame(AVCodecContext *avctx, AVFrame *pict)
Definition: rv34.c:1578
AVCodec.
Definition: avcodec.h:3120
AVCodec ff_shorten_decoder
Definition: shorten.c:651
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:202
static av_cold int shorten_decode_init(AVCodecContext *avctx)
Definition: shorten.c:110
#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:863
static char buffer[20]
Definition: seek.c:32
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2160
uint8_t
#define av_cold
Definition: attributes.h:66
#define AV_RB32
Definition: intreadwrite.h:130
#define LPCQSIZE
Definition: shorten.c:48
const char data[16]
Definition: mxf.c:70
static int decode_wave_header(AVCodecContext *avctx, const uint8_t *header, int header_size)
Definition: shorten.c:192
uint8_t * data
Definition: avcodec.h:1346
int av_reallocp(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:140
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:182
GetBitContext gb
Definition: shorten.c:82
#define FNSIZE
Definition: shorten.c:61
bitstream reader API header.
#define FN_QLPC
Definition: shorten.c:69
#define FN_VERBATIM
Definition: shorten.c:71
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2769
static av_unused const uint8_t * skip_bytes(CABACContext *c, int n)
Skip n bytes and reset the decoder.
AVCodecContext * avctx
Definition: shorten.c:81
int32_t * decoded_base[MAX_CHANNELS]
Definition: shorten.c:88
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:526
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
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:375
int min_framesize
Definition: shorten.c:84
#define AVERROR(e)
Definition: error.h:43
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:161
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:151
unsigned channels
Definition: shorten.c:85
const char * name
Name of the codec implementation.
Definition: avcodec.h:3127
#define FN_BLOCKSIZE
Definition: shorten.c:67
#define FFMAX(a, b)
Definition: common.h:64
#define BITSHIFTSIZE
Definition: shorten.c:50
#define NWRAP
Definition: shorten.c:55
int got_header
Definition: shorten.c:106
static void output_buffer(int16_t **samples, int nchan, int blocksize, int32_t **buffer)
Definition: shorten.c:256
static int allocate_buffers(ShortenContext *s)
Definition: shorten.c:119
static void fix_bitshift(ShortenContext *s, int32_t *buffer)
Definition: shorten.c:161
int got_quit_command
Definition: shorten.c:107
#define FFMIN(a, b)
Definition: common.h:66
static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
Definition: avconv.c:1288
#define MAX_BLOCKSIZE
Definition: shorten.c:36
int32_t
#define TYPESIZE
Definition: shorten.c:46
#define FN_QUIT
Definition: shorten.c:66
#define LPCQUANT
Definition: shorten.c:58
#define VERBATIM_BYTE_SIZE
Definition: shorten.c:77
if(ac->has_optimized_func)
#define AVERROR_PATCHWELCOME
Not yet implemented in Libav, patches welcome.
Definition: error.h:57
NULL
Definition: eval.c:55
#define CHANSIZE
Definition: shorten.c:47
#define AV_LOG_INFO
Standard information.
Definition: log.h:135
Libavcodec external API header.
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:60
int sample_rate
samples per second
Definition: avcodec.h:2152
uint8_t header[OUT_BUFFER_SIZE]
Definition: shorten.c:96
#define CANONICAL_HEADER_SIZE
Definition: shorten.c:78
int bitstream_size
Definition: shorten.c:92
main external API structure.
Definition: avcodec.h:1409
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:589
static av_cold int shorten_decode_close(AVCodecContext *avctx)
Definition: shorten.c:635
#define MAX_CHANNELS
Definition: shorten.c:35
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:259
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:362
int * coeffs
Definition: shorten.c:90
static int decode_subframe_lpc(ShortenContext *s, int command, int channel, int residual_size, int32_t coffset)
Definition: shorten.c:275
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:300
#define TYPE_S16HL
Definition: shorten.c:52
static unsigned int get_uint(ShortenContext *s, int k)
Definition: shorten.c:154
#define DEFAULT_BLOCK_SIZE
Definition: shorten.c:44
#define ULONGSIZE
Definition: shorten.c:40
static int shorten_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: shorten.c:418
common internal api header.
int cur_chan
Definition: shorten.c:98
uint8_t * bitstream
Definition: shorten.c:91
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:582
#define FN_ZERO
Definition: shorten.c:70
int max_framesize
Definition: shorten.c:84
int bitstream_index
Definition: shorten.c:93
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:638
int header_size
Definition: shorten.c:95
void * priv_data
Definition: avcodec.h:1451
int32_t * offset[MAX_CHANNELS]
Definition: shorten.c:89
#define WAVE_FORMAT_PCM
Definition: shorten.c:42
int len
int channels
number of audio channels
Definition: avcodec.h:2153
#define av_log2
Definition: intmath.h:85
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:700
FILE * out
Definition: movenc.c:54
signed 16 bits, planar
Definition: samplefmt.h:69
int32_t lpcqoffset
Definition: shorten.c:105
static const uint8_t is_audio_command[10]
indicates if the FN_* command is audio or non-audio
Definition: shorten.c:74
#define V2LPCQOFFSET
Definition: shorten.c:59
static int read_header(ShortenContext *s)
Definition: shorten.c:330
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:174
static unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k)
read unsigned golomb rice code (shorten).
Definition: golomb.h:388
#define MKTAG(a, b, c, d)
Definition: common.h:256
exp golomb vlc stuff
This structure stores compressed data.
Definition: avcodec.h:1323
int bitshift
Definition: shorten.c:99
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:184
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:838
for(j=16;j >0;--j)
#define TYPE_S16LH
Definition: shorten.c:53
static int get_sr_golomb_shorten(GetBitContext *gb, int k)
read signed golomb rice code (shorten).
Definition: golomb.h:396