FFmpeg
 All Data Structures 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 
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_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 {
89 
91  unsigned channels;
92 
96  int *coeffs;
103  int version;
104  int cur_chan;
105  int bitshift;
106  int nmean;
108  int nwrap;
110  int bitindex;
115 
117 {
118  ShortenContext *s = avctx->priv_data;
119  s->avctx = avctx;
120 
122  avctx->coded_frame = &s->frame;
123 
124  return 0;
125 }
126 
128 {
129  int i, chan;
130  int *coeffs;
131  void *tmp_ptr;
132 
133  for (chan = 0; chan < s->channels; chan++) {
134  if (FFMAX(1, s->nmean) >= UINT_MAX / sizeof(int32_t)) {
135  av_log(s->avctx, AV_LOG_ERROR, "nmean too large\n");
136  return AVERROR_INVALIDDATA;
137  }
138  if (s->blocksize + s->nwrap >= UINT_MAX / sizeof(int32_t) ||
139  s->blocksize + s->nwrap <= (unsigned)s->nwrap) {
141  "s->blocksize + s->nwrap too large\n");
142  return AVERROR_INVALIDDATA;
143  }
144 
145  tmp_ptr =
146  av_realloc(s->offset[chan], sizeof(int32_t) * FFMAX(1, s->nmean));
147  if (!tmp_ptr)
148  return AVERROR(ENOMEM);
149  s->offset[chan] = tmp_ptr;
150 
151  tmp_ptr = av_realloc(s->decoded_base[chan], (s->blocksize + s->nwrap) *
152  sizeof(s->decoded_base[0][0]));
153  if (!tmp_ptr)
154  return AVERROR(ENOMEM);
155  s->decoded_base[chan] = tmp_ptr;
156  for (i = 0; i < s->nwrap; i++)
157  s->decoded_base[chan][i] = 0;
158  s->decoded[chan] = s->decoded_base[chan] + s->nwrap;
159  }
160 
161  coeffs = av_realloc(s->coeffs, s->nwrap * sizeof(*s->coeffs));
162  if (!coeffs)
163  return AVERROR(ENOMEM);
164  s->coeffs = coeffs;
165 
166  return 0;
167 }
168 
169 static inline unsigned int get_uint(ShortenContext *s, int k)
170 {
171  if (s->version != 0)
173  return get_ur_golomb_shorten(&s->gb, k);
174 }
175 
177 {
178  int i;
179 
180  if (s->bitshift != 0)
181  for (i = 0; i < s->blocksize; i++)
182  buffer[i] <<= s->bitshift;
183 }
184 
186 {
187  int32_t mean = 0;
188  int chan, i;
189  int nblock = FFMAX(1, s->nmean);
190  /* initialise offset */
191  switch (s->internal_ftype) {
192  case TYPE_U8:
194  mean = 0x80;
195  break;
196  case TYPE_S16HL:
197  case TYPE_S16LH:
199  break;
200  default:
201  av_log(s->avctx, AV_LOG_ERROR, "unknown audio type\n");
202  return AVERROR_PATCHWELCOME;
203  }
204 
205  for (chan = 0; chan < s->channels; chan++)
206  for (i = 0; i < nblock; i++)
207  s->offset[chan][i] = mean;
208  return 0;
209 }
210 
211 static int decode_wave_header(AVCodecContext *avctx, const uint8_t *header,
212  int header_size)
213 {
214  int len, bps;
215  short wave_format;
216  const uint8_t *end= header + header_size;
217 
218  if (bytestream_get_le32(&header) != MKTAG('R', 'I', 'F', 'F')) {
219  av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n");
220  return AVERROR_INVALIDDATA;
221  }
222 
223  header += 4; /* chunk size */
224 
225  if (bytestream_get_le32(&header) != MKTAG('W', 'A', 'V', 'E')) {
226  av_log(avctx, AV_LOG_ERROR, "missing WAVE tag\n");
227  return AVERROR_INVALIDDATA;
228  }
229 
230  while (bytestream_get_le32(&header) != MKTAG('f', 'm', 't', ' ')) {
231  len = bytestream_get_le32(&header);
232  if (len<0 || end - header - 8 < len)
233  return AVERROR_INVALIDDATA;
234  header += len;
235  }
236  len = bytestream_get_le32(&header);
237 
238  if (len < 16) {
239  av_log(avctx, AV_LOG_ERROR, "fmt chunk was too short\n");
240  return AVERROR_INVALIDDATA;
241  }
242 
243  wave_format = bytestream_get_le16(&header);
244 
245  switch (wave_format) {
246  case WAVE_FORMAT_PCM:
247  break;
248  default:
249  av_log(avctx, AV_LOG_ERROR, "unsupported wave format\n");
250  return AVERROR(ENOSYS);
251  }
252 
253  header += 2; // skip channels (already got from shorten header)
254  avctx->sample_rate = bytestream_get_le32(&header);
255  header += 4; // skip bit rate (represents original uncompressed bit rate)
256  header += 2; // skip block align (not needed)
257  bps = bytestream_get_le16(&header);
258  avctx->bits_per_coded_sample = bps;
259 
260  if (bps != 16 && bps != 8) {
261  av_log(avctx, AV_LOG_ERROR, "unsupported number of bits per sample: %d\n", bps);
262  return AVERROR(ENOSYS);
263  }
264 
265  len -= 16;
266  if (len > 0)
267  av_log(avctx, AV_LOG_INFO, "%d header bytes unparsed\n", len);
268 
269  return 0;
270 }
271 
272 static const int fixed_coeffs[3][3] = {
273  { 1, 0, 0 },
274  { 2, -1, 0 },
275  { 3, -3, 1 }
276 };
277 
278 static int decode_subframe_lpc(ShortenContext *s, int command, int channel,
279  int residual_size, int32_t coffset)
280 {
281  int pred_order, sum, qshift, init_sum, i, j;
282  const int *coeffs;
283 
284  if (command == FN_QLPC) {
285  /* read/validate prediction order */
286  pred_order = get_ur_golomb_shorten(&s->gb, LPCQSIZE);
287  if (pred_order > s->nwrap) {
288  av_log(s->avctx, AV_LOG_ERROR, "invalid pred_order %d\n",
289  pred_order);
290  return AVERROR(EINVAL);
291  }
292  /* read LPC coefficients */
293  for (i = 0; i < pred_order; i++)
294  s->coeffs[i] = get_sr_golomb_shorten(&s->gb, LPCQUANT);
295  coeffs = s->coeffs;
296 
297  qshift = LPCQUANT;
298  } else {
299  /* fixed LPC coeffs */
300  pred_order = command;
301  coeffs = fixed_coeffs[pred_order - 1];
302  qshift = 0;
303  }
304 
305  /* subtract offset from previous samples to use in prediction */
306  if (command == FN_QLPC && coffset)
307  for (i = -pred_order; i < 0; i++)
308  s->decoded[channel][i] -= coffset;
309 
310  /* decode residual and do LPC prediction */
311  init_sum = pred_order ? (command == FN_QLPC ? s->lpcqoffset : 0) : coffset;
312  for (i = 0; i < s->blocksize; i++) {
313  sum = init_sum;
314  for (j = 0; j < pred_order; j++)
315  sum += coeffs[j] * s->decoded[channel][i - j - 1];
316  s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) +
317  (sum >> qshift);
318  }
319 
320  /* add offset to current samples */
321  if (command == FN_QLPC && coffset)
322  for (i = 0; i < s->blocksize; i++)
323  s->decoded[channel][i] += coffset;
324 
325  return 0;
326 }
327 
329 {
330  int i, ret;
331  int maxnlpc = 0;
332  /* shorten signature */
333  if (get_bits_long(&s->gb, 32) != AV_RB32("ajkg")) {
334  av_log(s->avctx, AV_LOG_ERROR, "missing shorten magic 'ajkg'\n");
335  return AVERROR_INVALIDDATA;
336  }
337 
338  s->lpcqoffset = 0;
340  s->nmean = -1;
341  s->version = get_bits(&s->gb, 8);
343 
344  s->channels = get_uint(s, CHANSIZE);
345  if (!s->channels) {
346  av_log(s->avctx, AV_LOG_ERROR, "No channels reported\n");
347  return AVERROR_INVALIDDATA;
348  }
349  if (s->channels > MAX_CHANNELS) {
350  av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
351  s->channels = 0;
352  return AVERROR_INVALIDDATA;
353  }
354  s->avctx->channels = s->channels;
355 
356  /* get blocksize if version > 0 */
357  if (s->version > 0) {
358  int skip_bytes;
359  unsigned blocksize;
360 
361  blocksize = get_uint(s, av_log2(DEFAULT_BLOCK_SIZE));
362  if (!blocksize || blocksize > MAX_BLOCKSIZE) {
364  "invalid or unsupported block size: %d\n",
365  blocksize);
366  return AVERROR(EINVAL);
367  }
368  s->blocksize = blocksize;
369 
370  maxnlpc = get_uint(s, LPCQSIZE);
371  s->nmean = get_uint(s, 0);
372 
373  skip_bytes = get_uint(s, NSKIPSIZE);
374  for (i = 0; i < skip_bytes; i++)
375  skip_bits(&s->gb, 8);
376  }
377  s->nwrap = FFMAX(NWRAP, maxnlpc);
378 
379  if ((ret = allocate_buffers(s)) < 0)
380  return ret;
381 
382  if ((ret = init_offset(s)) < 0)
383  return ret;
384 
385  if (s->version > 1)
387 
390  "missing verbatim section at beginning of stream\n");
391  return AVERROR_INVALIDDATA;
392  }
393 
395  if (s->header_size >= OUT_BUFFER_SIZE ||
397  av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n",
398  s->header_size);
399  return AVERROR_INVALIDDATA;
400  }
401 
402  for (i = 0; i < s->header_size; i++)
404 
405  if ((ret = decode_wave_header(s->avctx, s->header, s->header_size)) < 0)
406  return ret;
407 
408  s->cur_chan = 0;
409  s->bitshift = 0;
410 
411  s->got_header = 1;
412 
413  return 0;
414 }
415 
416 static int shorten_decode_frame(AVCodecContext *avctx, void *data,
417  int *got_frame_ptr, AVPacket *avpkt)
418 {
419  const uint8_t *buf = avpkt->data;
420  int buf_size = avpkt->size;
421  ShortenContext *s = avctx->priv_data;
422  int i, input_buf_size = 0;
423  int ret;
424 
425  /* allocate internal bitstream buffer */
426  if (s->max_framesize == 0) {
427  void *tmp_ptr;
428  s->max_framesize = 8192; // should hopefully be enough for the first header
430  s->max_framesize);
431  if (!tmp_ptr) {
432  av_log(avctx, AV_LOG_ERROR, "error allocating bitstream buffer\n");
433  return AVERROR(ENOMEM);
434  }
435  s->bitstream = tmp_ptr;
436  }
437 
438  /* append current packet data to bitstream buffer */
439  if (1 && s->max_framesize) { //FIXME truncated
440  buf_size = FFMIN(buf_size, s->max_framesize - s->bitstream_size);
441  input_buf_size = buf_size;
442 
443  if (s->bitstream_index + s->bitstream_size + buf_size >
445  memmove(s->bitstream, &s->bitstream[s->bitstream_index],
446  s->bitstream_size);
447  s->bitstream_index = 0;
448  }
449  if (buf)
450  memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf,
451  buf_size);
452  buf = &s->bitstream[s->bitstream_index];
453  buf_size += s->bitstream_size;
454  s->bitstream_size = buf_size;
455 
456  /* do not decode until buffer has at least max_framesize bytes or
457  * the end of the file has been reached */
458  if (buf_size < s->max_framesize && avpkt->data) {
459  *got_frame_ptr = 0;
460  return input_buf_size;
461  }
462  }
463  /* init and position bitstream reader */
464  init_get_bits(&s->gb, buf, buf_size * 8);
465  skip_bits(&s->gb, s->bitindex);
466 
467  /* process header or next subblock */
468  if (!s->got_header) {
469  if ((ret = read_header(s)) < 0)
470  return ret;
471  *got_frame_ptr = 0;
472  goto finish_frame;
473  }
474 
475  /* if quit command was read previously, don't decode anything */
476  if (s->got_quit_command) {
477  *got_frame_ptr = 0;
478  return avpkt->size;
479  }
480 
481  s->cur_chan = 0;
482  while (s->cur_chan < s->channels) {
483  unsigned int cmd;
484  int len;
485 
486  if (get_bits_left(&s->gb) < 3 + FNSIZE) {
487  *got_frame_ptr = 0;
488  break;
489  }
490 
491  cmd = get_ur_golomb_shorten(&s->gb, FNSIZE);
492 
493  if (cmd > FN_VERBATIM) {
494  av_log(avctx, AV_LOG_ERROR, "unknown shorten function %d\n", cmd);
495  *got_frame_ptr = 0;
496  break;
497  }
498 
499  if (!is_audio_command[cmd]) {
500  /* process non-audio command */
501  switch (cmd) {
502  case FN_VERBATIM:
504  while (len--)
506  break;
507  case FN_BITSHIFT:
509  break;
510  case FN_BLOCKSIZE: {
511  unsigned blocksize = get_uint(s, av_log2(s->blocksize));
512  if (blocksize > s->blocksize) {
513  av_log(avctx, AV_LOG_ERROR,
514  "Increasing block size is not supported\n");
515  return AVERROR_PATCHWELCOME;
516  }
517  if (!blocksize || blocksize > MAX_BLOCKSIZE) {
518  av_log(avctx, AV_LOG_ERROR, "invalid or unsupported "
519  "block size: %d\n", blocksize);
520  return AVERROR(EINVAL);
521  }
522  s->blocksize = blocksize;
523  break;
524  }
525  case FN_QUIT:
526  s->got_quit_command = 1;
527  break;
528  }
529  if (cmd == FN_BLOCKSIZE || cmd == FN_QUIT) {
530  *got_frame_ptr = 0;
531  break;
532  }
533  } else {
534  /* process audio command */
535  int residual_size = 0;
536  int channel = s->cur_chan;
537  int32_t coffset;
538 
539  /* get Rice code for residual decoding */
540  if (cmd != FN_ZERO) {
541  residual_size = get_ur_golomb_shorten(&s->gb, ENERGYSIZE);
542  /* This is a hack as version 0 differed in the definition
543  * of get_sr_golomb_shorten(). */
544  if (s->version == 0)
545  residual_size--;
546  }
547 
548  /* calculate sample offset using means from previous blocks */
549  if (s->nmean == 0)
550  coffset = s->offset[channel][0];
551  else {
552  int32_t sum = (s->version < 2) ? 0 : s->nmean / 2;
553  for (i = 0; i < s->nmean; i++)
554  sum += s->offset[channel][i];
555  coffset = sum / s->nmean;
556  if (s->version >= 2)
557  coffset = s->bitshift == 0 ? coffset : coffset >> s->bitshift - 1 >> 1;
558  }
559 
560  /* decode samples for this channel */
561  if (cmd == FN_ZERO) {
562  for (i = 0; i < s->blocksize; i++)
563  s->decoded[channel][i] = 0;
564  } else {
565  if ((ret = decode_subframe_lpc(s, cmd, channel,
566  residual_size, coffset)) < 0)
567  return ret;
568  }
569 
570  /* update means with info from the current block */
571  if (s->nmean > 0) {
572  int32_t sum = (s->version < 2) ? 0 : s->blocksize / 2;
573  for (i = 0; i < s->blocksize; i++)
574  sum += s->decoded[channel][i];
575 
576  for (i = 1; i < s->nmean; i++)
577  s->offset[channel][i - 1] = s->offset[channel][i];
578 
579  if (s->version < 2)
580  s->offset[channel][s->nmean - 1] = sum / s->blocksize;
581  else
582  s->offset[channel][s->nmean - 1] = (sum / s->blocksize) << s->bitshift;
583  }
584 
585  /* copy wrap samples for use with next block */
586  for (i = -s->nwrap; i < 0; i++)
587  s->decoded[channel][i] = s->decoded[channel][i + s->blocksize];
588 
589  /* shift samples to add in unused zero bits which were removed
590  * during encoding */
591  fix_bitshift(s, s->decoded[channel]);
592 
593  /* if this is the last channel in the block, output the samples */
594  s->cur_chan++;
595  if (s->cur_chan == s->channels) {
596  uint8_t *samples_u8;
597  int16_t *samples_s16;
598  int chan;
599 
600  /* get output buffer */
601  s->frame.nb_samples = s->blocksize;
602  if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
603  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
604  return ret;
605  }
606 
607  for (chan = 0; chan < s->channels; chan++) {
608  samples_u8 = ((uint8_t **)s->frame.extended_data)[chan];
609  samples_s16 = ((int16_t **)s->frame.extended_data)[chan];
610  for (i = 0; i < s->blocksize; i++) {
611  switch (s->internal_ftype) {
612  case TYPE_U8:
613  *samples_u8++ = av_clip_uint8(s->decoded[chan][i]);
614  break;
615  case TYPE_S16HL:
616  case TYPE_S16LH:
617  *samples_s16++ = av_clip_int16(s->decoded[chan][i]);
618  break;
619  }
620  }
621  }
622 
623 
624  *got_frame_ptr = 1;
625  *(AVFrame *)data = s->frame;
626  }
627  }
628  }
629  if (s->cur_chan < s->channels)
630  *got_frame_ptr = 0;
631 
633  s->bitindex = get_bits_count(&s->gb) - 8 * (get_bits_count(&s->gb) / 8);
634  i = get_bits_count(&s->gb) / 8;
635  if (i > buf_size) {
636  av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
637  s->bitstream_size = 0;
638  s->bitstream_index = 0;
639  return AVERROR_INVALIDDATA;
640  }
641  if (s->bitstream_size) {
642  s->bitstream_index += i;
643  s->bitstream_size -= i;
644  return input_buf_size;
645  } else
646  return i;
647 }
648 
650 {
651  ShortenContext *s = avctx->priv_data;
652  int i;
653 
654  for (i = 0; i < s->channels; i++) {
655  s->decoded[i] = NULL;
656  av_freep(&s->decoded_base[i]);
657  av_freep(&s->offset[i]);
658  }
659  av_freep(&s->bitstream);
660  av_freep(&s->coeffs);
661 
662  return 0;
663 }
664 
666  .name = "shorten",
667  .type = AVMEDIA_TYPE_AUDIO,
668  .id = AV_CODEC_ID_SHORTEN,
669  .priv_data_size = sizeof(ShortenContext),
673  .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
674  .long_name = NULL_IF_CONFIG_SMALL("Shorten"),
675  .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
678 };