LCOV - code coverage report
Current view: top level - libavcodec - shorten.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 230 402 57.2 %
Date: 2017-12-14 01:15:32 Functions: 10 11 90.9 %

          Line data    Source code
       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             : #include <limits.h>
      29             : #include "avcodec.h"
      30             : #include "bswapdsp.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             :     int swap;
     114             :     BswapDSPContext bdsp;
     115             : } ShortenContext;
     116             : 
     117           2 : static av_cold int shorten_decode_init(AVCodecContext *avctx)
     118             : {
     119           2 :     ShortenContext *s = avctx->priv_data;
     120           2 :     s->avctx          = avctx;
     121             : 
     122           2 :     ff_bswapdsp_init(&s->bdsp);
     123             : 
     124           2 :     return 0;
     125             : }
     126             : 
     127           2 : static int allocate_buffers(ShortenContext *s)
     128             : {
     129             :     int i, chan, err;
     130             : 
     131           6 :     for (chan = 0; chan < s->channels; chan++) {
     132           4 :         if (FFMAX(1, s->nmean) >= UINT_MAX / sizeof(int32_t)) {
     133           0 :             av_log(s->avctx, AV_LOG_ERROR, "nmean too large\n");
     134           0 :             return AVERROR_INVALIDDATA;
     135             :         }
     136           4 :         if (s->blocksize + (uint64_t)s->nwrap >= UINT_MAX / sizeof(int32_t)) {
     137           0 :             av_log(s->avctx, AV_LOG_ERROR,
     138             :                    "s->blocksize + s->nwrap too large\n");
     139           0 :             return AVERROR_INVALIDDATA;
     140             :         }
     141             : 
     142           4 :         if ((err = av_reallocp_array(&s->offset[chan],
     143             :                                sizeof(int32_t),
     144           4 :                                FFMAX(1, s->nmean))) < 0)
     145           0 :             return err;
     146             : 
     147           4 :         if ((err = av_reallocp_array(&s->decoded_base[chan], (s->blocksize + s->nwrap),
     148             :                                sizeof(s->decoded_base[0][0]))) < 0)
     149           0 :             return err;
     150          16 :         for (i = 0; i < s->nwrap; i++)
     151          12 :             s->decoded_base[chan][i] = 0;
     152           4 :         s->decoded[chan] = s->decoded_base[chan] + s->nwrap;
     153             :     }
     154             : 
     155           2 :     if ((err = av_reallocp_array(&s->coeffs, s->nwrap, sizeof(*s->coeffs))) < 0)
     156           0 :         return err;
     157             : 
     158           2 :     return 0;
     159             : }
     160             : 
     161          12 : static inline unsigned int get_uint(ShortenContext *s, int k)
     162             : {
     163          12 :     if (s->version != 0) {
     164          12 :         k = get_ur_golomb_shorten(&s->gb, ULONGSIZE);
     165          12 :         if (k > 31U)
     166           0 :             return AVERROR_INVALIDDATA;
     167             :     }
     168          12 :     return get_ur_golomb_shorten(&s->gb, k);
     169             : }
     170             : 
     171        3099 : static void fix_bitshift(ShortenContext *s, int32_t *buffer)
     172             : {
     173             :     int i;
     174             : 
     175        3099 :     if (s->bitshift == 32) {
     176           0 :         for (i = 0; i < s->blocksize; i++)
     177           0 :             buffer[i] = 0;
     178        3099 :     } else if (s->bitshift != 0) {
     179           0 :         for (i = 0; i < s->blocksize; i++)
     180           0 :             buffer[i] <<= s->bitshift;
     181             :     }
     182        3099 : }
     183             : 
     184           2 : static int init_offset(ShortenContext *s)
     185             : {
     186           2 :     int32_t mean = 0;
     187             :     int chan, i;
     188           2 :     int nblock = FFMAX(1, s->nmean);
     189             :     /* initialise offset */
     190           2 :     switch (s->internal_ftype) {
     191           0 :     case TYPE_U8:
     192           0 :         s->avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
     193           0 :         mean = 0x80;
     194           0 :         break;
     195           2 :     case TYPE_S16HL:
     196             :     case TYPE_S16LH:
     197           2 :         s->avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
     198           2 :         break;
     199           0 :     default:
     200           0 :         av_log(s->avctx, AV_LOG_ERROR, "unknown audio type\n");
     201           0 :         return AVERROR_PATCHWELCOME;
     202             :     }
     203             : 
     204           6 :     for (chan = 0; chan < s->channels; chan++)
     205          20 :         for (i = 0; i < nblock; i++)
     206          16 :             s->offset[chan][i] = mean;
     207           2 :     return 0;
     208             : }
     209             : 
     210           0 : static int decode_aiff_header(AVCodecContext *avctx, const uint8_t *header,
     211             :                               int header_size)
     212             : {
     213           0 :     ShortenContext *s = avctx->priv_data;
     214             :     int len, bps, exp;
     215             :     GetByteContext gb;
     216             :     uint64_t val;
     217             :     uint32_t tag;
     218             : 
     219           0 :     bytestream2_init(&gb, header, header_size);
     220             : 
     221           0 :     if (bytestream2_get_le32(&gb) != MKTAG('F', 'O', 'R', 'M')) {
     222           0 :         av_log(avctx, AV_LOG_ERROR, "missing FORM tag\n");
     223           0 :         return AVERROR_INVALIDDATA;
     224             :     }
     225             : 
     226           0 :     bytestream2_skip(&gb, 4); /* chunk size */
     227             : 
     228           0 :     tag = bytestream2_get_le32(&gb);
     229           0 :     if (tag != MKTAG('A', 'I', 'F', 'F') &&
     230             :         tag != MKTAG('A', 'I', 'F', 'C')) {
     231           0 :         av_log(avctx, AV_LOG_ERROR, "missing AIFF tag\n");
     232           0 :         return AVERROR_INVALIDDATA;
     233             :     }
     234             : 
     235           0 :     while (bytestream2_get_le32(&gb) != MKTAG('C', 'O', 'M', 'M')) {
     236           0 :         len = bytestream2_get_be32(&gb);
     237           0 :         bytestream2_skip(&gb, len + (len & 1));
     238           0 :         if (len < 0 || bytestream2_get_bytes_left(&gb) < 18) {
     239           0 :             av_log(avctx, AV_LOG_ERROR, "no COMM chunk found\n");
     240           0 :             return AVERROR_INVALIDDATA;
     241             :         }
     242             :     }
     243           0 :     len = bytestream2_get_be32(&gb);
     244             : 
     245           0 :     if (len < 18) {
     246           0 :         av_log(avctx, AV_LOG_ERROR, "COMM chunk was too short\n");
     247           0 :         return AVERROR_INVALIDDATA;
     248             :     }
     249             : 
     250           0 :     bytestream2_skip(&gb, 6);
     251           0 :     bps = bytestream2_get_be16(&gb);
     252           0 :     avctx->bits_per_coded_sample = bps;
     253             : 
     254           0 :     s->swap = tag == MKTAG('A', 'I', 'F', 'C');
     255             : 
     256           0 :     if (bps != 16 && bps != 8) {
     257           0 :         av_log(avctx, AV_LOG_ERROR, "unsupported number of bits per sample: %d\n", bps);
     258           0 :         return AVERROR(ENOSYS);
     259             :     }
     260             : 
     261           0 :     exp = bytestream2_get_be16(&gb) - 16383 - 63;
     262           0 :     val = bytestream2_get_be64(&gb);
     263           0 :     if (exp < -63 || exp > 63) {
     264           0 :         av_log(avctx, AV_LOG_ERROR, "exp %d is out of range\n", exp);
     265           0 :         return AVERROR_INVALIDDATA;
     266             :     }
     267           0 :     if (exp >= 0)
     268           0 :         avctx->sample_rate = val << exp;
     269             :     else
     270           0 :         avctx->sample_rate = (val + (1ULL<<(-exp-1))) >> -exp;
     271           0 :     len -= 18;
     272           0 :     if (len > 0)
     273           0 :         av_log(avctx, AV_LOG_INFO, "%d header bytes unparsed\n", len);
     274             : 
     275           0 :     return 0;
     276             : }
     277             : 
     278           2 : static int decode_wave_header(AVCodecContext *avctx, const uint8_t *header,
     279             :                               int header_size)
     280             : {
     281             :     int len, bps;
     282             :     short wave_format;
     283             :     GetByteContext gb;
     284             : 
     285           2 :     bytestream2_init(&gb, header, header_size);
     286             : 
     287           2 :     if (bytestream2_get_le32(&gb) != MKTAG('R', 'I', 'F', 'F')) {
     288           0 :         av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n");
     289           0 :         return AVERROR_INVALIDDATA;
     290             :     }
     291             : 
     292           2 :     bytestream2_skip(&gb, 4); /* chunk size */
     293             : 
     294           2 :     if (bytestream2_get_le32(&gb) != MKTAG('W', 'A', 'V', 'E')) {
     295           0 :         av_log(avctx, AV_LOG_ERROR, "missing WAVE tag\n");
     296           0 :         return AVERROR_INVALIDDATA;
     297             :     }
     298             : 
     299           4 :     while (bytestream2_get_le32(&gb) != MKTAG('f', 'm', 't', ' ')) {
     300           0 :         len = bytestream2_get_le32(&gb);
     301           0 :         bytestream2_skip(&gb, len);
     302           0 :         if (len < 0 || bytestream2_get_bytes_left(&gb) < 16) {
     303           0 :             av_log(avctx, AV_LOG_ERROR, "no fmt chunk found\n");
     304           0 :             return AVERROR_INVALIDDATA;
     305             :         }
     306             :     }
     307           2 :     len = bytestream2_get_le32(&gb);
     308             : 
     309           2 :     if (len < 16) {
     310           0 :         av_log(avctx, AV_LOG_ERROR, "fmt chunk was too short\n");
     311           0 :         return AVERROR_INVALIDDATA;
     312             :     }
     313             : 
     314           2 :     wave_format = bytestream2_get_le16(&gb);
     315             : 
     316           2 :     switch (wave_format) {
     317           2 :     case WAVE_FORMAT_PCM:
     318           2 :         break;
     319           0 :     default:
     320           0 :         av_log(avctx, AV_LOG_ERROR, "unsupported wave format\n");
     321           0 :         return AVERROR(ENOSYS);
     322             :     }
     323             : 
     324           2 :     bytestream2_skip(&gb, 2); // skip channels    (already got from shorten header)
     325           2 :     avctx->sample_rate = bytestream2_get_le32(&gb);
     326           2 :     bytestream2_skip(&gb, 4); // skip bit rate    (represents original uncompressed bit rate)
     327           2 :     bytestream2_skip(&gb, 2); // skip block align (not needed)
     328           2 :     bps = bytestream2_get_le16(&gb);
     329           2 :     avctx->bits_per_coded_sample = bps;
     330             : 
     331           2 :     if (bps != 16 && bps != 8) {
     332           0 :         av_log(avctx, AV_LOG_ERROR, "unsupported number of bits per sample: %d\n", bps);
     333           0 :         return AVERROR(ENOSYS);
     334             :     }
     335             : 
     336           2 :     len -= 16;
     337           2 :     if (len > 0)
     338           0 :         av_log(avctx, AV_LOG_INFO, "%d header bytes unparsed\n", len);
     339             : 
     340           2 :     return 0;
     341             : }
     342             : 
     343             : static const int fixed_coeffs[][3] = {
     344             :     { 0,  0,  0 },
     345             :     { 1,  0,  0 },
     346             :     { 2, -1,  0 },
     347             :     { 3, -3,  1 }
     348             : };
     349             : 
     350        3099 : static int decode_subframe_lpc(ShortenContext *s, int command, int channel,
     351             :                                int residual_size, int32_t coffset)
     352             : {
     353             :     int pred_order, sum, qshift, init_sum, i, j;
     354             :     const int *coeffs;
     355             : 
     356        3099 :     if (command == FN_QLPC) {
     357             :         /* read/validate prediction order */
     358           0 :         pred_order = get_ur_golomb_shorten(&s->gb, LPCQSIZE);
     359           0 :         if ((unsigned)pred_order > s->nwrap) {
     360           0 :             av_log(s->avctx, AV_LOG_ERROR, "invalid pred_order %d\n",
     361             :                    pred_order);
     362           0 :             return AVERROR(EINVAL);
     363             :         }
     364             :         /* read LPC coefficients */
     365           0 :         for (i = 0; i < pred_order; i++)
     366           0 :             s->coeffs[i] = get_sr_golomb_shorten(&s->gb, LPCQUANT);
     367           0 :         coeffs = s->coeffs;
     368             : 
     369           0 :         qshift = LPCQUANT;
     370             :     } else {
     371             :         /* fixed LPC coeffs */
     372        3099 :         pred_order = command;
     373        3099 :         if (pred_order >= FF_ARRAY_ELEMS(fixed_coeffs)) {
     374           0 :             av_log(s->avctx, AV_LOG_ERROR, "invalid pred_order %d\n",
     375             :                    pred_order);
     376           0 :             return AVERROR_INVALIDDATA;
     377             :         }
     378        3099 :         coeffs     = fixed_coeffs[pred_order];
     379        3099 :         qshift     = 0;
     380             :     }
     381             : 
     382             :     /* subtract offset from previous samples to use in prediction */
     383        3099 :     if (command == FN_QLPC && coffset)
     384           0 :         for (i = -pred_order; i < 0; i++)
     385           0 :             s->decoded[channel][i] -= coffset;
     386             : 
     387             :     /* decode residual and do LPC prediction */
     388        3099 :     init_sum = pred_order ? (command == FN_QLPC ? s->lpcqoffset : 0) : coffset;
     389      796443 :     for (i = 0; i < s->blocksize; i++) {
     390      793344 :         sum = init_sum;
     391     2060032 :         for (j = 0; j < pred_order; j++)
     392     1266688 :             sum += coeffs[j] * s->decoded[channel][i - j - 1];
     393     1586688 :         s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) +
     394      793344 :                                  (sum >> qshift);
     395             :     }
     396             : 
     397             :     /* add offset to current samples */
     398        3099 :     if (command == FN_QLPC && coffset)
     399           0 :         for (i = 0; i < s->blocksize; i++)
     400           0 :             s->decoded[channel][i] += coffset;
     401             : 
     402        3099 :     return 0;
     403             : }
     404             : 
     405           2 : static int read_header(ShortenContext *s)
     406             : {
     407             :     int i, ret;
     408           2 :     int maxnlpc = 0;
     409             :     /* shorten signature */
     410           2 :     if (get_bits_long(&s->gb, 32) != AV_RB32("ajkg")) {
     411           0 :         av_log(s->avctx, AV_LOG_ERROR, "missing shorten magic 'ajkg'\n");
     412           0 :         return AVERROR_INVALIDDATA;
     413             :     }
     414             : 
     415           2 :     s->lpcqoffset     = 0;
     416           2 :     s->blocksize      = DEFAULT_BLOCK_SIZE;
     417           2 :     s->nmean          = -1;
     418           2 :     s->version        = get_bits(&s->gb, 8);
     419           2 :     s->internal_ftype = get_uint(s, TYPESIZE);
     420             : 
     421           2 :     s->channels = get_uint(s, CHANSIZE);
     422           2 :     if (!s->channels) {
     423           0 :         av_log(s->avctx, AV_LOG_ERROR, "No channels reported\n");
     424           0 :         return AVERROR_INVALIDDATA;
     425             :     }
     426           2 :     if (s->channels > MAX_CHANNELS) {
     427           0 :         av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
     428           0 :         s->channels = 0;
     429           0 :         return AVERROR_INVALIDDATA;
     430             :     }
     431           2 :     s->avctx->channels = s->channels;
     432             : 
     433             :     /* get blocksize if version > 0 */
     434           2 :     if (s->version > 0) {
     435             :         int skip_bytes;
     436             :         unsigned blocksize;
     437             : 
     438           2 :         blocksize = get_uint(s, av_log2(DEFAULT_BLOCK_SIZE));
     439           2 :         if (!blocksize || blocksize > MAX_BLOCKSIZE) {
     440           0 :             av_log(s->avctx, AV_LOG_ERROR,
     441             :                    "invalid or unsupported block size: %d\n",
     442             :                    blocksize);
     443           0 :             return AVERROR(EINVAL);
     444             :         }
     445           2 :         s->blocksize = blocksize;
     446             : 
     447           2 :         maxnlpc  = get_uint(s, LPCQSIZE);
     448           2 :         if (maxnlpc > 1024U) {
     449           0 :             av_log(s->avctx, AV_LOG_ERROR, "maxnlpc is: %d\n", maxnlpc);
     450           0 :             return AVERROR_INVALIDDATA;
     451             :         }
     452           2 :         s->nmean = get_uint(s, 0);
     453             : 
     454           2 :         skip_bytes = get_uint(s, NSKIPSIZE);
     455           2 :         if ((unsigned)skip_bytes > get_bits_left(&s->gb)/8) {
     456           0 :             av_log(s->avctx, AV_LOG_ERROR, "invalid skip_bytes: %d\n", skip_bytes);
     457           0 :             return AVERROR_INVALIDDATA;
     458             :         }
     459             : 
     460           2 :         for (i = 0; i < skip_bytes; i++)
     461           0 :             skip_bits(&s->gb, 8);
     462             :     }
     463           2 :     s->nwrap = FFMAX(NWRAP, maxnlpc);
     464             : 
     465           2 :     if (s->version > 1)
     466           2 :         s->lpcqoffset = V2LPCQOFFSET;
     467             : 
     468           2 :     if (s->avctx->extradata_size > 0)
     469           0 :         goto end;
     470             : 
     471           2 :     if (get_ur_golomb_shorten(&s->gb, FNSIZE) != FN_VERBATIM) {
     472           0 :         av_log(s->avctx, AV_LOG_ERROR,
     473             :                "missing verbatim section at beginning of stream\n");
     474           0 :         return AVERROR_INVALIDDATA;
     475             :     }
     476             : 
     477           2 :     s->header_size = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
     478           4 :     if (s->header_size >= OUT_BUFFER_SIZE ||
     479           2 :         s->header_size < CANONICAL_HEADER_SIZE) {
     480           0 :         av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n",
     481             :                s->header_size);
     482           0 :         return AVERROR_INVALIDDATA;
     483             :     }
     484             : 
     485          90 :     for (i = 0; i < s->header_size; i++)
     486          88 :         s->header[i] = (char)get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
     487             : 
     488           2 :     if (AV_RL32(s->header) == MKTAG('R','I','F','F')) {
     489           2 :         if ((ret = decode_wave_header(s->avctx, s->header, s->header_size)) < 0)
     490           0 :             return ret;
     491           0 :     } else if (AV_RL32(s->header) == MKTAG('F','O','R','M')) {
     492           0 :         if ((ret = decode_aiff_header(s->avctx, s->header, s->header_size)) < 0)
     493           0 :             return ret;
     494             :     } else {
     495           0 :         avpriv_report_missing_feature(s->avctx, "unsupported bit packing %"
     496           0 :                                       PRIX32, AV_RL32(s->header));
     497           0 :         return AVERROR_PATCHWELCOME;
     498             :     }
     499             : 
     500           2 : end:
     501             : 
     502           2 :     if ((ret = allocate_buffers(s)) < 0)
     503           0 :         return ret;
     504             : 
     505           2 :     if ((ret = init_offset(s)) < 0)
     506           0 :         return ret;
     507             : 
     508           2 :     s->cur_chan = 0;
     509           2 :     s->bitshift = 0;
     510             : 
     511           2 :     s->got_header = 1;
     512             : 
     513           2 :     return 0;
     514             : }
     515             : 
     516        2581 : static int shorten_decode_frame(AVCodecContext *avctx, void *data,
     517             :                                 int *got_frame_ptr, AVPacket *avpkt)
     518             : {
     519        2581 :     AVFrame *frame     = data;
     520        2581 :     const uint8_t *buf = avpkt->data;
     521        2581 :     int buf_size       = avpkt->size;
     522        2581 :     ShortenContext *s  = avctx->priv_data;
     523        2581 :     int i, input_buf_size = 0;
     524             :     int ret;
     525             : 
     526             :     /* allocate internal bitstream buffer */
     527        2581 :     if (s->max_framesize == 0) {
     528             :         void *tmp_ptr;
     529           2 :         s->max_framesize = 8192; // should hopefully be enough for the first header
     530           2 :         tmp_ptr = av_fast_realloc(s->bitstream, &s->allocated_bitstream_size,
     531           2 :                                   s->max_framesize + AV_INPUT_BUFFER_PADDING_SIZE);
     532           2 :         if (!tmp_ptr) {
     533           0 :             s->max_framesize = 0;
     534           0 :             av_log(avctx, AV_LOG_ERROR, "error allocating bitstream buffer\n");
     535           0 :             return AVERROR(ENOMEM);
     536             :         }
     537           2 :         memset(tmp_ptr, 0, s->allocated_bitstream_size);
     538           2 :         s->bitstream = tmp_ptr;
     539             :     }
     540             : 
     541             :     /* append current packet data to bitstream buffer */
     542        2581 :     buf_size       = FFMIN(buf_size, s->max_framesize - s->bitstream_size);
     543        2581 :     input_buf_size = buf_size;
     544             : 
     545        5162 :     if (s->bitstream_index + s->bitstream_size + buf_size + AV_INPUT_BUFFER_PADDING_SIZE >
     546        2581 :         s->allocated_bitstream_size) {
     547        1411 :         memmove(s->bitstream, &s->bitstream[s->bitstream_index],
     548        1411 :                 s->bitstream_size);
     549        1411 :         s->bitstream_index = 0;
     550             :     }
     551        2581 :     if (buf)
     552        2566 :         memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf,
     553             :                buf_size);
     554        2581 :     buf               = &s->bitstream[s->bitstream_index];
     555        2581 :     buf_size         += s->bitstream_size;
     556        2581 :     s->bitstream_size = buf_size;
     557             : 
     558             :     /* do not decode until buffer has at least max_framesize bytes or
     559             :      * the end of the file has been reached */
     560        2581 :     if (buf_size < s->max_framesize && avpkt->data) {
     561        1028 :         *got_frame_ptr = 0;
     562        1028 :         return input_buf_size;
     563             :     }
     564             :     /* init and position bitstream reader */
     565        1553 :     if ((ret = init_get_bits8(&s->gb, buf, buf_size)) < 0)
     566           0 :         return ret;
     567        1553 :     skip_bits(&s->gb, s->bitindex);
     568             : 
     569             :     /* process header or next subblock */
     570        1553 :     if (!s->got_header) {
     571             : 
     572           2 :         if ((ret = read_header(s)) < 0)
     573           0 :             return ret;
     574             : 
     575           2 :         if (avpkt->size) {
     576             :             int max_framesize;
     577             :             void *tmp_ptr;
     578             : 
     579           2 :             max_framesize = FFMAX(s->max_framesize, s->blocksize * s->channels * 8);
     580           2 :             tmp_ptr = av_fast_realloc(s->bitstream, &s->allocated_bitstream_size,
     581           2 :                                       max_framesize + AV_INPUT_BUFFER_PADDING_SIZE);
     582           2 :             if (!tmp_ptr) {
     583           0 :                 av_log(avctx, AV_LOG_ERROR, "error allocating bitstream buffer\n");
     584           0 :                 return AVERROR(ENOMEM);
     585             :             }
     586           2 :             s->bitstream = tmp_ptr;
     587           2 :             s->max_framesize = max_framesize;
     588           2 :             *got_frame_ptr = 0;
     589           2 :             goto finish_frame;
     590             :         }
     591             :     }
     592             : 
     593             :     /* if quit command was read previously, don't decode anything */
     594        1551 :     if (s->got_quit_command) {
     595           0 :         *got_frame_ptr = 0;
     596           0 :         return avpkt->size;
     597             :     }
     598             : 
     599        1551 :     s->cur_chan = 0;
     600        6201 :     while (s->cur_chan < s->channels) {
     601             :         unsigned cmd;
     602             :         int len;
     603             : 
     604        3101 :         if (get_bits_left(&s->gb) < 3 + FNSIZE) {
     605           2 :             *got_frame_ptr = 0;
     606           2 :             break;
     607             :         }
     608             : 
     609        3099 :         cmd = get_ur_golomb_shorten(&s->gb, FNSIZE);
     610             : 
     611        3099 :         if (cmd > FN_VERBATIM) {
     612           0 :             av_log(avctx, AV_LOG_ERROR, "unknown shorten function %d\n", cmd);
     613           0 :             *got_frame_ptr = 0;
     614           0 :             break;
     615             :         }
     616             : 
     617        3099 :         if (!is_audio_command[cmd]) {
     618             :             /* process non-audio command */
     619           0 :             switch (cmd) {
     620           0 :             case FN_VERBATIM:
     621           0 :                 len = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
     622           0 :                 while (len--)
     623           0 :                     get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
     624           0 :                 break;
     625           0 :             case FN_BITSHIFT: {
     626           0 :                 unsigned bitshift = get_ur_golomb_shorten(&s->gb, BITSHIFTSIZE);
     627           0 :                 if (bitshift > 32) {
     628           0 :                     av_log(avctx, AV_LOG_ERROR, "bitshift %d is invalid\n",
     629             :                            bitshift);
     630           0 :                     return AVERROR_INVALIDDATA;
     631             :                 }
     632           0 :                 s->bitshift = bitshift;
     633           0 :                 break;
     634             :             }
     635           0 :             case FN_BLOCKSIZE: {
     636           0 :                 unsigned blocksize = get_uint(s, av_log2(s->blocksize));
     637           0 :                 if (blocksize > s->blocksize) {
     638           0 :                     avpriv_report_missing_feature(avctx,
     639             :                                                   "Increasing block size");
     640           0 :                     return AVERROR_PATCHWELCOME;
     641             :                 }
     642           0 :                 if (!blocksize || blocksize > MAX_BLOCKSIZE) {
     643           0 :                     av_log(avctx, AV_LOG_ERROR, "invalid or unsupported "
     644             :                                                 "block size: %d\n", blocksize);
     645           0 :                     return AVERROR(EINVAL);
     646             :                 }
     647           0 :                 s->blocksize = blocksize;
     648           0 :                 break;
     649             :             }
     650           0 :             case FN_QUIT:
     651           0 :                 s->got_quit_command = 1;
     652           0 :                 break;
     653             :             }
     654           0 :             if (cmd == FN_QUIT)
     655           0 :                 break;
     656             :         } else {
     657             :             /* process audio command */
     658        3099 :             int residual_size = 0;
     659        3099 :             int channel = s->cur_chan;
     660             :             int32_t coffset;
     661             : 
     662             :             /* get Rice code for residual decoding */
     663        3099 :             if (cmd != FN_ZERO) {
     664        3099 :                 residual_size = get_ur_golomb_shorten(&s->gb, ENERGYSIZE);
     665             :                 /* This is a hack as version 0 differed in the definition
     666             :                  * of get_sr_golomb_shorten(). */
     667        3099 :                 if (s->version == 0)
     668           0 :                     residual_size--;
     669        3099 :                 if (residual_size > 30U) {
     670           0 :                     av_log(avctx, AV_LOG_ERROR, "residual size unsupportd: %d\n", residual_size);
     671           0 :                     return AVERROR_INVALIDDATA;
     672             :                 }
     673             :             }
     674             : 
     675             :             /* calculate sample offset using means from previous blocks */
     676        3099 :             if (s->nmean == 0)
     677           0 :                 coffset = s->offset[channel][0];
     678             :             else {
     679        3099 :                 int32_t sum = (s->version < 2) ? 0 : s->nmean / 2;
     680       15495 :                 for (i = 0; i < s->nmean; i++)
     681       12396 :                     sum += s->offset[channel][i];
     682        3099 :                 coffset = sum / s->nmean;
     683        3099 :                 if (s->version >= 2)
     684        3099 :                     coffset = s->bitshift == 0 ? coffset : coffset >> s->bitshift - 1 >> 1;
     685             :             }
     686             : 
     687             :             /* decode samples for this channel */
     688        3099 :             if (cmd == FN_ZERO) {
     689           0 :                 for (i = 0; i < s->blocksize; i++)
     690           0 :                     s->decoded[channel][i] = 0;
     691             :             } else {
     692        3099 :                 if ((ret = decode_subframe_lpc(s, cmd, channel,
     693             :                                                residual_size, coffset)) < 0)
     694           0 :                     return ret;
     695             :             }
     696             : 
     697             :             /* update means with info from the current block */
     698        3099 :             if (s->nmean > 0) {
     699        3099 :                 int32_t sum = (s->version < 2) ? 0 : s->blocksize / 2;
     700      796443 :                 for (i = 0; i < s->blocksize; i++)
     701      793344 :                     sum += s->decoded[channel][i];
     702             : 
     703       12396 :                 for (i = 1; i < s->nmean; i++)
     704        9297 :                     s->offset[channel][i - 1] = s->offset[channel][i];
     705             : 
     706        3099 :                 if (s->version < 2)
     707           0 :                     s->offset[channel][s->nmean - 1] = sum / s->blocksize;
     708             :                 else
     709        3099 :                     s->offset[channel][s->nmean - 1] = s->bitshift == 32 ? 0 : (sum / s->blocksize) << s->bitshift;
     710             :             }
     711             : 
     712             :             /* copy wrap samples for use with next block */
     713       12396 :             for (i = -s->nwrap; i < 0; i++)
     714        9297 :                 s->decoded[channel][i] = s->decoded[channel][i + s->blocksize];
     715             : 
     716             :             /* shift samples to add in unused zero bits which were removed
     717             :              * during encoding */
     718        3099 :             fix_bitshift(s, s->decoded[channel]);
     719             : 
     720             :             /* if this is the last channel in the block, output the samples */
     721        3099 :             s->cur_chan++;
     722        3099 :             if (s->cur_chan == s->channels) {
     723             :                 uint8_t *samples_u8;
     724             :                 int16_t *samples_s16;
     725             :                 int chan;
     726             : 
     727             :                 /* get output buffer */
     728        1549 :                 frame->nb_samples = s->blocksize;
     729        1549 :                 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
     730           0 :                     return ret;
     731             : 
     732        4647 :                 for (chan = 0; chan < s->channels; chan++) {
     733        3098 :                     samples_u8  = ((uint8_t **)frame->extended_data)[chan];
     734        3098 :                     samples_s16 = ((int16_t **)frame->extended_data)[chan];
     735      796186 :                     for (i = 0; i < s->blocksize; i++) {
     736      793088 :                         switch (s->internal_ftype) {
     737           0 :                         case TYPE_U8:
     738           0 :                             *samples_u8++ = av_clip_uint8(s->decoded[chan][i]);
     739           0 :                             break;
     740      793088 :                         case TYPE_S16HL:
     741             :                         case TYPE_S16LH:
     742      793088 :                             *samples_s16++ = av_clip_int16(s->decoded[chan][i]);
     743      793088 :                             break;
     744             :                         }
     745             :                     }
     746        3098 :                     if (s->swap && s->internal_ftype != TYPE_U8)
     747           0 :                         s->bdsp.bswap16_buf(((uint16_t **)frame->extended_data)[chan],
     748           0 :                                             ((uint16_t **)frame->extended_data)[chan],
     749             :                                             s->blocksize);
     750             : 
     751             :                 }
     752             : 
     753        1549 :                 *got_frame_ptr = 1;
     754             :             }
     755             :         }
     756             :     }
     757        1551 :     if (s->cur_chan < s->channels)
     758           2 :         *got_frame_ptr = 0;
     759             : 
     760        3102 : finish_frame:
     761        1553 :     s->bitindex = get_bits_count(&s->gb) - 8 * (get_bits_count(&s->gb) / 8);
     762        1553 :     i           = get_bits_count(&s->gb) / 8;
     763        1553 :     if (i > buf_size) {
     764           1 :         av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
     765           1 :         s->bitstream_size  = 0;
     766           1 :         s->bitstream_index = 0;
     767           1 :         return AVERROR_INVALIDDATA;
     768             :     }
     769        1552 :     if (s->bitstream_size) {
     770        1551 :         s->bitstream_index += i;
     771        1551 :         s->bitstream_size  -= i;
     772        1551 :         return input_buf_size;
     773             :     } else
     774           1 :         return i;
     775             : }
     776             : 
     777           2 : static av_cold int shorten_decode_close(AVCodecContext *avctx)
     778             : {
     779           2 :     ShortenContext *s = avctx->priv_data;
     780             :     int i;
     781             : 
     782           6 :     for (i = 0; i < s->channels; i++) {
     783           4 :         s->decoded[i] = NULL;
     784           4 :         av_freep(&s->decoded_base[i]);
     785           4 :         av_freep(&s->offset[i]);
     786             :     }
     787           2 :     av_freep(&s->bitstream);
     788           2 :     av_freep(&s->coeffs);
     789             : 
     790           2 :     return 0;
     791             : }
     792             : 
     793             : AVCodec ff_shorten_decoder = {
     794             :     .name           = "shorten",
     795             :     .long_name      = NULL_IF_CONFIG_SMALL("Shorten"),
     796             :     .type           = AVMEDIA_TYPE_AUDIO,
     797             :     .id             = AV_CODEC_ID_SHORTEN,
     798             :     .priv_data_size = sizeof(ShortenContext),
     799             :     .init           = shorten_decode_init,
     800             :     .close          = shorten_decode_close,
     801             :     .decode         = shorten_decode_frame,
     802             :     .capabilities   = AV_CODEC_CAP_SUBFRAMES | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
     803             :     .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
     804             :                                                       AV_SAMPLE_FMT_U8P,
     805             :                                                       AV_SAMPLE_FMT_NONE },
     806             : };

Generated by: LCOV version 1.13