Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

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