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