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